using System; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; namespace DualScreenDemo { public partial class PrimaryForm { /// /// 「數字搜尋」按鈕點擊事件 /// 1. 更新所有搜尋按鈕的背景圖片,使「數字搜尋」顯示為啟動狀態 /// 2. 在 `pictureBoxSongIDSearch` 顯示對應的圖片 /// 3. 顯示「數字搜尋」相關 UI 元素 /// private void NumberSearchButton2_Click(object sender, EventArgs e) { // 更新搜尋按鈕的背景圖片 (將所有按鈕設為未選中狀態,僅數字搜尋為選中狀態) zhuyinSearchSongButton.BackgroundImage = zhuyinSearchSongNormalBackground; englishSearchSongButton.BackgroundImage = englishSearchSongNormalBackground; pinyinSearchSongButton.BackgroundImage = pinyinSearchSongNormalBackground; wordCountSearchSongButton.BackgroundImage = wordCountSearchSongNormalBackground; handWritingSearchSongButton.BackgroundImage = handWritingSearchNormalBackground; numberSearchSongButton.BackgroundImage = numberSearchSongActiveBackground; // 設定數字搜尋為選中狀態 // 在 `pictureBoxSongIDSearch` 顯示數字搜尋的 UI 圖片 ShowImageOnPictureBoxSongIDSearch(Path.Combine(Application.StartupPath, @"themes\superstar\6-1.png")); // 顯示與數字搜尋相關的 UI 元素 SetPictureBoxSongIDSearchAndButtonsVisibility(true); pictureBoxSongIDSearch.Visible = true; } /// /// 在 pictureBoxSongIDSearch 中顯示指定路徑的圖片,並對其進行裁切與調整大小 /// 1. 加載原始圖片 /// 2. 裁切圖片以符合 UI 需求 /// 3. 設定 pictureBoxSongIDSearch 的圖片 /// 4. 調整 pictureBoxSongIDSearch 的位置與大小 /// 5. 顯示 pictureBoxSongIDSearch /// /// 要顯示的圖片路徑 private void ShowImageOnPictureBoxSongIDSearch(string imagePath) { // 1. 加載原始圖片 Bitmap originalImage = new Bitmap(imagePath); // 2. 定義裁切區域 (左上角 x=593, y=135, 寬=507, 高=508) Rectangle cropArea = new Rectangle(593, 135, 507, 508); // 3. 進行圖片裁切 Bitmap croppedImage = CropImage(originalImage, cropArea); // 4. 設定 `pictureBoxSongIDSearch` 的圖片 pictureBoxSongIDSearch.Image = croppedImage; // 5. 調整 `pictureBoxSongIDSearch` 的大小與位置 (基於 cropArea 並加上偏移量) ResizeAndPositionPictureBox(pictureBoxSongIDSearch, cropArea.X + offsetXSongID, cropArea.Y + offsetYSongID, cropArea.Width, cropArea.Height); // 6. 顯示 `pictureBoxSongIDSearch` pictureBoxSongIDSearch.Visible = true; } /// /// 當使用者點擊「修改按鈕」時,刪除 `inputBoxSongIDSearch` 輸入框內的最後一個字元。 /// /// 觸發事件的對象(按鈕) /// 事件參數 private void ModifyButtonSongIDSearch_Click(object sender, EventArgs e) { // 確保 `inputBoxSongIDSearch` 內有文字,避免 `Substring` 發生錯誤 if (inputBoxSongIDSearch.Text.Length > 0) { // 刪除輸入框內的最後一個字元 inputBoxSongIDSearch.Text = inputBoxSongIDSearch.Text.Substring(0, inputBoxSongIDSearch.Text.Length - 1); } } /// /// 當使用者點擊「關閉按鈕」時,隱藏歌曲編號搜尋的相關 UI 元件。 /// /// 觸發事件的對象(按鈕) /// 事件參數 private void CloseButtonSongIDSearch_Click(object sender, EventArgs e) { // 隱藏歌曲編號搜尋的相關 UI 元件(包含輸入框、按鈕、圖片等) SetPictureBoxSongIDSearchAndButtonsVisibility(false); } /// /// 控制歌曲編號搜尋的 UI 顯示與隱藏。 /// true 時,顯示 UI 元件。 /// false 時,隱藏 UI 元件。 /// /// 是否顯示 UI private void SetPictureBoxSongIDSearchAndButtonsVisibility(bool isVisible) { // 定義 UI 更新的 Action System.Action action = () => { // 暫停佈局更新,提高效能 SuspendLayout(); // 控制歌曲編號搜尋圖片的顯示/隱藏 pictureBoxSongIDSearch.Visible = isVisible; // 如果顯示圖片,則將其置頂 if (isVisible) pictureBoxSongIDSearch.BringToFront(); // 控制「修改」和「關閉」按鈕的可見性 modifyButtonSongIDSearch.Visible = isVisible; closeButtonSongIDSearch.Visible = isVisible; // 如果按鈕顯示,則置頂 if (isVisible) { modifyButtonSongIDSearch.BringToFront(); closeButtonSongIDSearch.BringToFront(); } // 控制數字按鈕的可見性 foreach (Button button in numberButtonsSongIDSearch) { button.Visible = isVisible; // 使按鈕顯示時位於最上層 if (isVisible) button.BringToFront(); } // 控制輸入框的可見性 inputBoxSongIDSearch.Visible = isVisible; if (isVisible) inputBoxSongIDSearch.BringToFront(); // 恢復佈局更新 ResumeLayout(); }; // 如果當前執行緒不是 UI 執行緒,則使用 Invoke 呼叫 if (this.InvokeRequired) { this.Invoke(action); } else { action(); } } /// /// 初始化數字輸入 (Song ID Search) 相關按鈕與輸入框。 /// 這些按鈕用於數字輸入、刪除 (Modify) 及關閉 (Close) 功能。 /// private void InitializeButtonsForPictureBoxSongIDSearch() { // 數字按鈕的座標陣列 (左上角 X, 左上角 Y, 右下角 X, 右下角 Y) int[,] coords = new int[,] { {651, 292, 752, 400}, // 1 {760, 292, 861, 400}, // 2 {869, 292, 972, 399}, // 3 {652, 401, 752, 502}, // 4 {760, 401, 861, 504}, // 5 {869, 398, 972, 502}, // 6 {651, 502, 753, 607}, // 7 {759, 504, 863, 607}, // 8 {869, 503, 973, 608}, // 9 {981, 501, 1083, 609} // 0 }; // 取得螢幕的解析度 int screenW = Screen.PrimaryScreen.Bounds.Width; int screenH = Screen.PrimaryScreen.Bounds.Height; // 計算畫面縮放比例 float widthRatio = screenW / (float)1920; float heightRatio = screenH / (float)900; // 初始化數字按鈕陣列 numberButtonsSongIDSearch = new Button[10]; // 建立數字按鈕 for (int i = 0; i < numberButtonsSongIDSearch.Length; i++) { numberButtonsSongIDSearch[i] = new Button(); // 設定按鈕大小與位置 ConfigureButton( numberButtonsSongIDSearch[i], coords[i, 0], // X 座標 coords[i, 1], // Y 座標 coords[i, 2] - coords[i, 0], // 按鈕寬度 coords[i, 3] - coords[i, 1], // 按鈕高度 resizedNormalStateImageFor6_1, // 一般狀態圖片 resizedMouseOverImageFor6_1, // 滑鼠懸停圖片 resizedMouseDownImageFor6_1, // 按下時圖片 null ); // 重新計算按鈕位置,確保適應不同解析度 int newXForSongID = (int)(((numberButtonsSongIDSearch[i].Location.X / widthRatio) + offsetXSongID) * widthRatio); int newYForSongID = (int)(((numberButtonsSongIDSearch[i].Location.Y / heightRatio) + offsetYSongID) * heightRatio); numberButtonsSongIDSearch[i].Location = new Point(newXForSongID, newYForSongID); // 設定按鈕名稱與 Tag (數字值) numberButtonsSongIDSearch[i].Name = "NumberButtonSongIDSearch" + i; numberButtonsSongIDSearch[i].Tag = (i + 1).ToString(); if (i == 9) // 最後一個按鈕 (實際上應為數字 "0") { numberButtonsSongIDSearch[i].Name = "NumberButtonSongIDSearch0"; numberButtonsSongIDSearch[i].Tag = "0"; } // 設定按鈕的點擊事件 numberButtonsSongIDSearch[i].Click += SongIDSearchButton_Click; // 加入畫面 this.Controls.Add(numberButtonsSongIDSearch[i]); } // 初始化「刪除」按鈕 (Modify) modifyButtonSongIDSearch = new Button { Name = "ModifyButtonSongIDSearch", Tag = "Modify", Visible = false }; ConfigureButton(modifyButtonSongIDSearch, 978, 292, 1081 - 978, 397 - 292, resizedNormalStateImageFor6_1, resizedMouseOverImageFor6_1, resizedMouseDownImageFor6_1, ModifyButtonSongIDSearch_Click); // 調整刪除按鈕位置以適應不同解析度 int newX = (int)(((modifyButtonSongIDSearch.Location.X / widthRatio) + offsetXSongID) * widthRatio); int newY = (int)(((modifyButtonSongIDSearch.Location.Y / heightRatio) + offsetYSongID) * heightRatio); modifyButtonSongIDSearch.Location = new Point(newX, newY); this.Controls.Add(modifyButtonSongIDSearch); // 初始化「關閉」按鈕 (Close) closeButtonSongIDSearch = new Button { Name = "CloseButtonSongIDSearch", Tag = "Close", Visible = false }; ConfigureButton(closeButtonSongIDSearch, 982, 147, 1082 - 982, 250 - 147, resizedNormalStateImageFor6_1, resizedMouseOverImageFor6_1, resizedMouseDownImageFor6_1, CloseButtonSongIDSearch_Click); // 調整關閉按鈕位置以適應不同解析度 newX = (int)(((closeButtonSongIDSearch.Location.X / widthRatio) + offsetXSongID) * widthRatio); newY = (int)(((closeButtonSongIDSearch.Location.Y / heightRatio) + offsetYSongID) * heightRatio); closeButtonSongIDSearch.Location = new Point(newX, newY); this.Controls.Add(closeButtonSongIDSearch); // 初始化輸入框 (RichTextBox) inputBoxSongIDSearch = new RichTextBox { Name = "inputBoxSongIDSearch", ForeColor = Color.Black, Font = new Font("細明體", (float)26 / 900 * Screen.PrimaryScreen.Bounds.Height, FontStyle.Regular) }; // 調整輸入框大小與位置 ResizeAndPositionControl(inputBoxSongIDSearch, 645 + offsetXSongID, 197 + offsetXSongID, 986 - 645, 281 - 197); // 當輸入框內容改變時,自動搜尋歌曲 inputBoxSongIDSearch.TextChanged += (sender, e) => { string searchText = inputBoxSongIDSearch.Text; // 使用 LINQ 搜尋符合條件的歌曲 (以輸入的數字開頭) var searchResults = allSongs.Where(song => song.SongNumber.StartsWith(searchText)).ToList(); // 重設分頁與歌曲列表 currentPage = 0; currentSongList = searchResults; totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage); // 更新歌曲顯示 multiPagePanel.currentPageIndex = 0; multiPagePanel.LoadSongs(currentSongList); }; this.Controls.Add(inputBoxSongIDSearch); } /// /// 數字按鈕點擊事件,將按鈕上的數字 (Tag 值) 加入到 inputBoxSongIDSearch 中。 /// /// 觸發事件的按鈕 /// 事件參數 private void SongIDSearchButton_Click(object sender, EventArgs e) { // 嘗試將 sender 轉型為 Button var button = sender as Button; // 確保按鈕不為 null 且具有 Tag 屬性 (Tag 屬性存放數字) if (button != null && button.Tag != null) { // 將按鈕的 Tag 值 (數字) 加入到輸入框 inputBoxSongIDSearch.Text += button.Tag.ToString(); } } } }