This repository has been archived on 2025-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
superstar/PrimaryFormParts/SingerSearch/PrimaryForm.SingerSearch.BopomofoSearch.cs

838 lines
38 KiB
C#
Raw Normal View History

2025-03-18 11:35:10 +08:00
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using IniParser;
using IniParser.Model;
using System.Text;
namespace DualScreenDemo
{
public partial class PrimaryForm
{
private PictureBox pictureBoxZhuYinSingers;
private Button[] phoneticButtonsForSingers;
private Button modifyButtonZhuYinSingers;
private Button clearButtonZhuYinSingers;
private Button closeButtonZhuYinSingers;
private string[] phoneticSymbols;
private (int X, int Y, int Width, int Height)[] phoneticButtonCoords;
private Dictionary<string, (string normal, string mouseDown, string mouseOver)> phoneticButtonImages;
private (int X, int Y, int Width, int Height) modifyButtonZhuYinCoords;
private (int X, int Y, int Width, int Height) clearButtonZhuYinCoords;
private (int X, int Y, int Width, int Height) closeButtonZhuYinCoords;
private RichTextBox inputBoxZhuYinSingers;
private (int X, int Y, int Width, int Height) inputBoxZhuYinCoords;
private string inputBoxFontName;
private float inputBoxFontSize;
private FontStyle inputBoxFontStyle;
private Color inputBoxForeColor;
2025-03-20 16:21:01 +08:00
/// <summary>
/// <para> 點擊「注音歌手搜尋」按鈕時執行的事件處理函式。</para>
/// <para>此函式負責更新按鈕的背景圖片、載入對應的歌手圖片,並切換相關 UI 控件的可見性。</para>
/// </summary>
/// <param name="sender">觸發事件的物件(通常是按鈕本身)。</param>
/// <param name="e">事件參數。</param>
2025-03-18 11:35:10 +08:00
private void ZhuyinSearchSingersButton_Click(object sender, EventArgs e)
{
2025-03-20 16:21:01 +08:00
// 設定按鈕背景,將「注音搜尋」設為啟動狀態,其餘按鈕恢復為正常狀態
2025-03-18 11:35:10 +08:00
zhuyinSearchButton.BackgroundImage = zhuyinSearchActiveBackground;
englishSearchButton.BackgroundImage = englishSearchNormalBackground;
pinyinSearchButton.BackgroundImage = pinyinSearchNormalBackground;
wordCountSearchButton.BackgroundImage = wordCountSearchNormalBackground;
handWritingSearchButton.BackgroundImage = handWritingSearchNormalBackground;
2025-03-20 16:21:01 +08:00
// 載入設定檔,取得圖片路徑資訊
2025-03-18 11:35:10 +08:00
var configData = LoadConfigData();
2025-03-20 16:21:01 +08:00
// 取得「注音歌手圖片」的完整路徑
2025-03-18 11:35:10 +08:00
string imagePath = Path.Combine(Application.StartupPath, configData["ImagePaths"]["ZhuYinSingers"]);
2025-03-20 16:21:01 +08:00
// 在 PictureBox 中顯示對應的「注音歌手」圖片
2025-03-18 11:35:10 +08:00
ShowImageOnPictureBoxZhuYinSingers(Path.Combine(Application.StartupPath, imagePath));
2025-03-20 16:21:01 +08:00
// 設定不同搜尋模式的 UI 控件可見性
SetEnglishSingersAndButtonsVisibility(false); // 隱藏英文字母搜尋相關控件
SetPinYinSingersAndButtonsVisibility(false); // 隱藏拼音搜尋相關控件
SetHandWritingForSingersAndButtonsVisibility(false); // 隱藏手寫搜尋相關控件
2025-03-27 14:10:02 +08:00
SetWordCountSingersAndButtonsVisibility(false); // 隱藏字數搜尋相關控件
2025-03-28 09:21:32 +08:00
2025-03-20 16:21:01 +08:00
SetZhuYinSingersAndButtonsVisibility(true); // 顯示注音搜尋相關控件
2025-03-24 17:32:33 +08:00
//SetPictureBoxArtistSearchAndButtonsVisibility(false); // 隱藏其他搜尋模式的圖片框
2025-03-18 11:35:10 +08:00
2025-03-27 14:10:02 +08:00
ResetinputBox(); // 重置輸入框
2025-03-27 14:04:19 +08:00
2025-03-20 16:21:01 +08:00
// 顯示「注音歌手搜尋」的圖片框
2025-03-18 11:35:10 +08:00
pictureBoxZhuYinSingers.Visible = true;
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// <para>從 config.ini 設定檔中載入注音符號Phonetic Symbols。</para>
/// <para>讀取 ini 檔的 [PhoneticSymbols] 區塊並將「Symbols」欄位的值解析為陣列。</para>
/// </summary>
2025-03-18 11:35:10 +08:00
private void LoadPhoneticSymbolsFromConfig()
{
2025-03-20 16:21:01 +08:00
// 建立 INI 檔案解析器
2025-03-18 11:35:10 +08:00
var parser = new FileIniDataParser();
2025-03-20 16:21:01 +08:00
// 設定檔路徑
2025-03-18 11:35:10 +08:00
string iniFilePath = "config.ini";
IniData data;
2025-03-20 16:21:01 +08:00
// 以 UTF-8 編碼開啟並讀取 INI 檔案
2025-03-18 11:35:10 +08:00
using (var reader = new StreamReader(iniFilePath, Encoding.UTF8))
{
2025-03-20 16:21:01 +08:00
// 解析 INI 檔內容
2025-03-18 11:35:10 +08:00
data = parser.ReadData(reader);
}
2025-03-20 16:21:01 +08:00
// 取得 [PhoneticSymbols] 區塊中的 "Symbols" 欄位內容
2025-03-18 11:35:10 +08:00
string symbols = data["PhoneticSymbols"]["Symbols"];
2025-03-20 16:21:01 +08:00
// 將符號字串以逗號分隔,轉換為字串陣列
2025-03-18 11:35:10 +08:00
phoneticSymbols = symbols.Split(',');
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從設定檔 (config.ini) 載入 INI 設定數據。
/// </summary>
/// <returns>回傳解析後的 INI 設定數據 (IniData)。</returns>
2025-03-18 11:35:10 +08:00
private IniData LoadConfigData()
{
var parser = new FileIniDataParser();
string iniFilePath = "config.ini";
2025-03-20 16:21:01 +08:00
// 使用 UTF-8 讀取 INI 檔案並解析內容
2025-03-18 11:35:10 +08:00
using (var reader = new StreamReader(iniFilePath, Encoding.UTF8))
{
return parser.ReadData(reader);
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從 INI 設定數據中讀取注音符號 (Phonetic Symbols)。
/// </summary>
/// <param name="data">已解析的 INI 設定數據。</param>
/// <returns>回傳包含注音符號的字串陣列。</returns>
2025-03-18 11:35:10 +08:00
private string[] LoadPhoneticSymbols(IniData data)
{
2025-03-20 16:21:01 +08:00
// 從 INI 檔案的 [PhoneticSymbols] 區塊取得 Symbols 欄位值
2025-03-18 11:35:10 +08:00
string symbols = data["PhoneticSymbols"]["Symbols"];
2025-03-20 16:21:01 +08:00
// 以逗號分隔字串並轉換為字串陣列
2025-03-18 11:35:10 +08:00
return symbols.Split(',');
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從 INI 設定數據中載入按鈕座標資料。
/// </summary>
/// <param name="data">已解析的 INI 設定數據。</param>
/// <param name="section">設定檔中按鈕座標所屬的區塊名稱。</param>
/// <param name="buttonCount">按鈕數量。</param>
/// <returns>回傳包含按鈕座標的陣列,每個元素是由 (X, Y, Width, Height) 組成的元組。</returns>
2025-03-18 11:35:10 +08:00
private (int X, int Y, int Width, int Height)[] LoadButtonCoordinates(IniData data, string section, int buttonCount)
{
var buttonList = new List<(int X, int Y, int Width, int Height)>();
2025-03-20 16:21:01 +08:00
// 迴圈讀取每個按鈕的座標設定
2025-03-18 11:35:10 +08:00
for (int i = 1; i <= buttonCount; i++)
{
2025-03-20 16:21:01 +08:00
// 取得按鈕座標的字串 (格式: X,Y,Width,Height)
2025-03-18 11:35:10 +08:00
var coordString = data[section][$"button{i}"];
var coords = coordString.Split(',');
2025-03-20 16:21:01 +08:00
// 將座標資料轉換為 (X, Y, Width, Height) 元組並加入清單
buttonList.Add((
int.Parse(coords[0]), // X 座標
int.Parse(coords[1]), // Y 座標
int.Parse(coords[2]), // 寬度
int.Parse(coords[3]) // 高度
));
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 回傳所有按鈕座標的陣列
2025-03-18 11:35:10 +08:00
return buttonList.ToArray();
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從 INI 設定數據中載入按鈕圖片檔案路徑資料 (包含正常、點擊、滑鼠移過圖片)。
/// </summary>
/// <param name="data">已解析的 INI 設定數據。</param>
/// <param name="section">設定檔中按鈕圖片設定所屬的區塊名稱。</param>
/// <param name="buttonCount">按鈕數量。</param>
/// <returns>回傳一個字典,鍵是按鈕名稱,值是包含正常、點擊和滑鼠移過狀態的元組。</returns>
2025-03-18 11:35:10 +08:00
private Dictionary<string, (string normal, string mouseDown, string mouseOver)> LoadButtonImages(IniData data, string section, int buttonCount)
{
var buttonImages = new Dictionary<string, (string normal, string mouseDown, string mouseOver)>();
2025-03-20 16:21:01 +08:00
// 迴圈讀取每個按鈕的圖片設定
2025-03-18 11:35:10 +08:00
for (int i = 0; i < 35; i++)
{
2025-03-20 16:21:01 +08:00
// 讀取按鈕的三種圖片狀態:正常、點擊、滑鼠移過
2025-03-18 11:35:10 +08:00
buttonImages[$"button{i}"] = (
2025-03-20 16:21:01 +08:00
data[section][$"button{i}_normal"], // 正常狀態圖片路徑
data[section][$"button{i}_mouseDown"], // 點擊狀態圖片路徑
data[section][$"button{i}_mouseOver"] // 滑鼠移過狀態圖片路徑
2025-03-18 11:35:10 +08:00
);
}
2025-03-20 16:21:01 +08:00
// 回傳包含所有按鈕圖片路徑資料的字典
2025-03-18 11:35:10 +08:00
return buttonImages;
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從 INI 設定數據中載入特定按鈕的座標資料。
/// </summary>
/// <param name="data">已解析的 INI 設定數據。</param>
/// <param name="section">設定檔中按鈕座標所屬的區塊名稱。</param>
/// <param name="buttonKey">指定按鈕的鍵名 (如 "button1")。</param>
/// <returns>回傳包含按鈕座標的元組 (X, Y, Width, Height)。</returns>
2025-03-18 11:35:10 +08:00
private (int X, int Y, int Width, int Height) LoadSpecialButtonCoordinates(IniData data, string section, string buttonKey)
{
2025-03-20 16:21:01 +08:00
// 取得按鈕座標的字串 (格式: X,Y,Width,Height)
2025-03-18 11:35:10 +08:00
var coords = data[section][buttonKey].Split(',');
2025-03-20 16:21:01 +08:00
// 解析座標字串並回傳 (X, Y, Width, Height) 元組
2025-03-18 11:35:10 +08:00
return (int.Parse(coords[0]), int.Parse(coords[1]), int.Parse(coords[2]), int.Parse(coords[3]));
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從 INI 設定數據中載入按鈕的圖片資料 (包含正常、點擊、滑鼠移過圖片)。
/// </summary>
/// <param name="data">已解析的 INI 設定數據。</param>
/// <param name="section">設定檔中按鈕圖片設定所屬的區塊名稱。</param>
/// <returns>回傳包含按鈕三種狀態圖片路徑的元組 (normal, mouseDown, mouseOver)。</returns>
2025-03-18 11:35:10 +08:00
private (string normal, string mouseDown, string mouseOver) LoadButtonImages(IniData data, string section)
{
2025-03-20 16:21:01 +08:00
// 讀取按鈕三種狀態的圖片路徑
2025-03-18 11:35:10 +08:00
return (
2025-03-20 16:21:01 +08:00
data[section]["normal"], // 正常狀態圖片路徑
data[section]["mouseDown"], // 點擊狀態圖片路徑
data[section]["mouseOver"] // 滑鼠移過狀態圖片路徑
2025-03-18 11:35:10 +08:00
);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化並設置語音按鈕的相關資料,包括符號、座標與圖片等。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializePhoneticButtons()
{
2025-03-20 16:21:01 +08:00
// 載入配置資料
2025-03-18 11:35:10 +08:00
var data = LoadConfigData();
2025-03-20 16:21:01 +08:00
// 載入語音符號(如拼音、注音符號等)
2025-03-18 11:35:10 +08:00
phoneticSymbols = LoadPhoneticSymbols(data);
2025-03-20 16:21:01 +08:00
// 載入按鈕座標資料
2025-03-18 11:35:10 +08:00
phoneticButtonCoords = LoadButtonCoordinates(data, "PhoneticButtonCoordinates", 35);
2025-03-20 16:21:01 +08:00
// 載入按鈕圖片資料
2025-03-18 11:35:10 +08:00
phoneticButtonImages = LoadButtonImages(data, "PhoneticButtonImages", 35);
2025-03-20 16:21:01 +08:00
// 初始化語音按鈕陣列,總共有 35 個按鈕
2025-03-18 11:35:10 +08:00
phoneticButtonsForSingers = new Button[35];
2025-03-20 16:21:01 +08:00
// 設置每個語音按鈕
2025-03-18 11:35:10 +08:00
for (int i = 0; i < 35; i++)
{
2025-03-20 16:21:01 +08:00
// 根據按鈕索引讀取其圖片資料
2025-03-18 11:35:10 +08:00
var buttonImages = phoneticButtonImages[$"button{i}"];
2025-03-20 16:21:01 +08:00
// 創建並初始化語音按鈕,設定其背景圖片
2025-03-18 11:35:10 +08:00
CreatePhoneticButton(i, buttonImages.normal, buttonImages.mouseDown, buttonImages.mouseOver);
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 創建一個語音按鈕,並為其設置圖片、座標、事件等屬性。
/// </summary>
/// <param name="index">按鈕的索引,用來獲取對應的語音符號、座標和圖片資料。</param>
/// <param name="normalImagePath">正常狀態下的圖片路徑。</param>
/// <param name="mouseDownImagePath">點擊狀態下的圖片路徑。</param>
/// <param name="mouseOverImagePath">滑鼠移過狀態下的圖片路徑。</param>
2025-03-18 11:35:10 +08:00
private void CreatePhoneticButton(int index, string normalImagePath, string mouseDownImagePath, string mouseOverImagePath)
{
try
{
2025-03-20 16:21:01 +08:00
// 創建語音按鈕並設置其屬性
2025-03-18 11:35:10 +08:00
phoneticButtonsForSingers[index] = new Button
{
2025-03-20 16:21:01 +08:00
Name = $"phoneticButton_{phoneticSymbols[index]}", // 按鈕名稱設為語音符號名稱
BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, normalImagePath)), // 設定背景圖片
BackgroundImageLayout = ImageLayout.Stretch, // 設定圖片拉伸樣式
FlatStyle = FlatStyle.Flat, // 設定為平面風格
FlatAppearance = { BorderSize = 0 } // 設定無邊框
2025-03-18 11:35:10 +08:00
};
2025-03-20 16:21:01 +08:00
// 調整按鈕大小並設置位置
2025-03-18 11:35:10 +08:00
ResizeAndPositionButton(phoneticButtonsForSingers[index], phoneticButtonCoords[index].X, phoneticButtonCoords[index].Y,
phoneticButtonCoords[index].Width, phoneticButtonCoords[index].Height);
2025-03-20 16:21:01 +08:00
// 從檔案中讀取正常、點擊和滑鼠懸停狀態的圖片
2025-03-18 11:35:10 +08:00
Image normalImage = Image.FromFile(Path.Combine(Application.StartupPath, normalImagePath));
Image mouseDownImage = Image.FromFile(Path.Combine(Application.StartupPath, mouseDownImagePath));
Image mouseOverImage = Image.FromFile(Path.Combine(Application.StartupPath, mouseOverImagePath));
2025-03-20 16:21:01 +08:00
// 設置滑鼠事件:點擊、進入、離開等,改變按鈕的背景圖片
2025-03-18 11:35:10 +08:00
phoneticButtonsForSingers[index].MouseDown += (s, e) => phoneticButtonsForSingers[index].BackgroundImage = mouseDownImage;
phoneticButtonsForSingers[index].MouseUp += (s, e) => phoneticButtonsForSingers[index].BackgroundImage = normalImage;
phoneticButtonsForSingers[index].MouseEnter += (s, e) => phoneticButtonsForSingers[index].BackgroundImage = mouseOverImage;
phoneticButtonsForSingers[index].MouseLeave += (s, e) => phoneticButtonsForSingers[index].BackgroundImage = normalImage;
2025-03-20 16:21:01 +08:00
// 設置點擊事件處理方法
2025-03-18 11:35:10 +08:00
phoneticButtonsForSingers[index].Click += PhoneticButton_Click;
2025-03-20 16:21:01 +08:00
// 設置按鈕的 Tag 屬性為對應的語音符號
2025-03-18 11:35:10 +08:00
phoneticButtonsForSingers[index].Tag = phoneticSymbols[index];
2025-03-20 16:21:01 +08:00
// 將按鈕添加到表單的控制項集合中
2025-03-18 11:35:10 +08:00
this.Controls.Add(phoneticButtonsForSingers[index]);
}
catch (Exception ex)
{
2025-03-20 16:21:01 +08:00
// 捕捉錯誤並輸出錯誤訊息
2025-03-18 11:35:10 +08:00
Console.WriteLine($"Error creating button at index {index}: {ex.Message}");
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化所有與注音歌手相關的按鈕,包括語音符號按鈕、特殊按鈕及輸入框。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeButtonsForZhuYinSingers()
{
2025-03-20 16:21:01 +08:00
// 從配置檔案加載注音符號並初始化按鈕
2025-03-18 11:35:10 +08:00
LoadPhoneticSymbolsFromConfig();
2025-03-20 16:21:01 +08:00
// 初始化所有語音按鈕
2025-03-18 11:35:10 +08:00
InitializePhoneticButtons();
2025-03-20 16:21:01 +08:00
// 初始化注音歌手的特殊按鈕(例如音量、搜尋等)
2025-03-18 11:35:10 +08:00
InitializeSpecialButtonsForZhuYinSingers();
2025-03-20 16:21:01 +08:00
// 初始化注音歌手的輸入框
2025-03-18 11:35:10 +08:00
InitializeInputBoxZhuYinSingers();
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 移除圖像周圍的白色邊框,將邊框的像素透明化。
/// </summary>
/// <param name="imagePath">待處理的圖像文件路徑。</param>
/// <returns>處理後的圖像,其中白色邊框已被去除並替換為透明。</returns>
2025-03-18 11:35:10 +08:00
private Image RemoveWhiteBorder(string imagePath)
{
2025-03-20 16:21:01 +08:00
// 創建一個 Bitmap 物件來加載圖像
2025-03-18 11:35:10 +08:00
Bitmap bmp = new Bitmap(imagePath);
2025-03-20 16:21:01 +08:00
// 定義圖像的矩形區域,這是我們將要操作的區域
2025-03-18 11:35:10 +08:00
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
2025-03-20 16:21:01 +08:00
// 鎖定圖像的位圖數據,以便進行直接修改
2025-03-18 11:35:10 +08:00
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
2025-03-20 16:21:01 +08:00
IntPtr ptr = bmpData.Scan0; // 獲取位圖數據的起始位置
int bytes = Math.Abs(bmpData.Stride) * bmp.Height; // 計算圖像的總字節數
byte[] rgbValues = new byte[bytes]; // 用來存儲圖像的像素數據
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // 從圖像數據中複製像素數據到 rgbValues 陣列
2025-03-18 11:35:10 +08:00
2025-03-20 16:21:01 +08:00
// 遍歷每個像素點,檢查是否為白色邊框
2025-03-18 11:35:10 +08:00
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
2025-03-20 16:21:01 +08:00
int position = (y * bmpData.Stride) + (x * 4); // 計算當前像素的位址
byte b = rgbValues[position]; // 藍色分量
byte g = rgbValues[position + 1]; // 綠色分量
byte r = rgbValues[position + 2]; // 紅色分量
byte a = rgbValues[position + 3]; // alpha 分量(透明度)
2025-03-18 11:35:10 +08:00
2025-03-20 16:21:01 +08:00
// 如果當前像素在圖像邊緣且為白色 (255, 255, 255),則將其設為透明
2025-03-18 11:35:10 +08:00
if ((x < 5 || x > bmp.Width - 5 || y < 5 || y > bmp.Height - 5) && r == 255 && g == 255 && b == 255)
{
2025-03-20 16:21:01 +08:00
// 將白色像素的 RGB 設置為 255, 255, 255 且 alpha 設為 0 (透明)
2025-03-18 11:35:10 +08:00
rgbValues[position] = 255;
rgbValues[position + 1] = 255;
rgbValues[position + 2] = 255;
2025-03-20 16:21:01 +08:00
rgbValues[position + 3] = 0; // 透明
2025-03-18 11:35:10 +08:00
}
}
}
2025-03-20 16:21:01 +08:00
// 將修改後的像素數據重新複製回位圖數據
2025-03-18 11:35:10 +08:00
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
2025-03-20 16:21:01 +08:00
// 解鎖圖像數據
2025-03-18 11:35:10 +08:00
bmp.UnlockBits(bmpData);
2025-03-20 16:21:01 +08:00
// 返回處理後的圖像
2025-03-18 11:35:10 +08:00
return bmp;
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化與注音歌手相關的特殊按鈕,包括修改、清除和關閉按鈕。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeSpecialButtonsForZhuYinSingers()
{
2025-03-20 16:21:01 +08:00
// 初始化修改按鈕
2025-03-18 11:35:10 +08:00
InitializeModifyButtonZhuYinSingers();
2025-03-20 16:21:01 +08:00
// 初始化清除按鈕
2025-03-18 11:35:10 +08:00
InitializeClearButtonZhuYinSingers();
2025-03-20 16:21:01 +08:00
// 初始化關閉按鈕
2025-03-18 11:35:10 +08:00
InitializeCloseButtonZhuYinSingers();
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化「修改」按鈕,設定按鈕的坐標、圖片和點擊事件。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeModifyButtonZhuYinSingers()
{
2025-03-20 16:21:01 +08:00
// 加載配置數據
2025-03-18 11:35:10 +08:00
var data = LoadConfigData();
2025-03-20 16:21:01 +08:00
// 讀取按鈕坐標
2025-03-18 11:35:10 +08:00
modifyButtonZhuYinCoords = LoadSpecialButtonCoordinates(data, "SpecialButtonCoordinates", "modifyButtonZhuYinSingers");
2025-03-20 16:21:01 +08:00
// 加載按鈕圖片(正常、鼠標懸停、鼠標按下)
2025-03-18 11:35:10 +08:00
var buttonImages = LoadButtonImages(data, "ModifyButtonImagesZhuYin");
2025-03-20 16:21:01 +08:00
// 創建「修改」按鈕,並設置坐標、圖片及點擊事件
2025-03-18 11:35:10 +08:00
modifyButtonZhuYinSingers = CreateSpecialButton(
2025-03-20 16:21:01 +08:00
"btnModifyZhuYinSingers", // 按鈕名稱
modifyButtonZhuYinCoords, // 按鈕坐標
buttonImages.normal, // 正常狀態圖片
buttonImages.mouseOver, // 鼠標懸停圖片
buttonImages.mouseDown, // 鼠標按下圖片
ModifyButtonZhuYinSingers_Click // 按鈕點擊事件
2025-03-18 11:35:10 +08:00
);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 處理「修改」按鈕的點擊事件,該事件會刪除輸入框中的最後一個字符。
/// </summary>
/// <param name="sender">觸發事件的對象</param>
/// <param name="e">事件參數</param>
2025-03-18 11:35:10 +08:00
private void ModifyButtonZhuYinSingers_Click(object sender, EventArgs e)
{
2025-03-20 16:21:01 +08:00
// 如果輸入框不為空,且包含輸入內容,則刪除最後一個字符
2025-03-18 11:35:10 +08:00
if (this.Controls.Contains(inputBoxZhuYinSingers) && inputBoxZhuYinSingers.Text.Length > 0)
{
inputBoxZhuYinSingers.Text = inputBoxZhuYinSingers.Text.Substring(0, inputBoxZhuYinSingers.Text.Length - 1);
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化「清除」按鈕,設定按鈕的坐標、圖片和點擊事件。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeClearButtonZhuYinSingers()
{
2025-03-20 16:21:01 +08:00
// 加載配置數據
2025-03-18 11:35:10 +08:00
var data = LoadConfigData();
2025-03-20 16:21:01 +08:00
// 讀取按鈕坐標
2025-03-18 11:35:10 +08:00
clearButtonZhuYinCoords = LoadSpecialButtonCoordinates(data, "SpecialButtonCoordinates", "clearButtonZhuYinSingers");
2025-03-20 16:21:01 +08:00
// 加載按鈕圖片(正常、鼠標懸停、鼠標按下)
2025-03-18 11:35:10 +08:00
var buttonImages = LoadButtonImages(data, "ClearButtonImagesZhuYin");
2025-03-20 16:21:01 +08:00
// 創建「清除」按鈕,並設置坐標、圖片及點擊事件
2025-03-18 11:35:10 +08:00
clearButtonZhuYinSingers = CreateSpecialButton(
2025-03-20 16:21:01 +08:00
"btnClearZhuYinSingers", // 按鈕名稱
clearButtonZhuYinCoords, // 按鈕坐標
buttonImages.normal, // 正常狀態圖片
buttonImages.mouseOver, // 鼠標懸停圖片
buttonImages.mouseDown, // 鼠標按下圖片
ClearButtonZhuYinSingers_Click // 按鈕點擊事件
2025-03-18 11:35:10 +08:00
);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 處理「清除」按鈕的點擊事件,該事件會清空輸入框中的所有文本。
/// </summary>
/// <param name="sender">觸發事件的對象</param>
/// <param name="e">事件參數</param>
2025-03-18 11:35:10 +08:00
private void ClearButtonZhuYinSingers_Click(object sender, EventArgs e)
2025-03-20 16:21:01 +08:00
{
// 如果輸入框不為空,則清空該框的文本內容
2025-03-18 11:35:10 +08:00
if (this.Controls.Contains(inputBoxZhuYinSingers) && inputBoxZhuYinSingers.Text.Length > 0)
{
inputBoxZhuYinSingers.Text = "";
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化「關閉」按鈕,設定按鈕的坐標、圖片和點擊事件。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeCloseButtonZhuYinSingers()
{
2025-03-20 16:21:01 +08:00
// 加載配置數據
2025-03-18 11:35:10 +08:00
var data = LoadConfigData();
2025-03-20 16:21:01 +08:00
// 讀取按鈕坐標
2025-03-18 11:35:10 +08:00
closeButtonZhuYinCoords = LoadSpecialButtonCoordinates(data, "SpecialButtonCoordinates", "closeButtonZhuYinSingers");
2025-03-20 16:21:01 +08:00
// 加載按鈕圖片(正常、鼠標懸停、鼠標按下)
2025-03-18 11:35:10 +08:00
var buttonImages = LoadButtonImages(data, "CloseButtonImagesZhuYin");
2025-03-20 16:21:01 +08:00
// 創建「關閉」按鈕,並設置坐標、圖片及點擊事件
2025-03-18 11:35:10 +08:00
closeButtonZhuYinSingers = CreateSpecialButton(
2025-03-20 16:21:01 +08:00
"btnCloseZhuYinSingers", // 按鈕名稱
closeButtonZhuYinCoords, // 按鈕坐標
buttonImages.normal, // 正常狀態圖片
buttonImages.mouseOver, // 鼠標懸停圖片
buttonImages.mouseDown, // 鼠標按下圖片
CloseButtonZhuYinSingers_Click // 按鈕點擊事件
2025-03-18 11:35:10 +08:00
);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 「關閉」按鈕的點擊事件處理方法。
/// 隱藏 ZhuYin 歌手圖片框以及與其相關的按鈕。
/// </summary>
/// <param name="sender">觸發事件的對象,這裡是關閉按鈕。</param>
/// <param name="e">事件參數。</param>
2025-03-18 11:35:10 +08:00
private void CloseButtonZhuYinSingers_Click(object sender, EventArgs e)
{
2025-03-20 16:21:01 +08:00
// 隱藏 ZhuYin 歌手圖片框
2025-03-18 11:35:10 +08:00
pictureBoxZhuYinSingers.Visible = false;
2025-03-28 09:21:32 +08:00
// 關閉注音搜尋的按鈕顏色
zhuyinSearchButton.BackgroundImage = zhuyinSearchNormalBackground;
2025-03-20 16:21:01 +08:00
// 隱藏與 ZhuYin 歌手相關的所有按鈕
2025-03-18 11:35:10 +08:00
SetZhuYinSingersAndButtonsVisibility(false);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 創建一個特殊的按鈕,並設定其顯示屬性、事件處理和位置。
/// </summary>
/// <param name="name">按鈕的名稱。</param>
/// <param name="coords">按鈕的坐標和大小,包含 X, Y, 寬度和高度。</param>
/// <param name="normalImagePath">按鈕正常狀態下的背景圖片路徑。</param>
/// <param name="mouseOverImagePath">鼠標懸停時按鈕的背景圖片路徑。</param>
/// <param name="mouseDownImagePath">鼠標點擊時按鈕的背景圖片路徑。</param>
/// <param name="clickEventHandler">按鈕的點擊事件處理程序。</param>
/// <returns>創建並返回的按鈕對象。</returns>
2025-03-18 11:35:10 +08:00
private Button CreateSpecialButton(string name, (int X, int Y, int Width, int Height) coords, string normalImagePath, string mouseOverImagePath, string mouseDownImagePath, EventHandler clickEventHandler)
{
2025-03-20 16:21:01 +08:00
// 創建按鈕並設定基本屬性
2025-03-18 11:35:10 +08:00
var button = new Button
{
Name = name,
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0, MouseDownBackColor = Color.Transparent, MouseOverBackColor = Color.Transparent },
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, normalImagePath))
};
2025-03-20 16:21:01 +08:00
// 設定按鈕的大小和位置
2025-03-18 11:35:10 +08:00
ResizeAndPositionButton(button, coords.X, coords.Y, coords.Width, coords.Height);
2025-03-20 16:21:01 +08:00
// 設定鼠標事件:進入、離開、按下、放開
2025-03-18 11:35:10 +08:00
button.MouseEnter += (sender, e) => button.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, mouseOverImagePath));
button.MouseLeave += (sender, e) => button.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, normalImagePath));
button.MouseDown += (sender, e) => button.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, mouseDownImagePath));
button.MouseUp += (sender, e) => button.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, normalImagePath));
2025-03-20 16:21:01 +08:00
// 註冊點擊事件處理
2025-03-18 11:35:10 +08:00
button.Click += clickEventHandler;
2025-03-20 16:21:01 +08:00
// 將按鈕添加到控件集合中
2025-03-18 11:35:10 +08:00
this.Controls.Add(button);
return button;
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 初始化 ZhuYin 歌手的輸入框,並設定其屬性與事件處理程序。
/// </summary>
2025-03-18 11:35:10 +08:00
private void InitializeInputBoxZhuYinSingers()
{
try
{
2025-03-20 16:21:01 +08:00
// 加載輸入框配置
2025-03-18 11:35:10 +08:00
LoadInputBoxConfig();
2025-03-20 16:21:01 +08:00
// 創建一個 RichTextBox 控件來作為輸入框
2025-03-18 11:35:10 +08:00
inputBoxZhuYinSingers = new RichTextBox
{
Name = "inputBoxZhuYinSingers",
2025-03-20 16:21:01 +08:00
ForeColor = inputBoxForeColor, // 設定文字顏色
Font = new Font(inputBoxFontName, inputBoxFontSize, inputBoxFontStyle), // 設定字體樣式
ScrollBars = RichTextBoxScrollBars.None // 不顯示滾動條
2025-03-18 11:35:10 +08:00
};
2025-03-20 16:21:01 +08:00
// 調整和定位輸入框的位置及大小
2025-03-18 11:35:10 +08:00
ResizeAndPositionControl(inputBoxZhuYinSingers, inputBoxZhuYinCoords.X, inputBoxZhuYinCoords.Y, inputBoxZhuYinCoords.Width, inputBoxZhuYinCoords.Height);
2025-03-20 16:21:01 +08:00
// 設定文本變更事件,當輸入框內容改變時觸發
2025-03-18 11:35:10 +08:00
inputBoxZhuYinSingers.TextChanged += (sender, e) =>
{
string searchText = inputBoxZhuYinSingers.Text;
2025-03-20 16:21:01 +08:00
2025-03-26 15:43:07 +08:00
// 使用注音符號開頭的歌手名稱進行搜索
var searchResults = allArtists.Where(artist => artist.Phonetic.StartsWith(searchText)).ToList();
// 使用注音符號包含的歌手名稱進行搜索
2025-03-18 11:35:10 +08:00
currentPage = 0;
2025-03-26 15:43:07 +08:00
currentArtistList = searchResults;
totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
2025-03-18 11:35:10 +08:00
multiPagePanel.currentPageIndex = 0;
2025-03-26 15:43:07 +08:00
multiPagePanel.LoadSingers(currentArtistList);
2025-03-18 11:35:10 +08:00
};
2025-03-20 16:21:01 +08:00
// 將輸入框加入到窗體的控件集合中
2025-03-18 11:35:10 +08:00
this.Controls.Add(inputBoxZhuYinSingers);
}
catch (Exception ex)
{
2025-03-20 16:21:01 +08:00
// 如果初始化過程中出現錯誤,則在控制台輸出錯誤信息
2025-03-18 11:35:10 +08:00
Console.WriteLine("Error initializing inputBoxZhuYinSingers: " + ex.Message);
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從配置文件 `config.ini` 中加載輸入框的設置,包括位置、大小、字體等屬性。
/// </summary>
2025-03-18 11:35:10 +08:00
private void LoadInputBoxConfig()
{
try
{
2025-03-20 16:21:01 +08:00
// 創建 INI 解析器
2025-03-18 11:35:10 +08:00
var parser = new FileIniDataParser();
2025-03-20 16:21:01 +08:00
string iniFilePath = "config.ini"; // 配置文件的路徑
2025-03-18 11:35:10 +08:00
IniData data;
2025-03-20 16:21:01 +08:00
// 打開並讀取配置文件
2025-03-18 11:35:10 +08:00
using (var reader = new StreamReader(iniFilePath, Encoding.UTF8))
{
data = parser.ReadData(reader);
}
2025-03-20 16:21:01 +08:00
// 從配置中加載輸入框的坐標和大小
2025-03-18 11:35:10 +08:00
inputBoxZhuYinCoords = (
2025-03-20 16:21:01 +08:00
int.Parse(data["InputBoxZhuYinSingers"]["X"]), // 輸入框的 X 坐標
int.Parse(data["InputBoxZhuYinSingers"]["Y"]), // 輸入框的 Y 坐標
int.Parse(data["InputBoxZhuYinSingers"]["Width"]), // 輸入框的寬度
int.Parse(data["InputBoxZhuYinSingers"]["Height"]) // 輸入框的高度
2025-03-18 11:35:10 +08:00
);
2025-03-20 16:21:01 +08:00
// 從配置中加載字體屬性
inputBoxFontName = data["InputBoxZhuYinSingers"]["FontName"]; // 字體名稱
inputBoxFontSize = float.Parse(data["InputBoxZhuYinSingers"]["FontSize"]); // 字體大小
inputBoxFontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), data["InputBoxZhuYinSingers"]["FontStyle"]); // 字體樣式
inputBoxForeColor = Color.FromName(data["InputBoxZhuYinSingers"]["ForeColor"]); // 字體顏色
2025-03-18 11:35:10 +08:00
}
catch (Exception ex)
{
2025-03-20 16:21:01 +08:00
// 若發生錯誤,顯示錯誤信息
2025-03-18 11:35:10 +08:00
Console.WriteLine("Error loading inputBox configuration: " + ex.Message);
}
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 存儲 `pictureBoxZhuYinSingers` 控制項的坐標和大小設置。
/// </summary>
/// <remarks>
/// 這個元組包含了 `X`、`Y` 坐標以及 `Width`、`Height` 大小,用於配置 `pictureBoxZhuYinSingers` 的位置和大小。
/// </remarks>
2025-03-18 11:35:10 +08:00
private (int X, int Y, int Width, int Height) pictureBoxZhuYinSingerCoords;
2025-03-20 16:21:01 +08:00
/// <summary>
/// 從配置檔案中讀取 `PictureBoxZhuYinSingers` 控制項的坐標和大小設置。
/// </summary>
2025-03-18 11:35:10 +08:00
private void LoadPictureBoxZhuYinSingerCoordsFromConfig()
{
var parser = new FileIniDataParser();
IniData data = parser.ReadFile("config.ini");
var coords = data["PictureBoxZhuYinSingers"];
pictureBoxZhuYinSingerCoords = (
int.Parse(coords["X"]),
int.Parse(coords["Y"]),
int.Parse(coords["Width"]),
int.Parse(coords["Height"])
);
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 顯示圖片並根據配置文件設置顯示區域的大小和位置。
/// </summary>
/// <param name="imagePath">圖片的路徑。</param>
2025-03-18 11:35:10 +08:00
private void ShowImageOnPictureBoxZhuYinSingers(string imagePath)
{
2025-03-20 16:21:01 +08:00
// 讀取配置文件中的顯示區域設置
2025-03-18 11:35:10 +08:00
LoadPictureBoxZhuYinSingerCoordsFromConfig();
2025-03-20 16:21:01 +08:00
// 加載原始圖片
2025-03-18 11:35:10 +08:00
Bitmap originalImage = new Bitmap(imagePath);
2025-03-20 16:21:01 +08:00
// 創建顯示區域,根據配置文件中的坐標和大小設置
2025-03-18 11:35:10 +08:00
Rectangle displayArea = new Rectangle(pictureBoxZhuYinSingerCoords.X, pictureBoxZhuYinSingerCoords.Y, pictureBoxZhuYinSingerCoords.Width, pictureBoxZhuYinSingerCoords.Height);
2025-03-20 16:21:01 +08:00
// 設置圖片到 PictureBox
2025-03-18 11:35:10 +08:00
pictureBoxZhuYinSingers.Image = originalImage;
2025-03-20 16:21:01 +08:00
// 調整 PictureBox 大小和位置
2025-03-18 11:35:10 +08:00
ResizeAndPositionPictureBox(pictureBoxZhuYinSingers, displayArea.X, displayArea.Y, displayArea.Width, displayArea.Height);
2025-03-20 16:21:01 +08:00
// 顯示圖片
2025-03-18 11:35:10 +08:00
pictureBoxZhuYinSingers.Visible = true;
}
2025-03-20 16:21:01 +08:00
/// <summary>
/// 設置注音歌手相關控制項(包括圖片框、按鈕和輸入框)的顯示或隱藏狀態。
/// </summary>
/// <param name="isVisible">指定控件是否可見。True 為顯示False 為隱藏。</param>
2025-03-18 11:35:10 +08:00
private void SetZhuYinSingersAndButtonsVisibility(bool isVisible)
{
2025-03-20 16:21:01 +08:00
// 定義一個動作來處理控制項的顯示或隱藏
2025-03-18 11:35:10 +08:00
System.Action action = () =>
{
try
{
2025-03-20 16:21:01 +08:00
// 暫停控制項佈局的重新排版,提高效率
2025-03-18 11:35:10 +08:00
SuspendLayout();
2025-03-20 16:21:01 +08:00
// 檢查並設置圖片框的可見性
2025-03-18 11:35:10 +08:00
if (pictureBoxZhuYinSingers == null)
{
Console.WriteLine("pictureBoxZhuYinSingers is null");
}
else
{
pictureBoxZhuYinSingers.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) pictureBoxZhuYinSingers.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 檢查並設置拼音按鈕的可見性
2025-03-18 11:35:10 +08:00
if (phoneticButtonsForSingers == null)
{
Console.WriteLine("phoneticButtonsForSingers is null");
}
else
{
foreach (var button in phoneticButtonsForSingers)
{
if (button == null)
{
Console.WriteLine("One of the phoneticButtonsForSingers is null");
}
else
{
button.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) button.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
}
}
2025-03-20 16:21:01 +08:00
// 檢查並設置修改按鈕的可見性
2025-03-18 11:35:10 +08:00
if (modifyButtonZhuYinSingers == null)
{
Console.WriteLine("modifyButtonZhuYinSingers is null");
}
else
{
modifyButtonZhuYinSingers.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) modifyButtonZhuYinSingers.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 檢查並設置清除按鈕的可見性
2025-03-18 11:35:10 +08:00
if (clearButtonZhuYinSingers == null)
{
Console.WriteLine("clearButtonZhuYinSingers is null");
}
else
{
clearButtonZhuYinSingers.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) clearButtonZhuYinSingers.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 檢查並設置關閉按鈕的可見性
2025-03-18 11:35:10 +08:00
if (closeButtonZhuYinSingers == null)
{
Console.WriteLine("closeButtonZhuYinSingers is null");
}
else
{
closeButtonZhuYinSingers.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) closeButtonZhuYinSingers.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 檢查並設置輸入框的可見性
2025-03-18 11:35:10 +08:00
if (inputBoxZhuYinSingers == null)
{
Console.WriteLine("inputBoxZhuYinSingers is null");
}
else
{
inputBoxZhuYinSingers.Visible = isVisible;
2025-03-20 16:21:01 +08:00
if (isVisible) inputBoxZhuYinSingers.BringToFront(); // 如果顯示,將其置於最前
2025-03-18 11:35:10 +08:00
}
2025-03-20 16:21:01 +08:00
// 恢復控制項的佈局重新排版
2025-03-18 11:35:10 +08:00
ResumeLayout();
PerformLayout();
2025-03-20 16:21:01 +08:00
// 刷新所有控制項的顯示
2025-03-18 11:35:10 +08:00
pictureBoxZhuYinSingers?.Refresh();
if (phoneticButtonsForSingers != null)
{
foreach (var button in phoneticButtonsForSingers)
{
2025-03-20 16:21:01 +08:00
button?.Refresh(); // 刷新每個按鈕
2025-03-18 11:35:10 +08:00
}
}
modifyButtonZhuYinSingers?.Refresh();
clearButtonZhuYinSingers?.Refresh();
closeButtonZhuYinSingers?.Refresh();
inputBoxZhuYinSingers?.Refresh();
}
catch (Exception ex)
{
Console.WriteLine("Error in SetZhuYinSingersAndButtonsVisibility: " + ex.Message);
}
};
2025-03-20 16:21:01 +08:00
// 檢查是否需要在主執行緒外執行
2025-03-18 11:35:10 +08:00
if (this.InvokeRequired)
{
2025-03-20 16:21:01 +08:00
this.Invoke(action); // 如果需要,透過主執行緒執行
2025-03-18 11:35:10 +08:00
}
else
{
2025-03-20 16:21:01 +08:00
action(); // 否則直接執行
2025-03-18 11:35:10 +08:00
}
}
2025-03-20 16:21:01 +08:00
2025-03-18 11:35:10 +08:00
}
}