歌曲查詢字數註解

This commit is contained in:
jasonchenwork 2025-03-19 09:35:34 +08:00
parent 873a8fee13
commit 3b3111d227

View File

@ -8,182 +8,237 @@ namespace DualScreenDemo
{
public partial class PrimaryForm
{
/// <summary>
/// 當使用者點擊「詞數查歌」按鈕時,切換到詞數查歌模式,更新 UI 並顯示對應的查歌界面。
/// </summary>
/// <param name="sender">觸發事件的按鈕。</param>
/// <param name="e">事件參數。</param>
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;
// 設定查歌模式按鈕的背景圖片
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.Visible = true; // 顯示 PictureBoxWordCount
}
/// <summary>
/// 顯示詞數查歌對應的圖片
/// </summary>
/// <param name="imagePath">圖片路徑</param>
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;
ResizeAndPositionPictureBox(pictureBoxWordCount, cropArea.X + offsetXWordCount, cropArea.Y + offsetXWordCount, cropArea.Width, cropArea.Height);
// 調整 pictureBoxWordCount 的大小與位置
ResizeAndPositionPictureBox(pictureBoxWordCount,
cropArea.X + offsetXWordCount, // X 座標 (包含位移量)
cropArea.Y + offsetYWordCount, // Y 座標 (包含位移量)
cropArea.Width, // 寬度
cropArea.Height // 高度
);
// 顯示 pictureBoxWordCount
pictureBoxWordCount.Visible = true;
}
private void InitializeButtonsForPictureBoxWordCount()
{
int[,] coords = new int[,]
/// <summary>
/// 初始化「詞數查歌」模式的按鈕與輸入框,並根據螢幕解析度進行調整。
/// </summary>
private void InitializeButtonsForPictureBoxWordCount()
{
{651, 292, 752, 400},
{760, 292, 861, 400},
{869, 292, 972, 399},
{652, 401, 752, 502},
{760, 401, 861, 504},
{869, 398, 972, 502},
{651, 502, 753, 607},
{759, 504, 863, 607},
{869, 503, 973, 608},
{981, 501, 1083, 609}
};
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];
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();
if (i == 9)
// 定義數字按鈕的座標範圍 (左上角 X, Y 以及右下角 X, Y)
int[,] coords = new int[,]
{
numberButtonsWordCount[i].Name = "NumberButtonWordCount0";
numberButtonsWordCount[i].Tag = "0";
{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]);
}
numberButtonsWordCount[i].Click += WordCountButton_Click;
// 建立「修改」按鈕 (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);
this.Controls.Add(numberButtonsWordCount[i]);
// 調整「修改」按鈕的位置
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);
}
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 / widthRatio) + offsetYWordCount) * heightRatio);
modifyButtonWordCount.Location = new Point(newX, newY);
this.Controls.Add(modifyButtonWordCount);
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 / widthRatio) + offsetYWordCount) * heightRatio);
closeButtonWordCount.Location = new Point(newX, newY);
this.Controls.Add(closeButtonWordCount);
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);
}
private void WordCountButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null && button.Tag != null)
/// <summary>
/// 處理數字按鈕的點擊事件,將按鈕對應的數字 (Tag) 加入輸入框 inputBoxWordCount 中。
/// </summary>
/// <param name="sender">觸發事件的按鈕</param>
/// <param name="e">事件參數</param>
private void WordCountButton_Click(object sender, EventArgs e)
{
inputBoxWordCount.Text += button.Tag.ToString();
}
}
// 嘗試將 sender 轉換為 Button 類型
var button = sender as Button;
private void CloseButtonWordCount_Click(object sender, EventArgs e)
{
SetPictureBoxWordCountAndButtonsVisibility(false);
}
// 確保按鈕不為空,且擁有有效的 Tag 值
if (button != null && button.Tag != null)
{
// 將按鈕的 Tag 值 (數字) 追加到輸入框中
inputBoxWordCount.Text += button.Tag.ToString();
}
}
/// <summary>
/// 關閉詞數輸入界面,隱藏相關的 PictureBox 和按鈕。
/// </summary>
/// <param name="sender">觸發事件的按鈕。</param>
/// <param name="e">事件參數。</param>
private void CloseButtonWordCount_Click(object sender, EventArgs e)
{
// 設定詞數輸入界面及其按鈕的可見性為 false使其隱藏
SetPictureBoxWordCountAndButtonsVisibility(false);
}
}
}