From c4b8b82b64c1d3228c746e840345877a6fb43049 Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Thu, 22 May 2025 13:33:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=88=91=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E6=84=9B=E6=96=B0=E5=BB=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrimaryFormParts/PrimaryForm.Favorite.cs | 39 ++++++++----- PrimaryFormParts/PrimaryForm.SQLSearch.cs | 68 +++++++++++++++++------ PrimaryFormParts/PrimaryForm.cs | 30 ++++++++++ 3 files changed, 106 insertions(+), 31 deletions(-) diff --git a/PrimaryFormParts/PrimaryForm.Favorite.cs b/PrimaryFormParts/PrimaryForm.Favorite.cs index 69f5a8e..a74e81d 100644 --- a/PrimaryFormParts/PrimaryForm.Favorite.cs +++ b/PrimaryFormParts/PrimaryForm.Favorite.cs @@ -239,8 +239,8 @@ namespace DualScreenDemo } else { - showError = true; - isPhoneNumberValid = true; + showError = true; + isPhoneNumberValid = false; FavoritePictureBox.Invalidate(); FavoritePictureBox.Refresh(); } @@ -254,7 +254,7 @@ namespace DualScreenDemo if (checkPhoneNumberExist(mobileNumber)) { isPhoneNumberValid = true; - var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql()); + var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql()); // 重置分頁 currentPage = 0; currentSongList = searchResults; @@ -267,22 +267,35 @@ namespace DualScreenDemo } else { - /*isPhoneNumberValid = true; - SongListManager.Instance.AddNewUser(mobileNumber); - SongListManager.Instance.UserLogin(mobileNumber); + isPhoneNumberValid = true; + InsertNewFavoriteUser(mobileNumber); + if (checkPhoneNumberExist(mobileNumber)) + { + var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql()); + // 重置分頁 + currentPage = 0; + currentSongList = searchResults; + totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage); + // 更新多頁面面板的內容 + multiPagePanel.currentPageIndex = 0; + multiPagePanel.LoadSongs(currentSongList); + + ToggleFavoritePictureBoxButtonsVisibility(); + } + else + { + showError = true; + isPhoneNumberValid = false; + FavoritePictureBox.Invalidate(); + FavoritePictureBox.Refresh(); + } - - List emptySongList = new List { new SongData("", "", "歡迎光臨 " + "(" + mobileNumber + ")", 0, "", "", "", "", DateTime.Now, "", "", "", "", "", "", "", "", "", "", "", "", 1) }; - - multiPagePanel.currentPageIndex = 0; - multiPagePanel.LoadSongs(emptySongList);*/ - ToggleFavoritePictureBoxButtonsVisibility(); } } else { showError = true; - isPhoneNumberValid = true; + isPhoneNumberValid = false; FavoritePictureBox.Invalidate(); FavoritePictureBox.Refresh(); } diff --git a/PrimaryFormParts/PrimaryForm.SQLSearch.cs b/PrimaryFormParts/PrimaryForm.SQLSearch.cs index d0dfa1a..81f8464 100644 --- a/PrimaryFormParts/PrimaryForm.SQLSearch.cs +++ b/PrimaryFormParts/PrimaryForm.SQLSearch.cs @@ -124,9 +124,38 @@ namespace DualScreenDemo{ Console.WriteLine("MyDB 連線已關閉!"); } } - public string SearchFavoriteSongs_Mysql(){ + public void InsertNewFavoriteUser(string phonenumber){ + string songlist = phonenumber + "的歌單"; + string query = $"INSERT INTO FavoriteSongs (userPhone,songNumber) VALUES ('{phonenumber}','{songlist}');"; + Console.WriteLine(query); + string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;"; + + using (var connection = new MySqlConnection(connectionString)) + { + + connection.Open(); + Console.WriteLine("MyDB 連線成功!"); + + using (var command = new MySqlCommand(query, connection)) + { + int rowsAffected = command.ExecuteNonQuery(); + if (rowsAffected > 0) + { + Console.WriteLine("插入成功!"); + } + else + { + Console.WriteLine("插入失敗(沒有資料被新增)"); + } + } + connection.Close(); + Console.WriteLine("MyDB 連線已關閉!"); + } + } + public string SearchFavoriteSongs_Mysql() + { string query = $"SELECT " + - "sl.*"+ + "sl.*" + "FROM FavoriteSongs fs " + "JOIN song_library_cache sl ON fs.songNumber = sl.song_id " + $"WHERE fs.userPhone = '{userPhone}';"; @@ -141,25 +170,28 @@ namespace DualScreenDemo{ connection.Open(); Console.WriteLine("MyDB 連線成功!"); string query = $"SELECT COUNT(*) FROM FavoriteSongs WHERE userPhone = '{phonenumber}'"; - using (var command = new MySqlCommand(query, connection)){ - int count = Convert.ToInt32(command.ExecuteScalar()); - if (count > 0) - { - exists = true; // 存在 - userPhone= phonenumber; - isLoggedIn = true; - //點歌我的最愛按鈕,調整按鈕狀態 - favoriteButton.Enabled = true; - favoriteButton.BackColor = Color.Transparent; - disabledPanel.Visible = false; - } - else - { - exists = false; // 不存在 - } + using (var command = new MySqlCommand(query, connection)) + { + int count = Convert.ToInt32(command.ExecuteScalar()); + if (count > 0) + { + exists = true; // 存在 + userPhone = phonenumber; + isLoggedIn = true; + //點歌我的最愛按鈕,調整按鈕狀態 + favoriteButton.Enabled = true; + favoriteButton.BackColor = Color.Transparent; + disabledPanel.Visible = false; + this.Invalidate(); + } + else + { + exists = false; // 不存在 + } } connection.Close(); Console.WriteLine("MyDB 連線關閉!"); + } return exists; } diff --git a/PrimaryFormParts/PrimaryForm.cs b/PrimaryFormParts/PrimaryForm.cs index 4d90c23..ec9833b 100644 --- a/PrimaryFormParts/PrimaryForm.cs +++ b/PrimaryFormParts/PrimaryForm.cs @@ -234,6 +234,7 @@ namespace DualScreenDemo SaveInitialControlStates(this); // 包廂 + port 名稱 this.Paint += PrimaryForm_Paint; + this.Paint += User_Paint; // 註冊多頁面面板的頁碼改變事件處理 multiPagePanel.PageIndexChanged += HandlePageChanged; // 添加 Load 事件處理 @@ -302,7 +303,36 @@ namespace DualScreenDemo e.Graphics.DrawString(displayName, font, brush, point_PCName); } + private void User_Paint(object sender, PaintEventArgs e) + { + if (!string.IsNullOrWhiteSpace(userPhone) && isLoggedIn) + { + string displayName = "歡迎貴賓 :" + userPhone; + float baseWidth = 1920f; + float baseHeight = 1080f; + // 實際螢幕解析度 + float actualWidth = Screen.PrimaryScreen.Bounds.Width; + float actualHeight = Screen.PrimaryScreen.Bounds.Height; + + // 計算縮放比例 + float scaleX = actualWidth / baseWidth; + float scaleY = actualHeight / baseHeight; + + + // 設定字型: + // "微軟正黑體",大小 20,粗體 (Bold) + float fontSize = 18 * scaleY; + Font font = new Font("微軟正黑體", fontSize, FontStyle.Bold); + // 設定畫刷 (Brush): + // 使用紅色 (Red) 來繪製文字 + Brush brush = new SolidBrush(Color.Red); + + // 設定繪製文字的位置 (X=800, Y=30) + PointF point_user = new PointF(800 * scaleX, 30 * scaleY); + e.Graphics.DrawString(displayName, font, brush, point_user); + } + } private void buttonMiddle_Click(object sender, EventArgs e) { sequenceManager.ProcessClick("巨");