using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using System.IO; namespace DualScreenDemo { public partial class PrimaryForm { /// /// 當使用者點擊「詞數查歌」按鈕時,切換到詞數查歌模式,更新 UI 並顯示對應的查歌界面。 /// /// 觸發事件的按鈕。 /// 事件參數。 private void WordCountSearchSong_Click(object sender, EventArgs e) { // 設定查歌模式按鈕的背景圖片 zhuyinSearchSongButton.BackgroundImage = zhuyinSearchSongNormalBackground; // 注音查歌(普通狀態) englishSearchSongButton.BackgroundImage = englishSearchSongNormalBackground; // 英文查歌(普通狀態) pinyinSearchSongButton.BackgroundImage = pinyinSearchSongNormalBackground; // 拼音查歌(普通狀態) wordCountSearchSongButton.BackgroundImage = wordCountSearchSongActiveBackground; // 詞數查歌(選中狀態) handWritingSearchSongButton.BackgroundImage = handWritingSearchNormalBackground; // 手寫查歌(普通狀態) numberSearchSongButton.BackgroundImage = numberSearchSongNormalBackground; // 數字查歌(普通狀態) // 顯示詞數查歌對應的圖片 ShowImageOnPictureBoxWordCount(Path.Combine(Application.StartupPath, @"themes\superstar\6-1.png")); // 設定詞數查歌的 PictureBox 和按鈕的可見性 SetPictureBoxWordCountAndButtonsVisibility(true); pictureBoxWordCount.Visible = true; // 顯示 PictureBoxWordCount } /// /// 顯示詞數查歌對應的圖片 /// /// 圖片路徑 private void ShowImageOnPictureBoxWordCount(string imagePath) { // 載入原始圖片 Bitmap originalImage = new Bitmap(imagePath); // 定義裁切區域 (起始座標: 593, 135,寬度: 507,高度: 508) Rectangle cropArea = new Rectangle(593, 135, 507, 508); // 裁切圖片,獲得指定範圍的影像 Bitmap croppedImage = CropImage(originalImage, cropArea); // 將裁切後的圖片顯示在 pictureBoxWordCount 上 pictureBoxWordCount.Image = croppedImage; // 調整 pictureBoxWordCount 的大小與位置 ResizeAndPositionPictureBox(pictureBoxWordCount, cropArea.X + offsetXWordCount, // X 座標 (包含位移量) cropArea.Y + offsetYWordCount, // Y 座標 (包含位移量) cropArea.Width, // 寬度 cropArea.Height // 高度 ); // 顯示 pictureBoxWordCount pictureBoxWordCount.Visible = true; } /// /// 初始化「詞數查歌」模式的按鈕與輸入框,並根據螢幕解析度進行調整。 /// private void InitializeButtonsForPictureBoxWordCount() { // 定義數字按鈕的座標範圍 (左上角 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)1440; float heightRatio = screenH / (float)900; // 建立數字按鈕陣列 numberButtonsWordCount = new Button[10]; // 依序建立 0-9 按鈕 for (int i = 0; i < numberButtonsWordCount.Length; i++) { numberButtonsWordCount[i] = new Button(); // 設定按鈕屬性 (位置、大小、圖片) ConfigureButton( numberButtonsWordCount[i], coords[i, 0], coords[i, 1], coords[i, 2] - coords[i, 0], // 計算按鈕寬度 coords[i, 3] - coords[i, 1], // 計算按鈕高度 resizedNormalStateImageFor6_1, resizedMouseOverImageFor6_1, resizedMouseDownImageFor6_1, null ); // 調整按鈕位置,使其適應不同解析度 int newXForWordCount = (int)(((numberButtonsWordCount[i].Location.X / widthRatio) + offsetXWordCount) * widthRatio); int newYForWordCount = (int)(((numberButtonsWordCount[i].Location.Y / heightRatio) + offsetYWordCount) * heightRatio); numberButtonsWordCount[i].Location = new Point(newXForWordCount, newYForWordCount); // 設定按鈕名稱 numberButtonsWordCount[i].Name = "NumberButtonWordCount" + i; numberButtonsWordCount[i].Tag = (i + 1).ToString(); // 處理按鈕 0 的特殊名稱與標籤 if (i == 9) { numberButtonsWordCount[i].Name = "NumberButtonWordCount0"; numberButtonsWordCount[i].Tag = "0"; } // 設定點擊事件 numberButtonsWordCount[i].Click += WordCountButton_Click; // 將按鈕加入視窗控制項 this.Controls.Add(numberButtonsWordCount[i]); } // 建立「修改」按鈕 (Modify) modifyButtonWordCount = new Button { Name = "ModifyButtonWordCount", Tag = "Modify", Visible = false }; ConfigureButton(modifyButtonWordCount, 978, 292, 1081 - 978, 397 - 292, resizedNormalStateImageFor6_1, resizedMouseOverImageFor6_1, resizedMouseDownImageFor6_1, ModifyButtonWordCount_Click); // 調整「修改」按鈕的位置 int newX = (int)(((modifyButtonWordCount.Location.X / widthRatio) + offsetXWordCount) * widthRatio); int newY = (int)(((modifyButtonWordCount.Location.Y / heightRatio) + offsetYWordCount) * heightRatio); modifyButtonWordCount.Location = new Point(newX, newY); // 加入「修改」按鈕至視窗 this.Controls.Add(modifyButtonWordCount); // 建立「關閉」按鈕 (Close) closeButtonWordCount = new Button { Name = "CloseButtonWordCount", Tag = "Close", Visible = false }; ConfigureButton(closeButtonWordCount, 982, 147, 1082 - 982, 250 - 147, resizedNormalStateImageFor6_1, resizedMouseOverImageFor6_1, resizedMouseDownImageFor6_1, CloseButtonWordCount_Click); // 調整「關閉」按鈕的位置 newX = (int)(((closeButtonWordCount.Location.X / widthRatio) + offsetXWordCount) * widthRatio); newY = (int)(((closeButtonWordCount.Location.Y / heightRatio) + offsetYWordCount) * heightRatio); closeButtonWordCount.Location = new Point(newX, newY); // 加入「關閉」按鈕至視窗 this.Controls.Add(closeButtonWordCount); // 建立輸入框 (RichTextBox) 供使用者輸入詞數 inputBoxWordCount = new RichTextBox(); inputBoxWordCount.Name = "inputBoxWordCount"; // 設定輸入框位置與大小 ResizeAndPositionControl(inputBoxWordCount, 645 + offsetXWordCount, 197 + offsetYWordCount, 986 - 645, 281 - 197); inputBoxWordCount.ForeColor = Color.Black; inputBoxWordCount.Font = new Font("細明體", (float)26 / 900 * Screen.PrimaryScreen.Bounds.Height, FontStyle.Regular); // 設定輸入框內容變更時的處理邏輯 inputBoxWordCount.TextChanged += (sender, e) => { string searchText = inputBoxWordCount.Text; int targetLength = 0; // 檢查輸入的是否為數字 if (int.TryParse(searchText, out targetLength)) { // 根據詞數長度篩選歌曲 var searchResults = allSongs.Where(song => song.Song.Replace(" ", "").Length == targetLength).ToList(); currentPage = 0; currentSongList = searchResults; totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage); // 更新多頁面面板 multiPagePanel.currentPageIndex = 0; multiPagePanel.LoadSongs(currentSongList); } else { // 若輸入非數字,則清空當前歌曲列表 currentSongList.Clear(); } }; // 加入輸入框至視窗 this.Controls.Add(inputBoxWordCount); } /// /// 處理數字按鈕的點擊事件,將按鈕對應的數字 (Tag) 加入輸入框 inputBoxWordCount 中。 /// /// 觸發事件的按鈕 /// 事件參數 private void WordCountButton_Click(object sender, EventArgs e) { // 嘗試將 sender 轉換為 Button 類型 var button = sender as Button; // 確保按鈕不為空,且擁有有效的 Tag 值 if (button != null && button.Tag != null) { // 將按鈕的 Tag 值 (數字) 追加到輸入框中 inputBoxWordCount.Text += button.Tag.ToString(); } } /// /// 關閉詞數輸入界面,隱藏相關的 PictureBox 和按鈕。 /// /// 觸發事件的按鈕。 /// 事件參數。 private void CloseButtonWordCount_Click(object sender, EventArgs e) { // 設定詞數輸入界面及其按鈕的可見性為 false,使其隱藏 SetPictureBoxWordCountAndButtonsVisibility(false); } } }