303 lines
13 KiB
C#
303 lines
13 KiB
C#
using System.IO;
|
|
|
|
namespace DualScreenDemo
|
|
{
|
|
public partial class PrimaryForm
|
|
{
|
|
// 注音歌曲的 PictureBox
|
|
private PictureBox pictureBoxZhuYinSongs;
|
|
//存放注音按鈕的陣列
|
|
private Button[] phoneticButtonsForSongs;
|
|
//特殊功能按鈕(修改、清除、關閉)
|
|
private string[] ButtonsPhoneticSymbols = ["ㄅ","ㄉ","ㄍ","ㄐ","ㄓ","ㄗ","ㄛ","ㄡ","ㄤ","ㄧ",
|
|
"ㄆ","ㄊ","ㄎ","ㄑ","ㄔ","ㄘ","ㄜ","ㄢ","ㄦ","ㄨ",
|
|
"ㄇ","ㄋ","ㄏ","ㄒ","ㄕ","ㄙ","ㄞ","ㄣ","ㄩ",
|
|
"ㄈ","ㄌ","","ㄖ","ㄚ","ㄠ"];
|
|
private Button modifyButtonZhuYinSongs;
|
|
private Button clearButtonZhuYinSongs;
|
|
private Button closeButtonZhuYinSongs;
|
|
//用於顯示輸入文字的輸入框
|
|
private RichTextBox inputBoxZhuYinSongs;
|
|
|
|
private void InitializeButtonsForZhuYinSongs()
|
|
{
|
|
|
|
// 4. 初始化輸入框 (用於顯示使用者輸入的注音符號)
|
|
InitializeInputBoxZhuYinSongs();
|
|
InitializeBopomofoSongsButton();
|
|
InitializePhoneticSymbolBtns(pictureBoxZhuYinSongs,PhoneticButton_Click);
|
|
}
|
|
|
|
private void InitializeBopomofoSongsButton()
|
|
{
|
|
var data = LoadBtnConfigData();
|
|
|
|
modifyButtonZhuYinSongs = new Button { Name = "modifyButtonZhuYinSongs" };
|
|
ConfigureButton(modifyButtonZhuYinSongs, 650, 275, 72, 67,
|
|
new Bitmap(Path.Combine(serverPath, data["ModifyButtonImagesZhuYin"]["normal"])),
|
|
new Bitmap(Path.Combine(serverPath, data["ModifyButtonImagesZhuYin"]["mouseOver"])),
|
|
new Bitmap(Path.Combine(serverPath, data["ModifyButtonImagesZhuYin"]["mouseDown"])),
|
|
ModifyButtonZhuYinSongs_Click);
|
|
|
|
pictureBoxZhuYinSongs.Controls.Add(modifyButtonZhuYinSongs);
|
|
|
|
|
|
clearButtonZhuYinSongs = new Button { Name = "clearButtonZhuYinSongs" };
|
|
ConfigureButton(clearButtonZhuYinSongs, 8, 275, 72, 67,
|
|
new Bitmap(Path.Combine(serverPath, data["ClearButtonImagesZhuYin"]["normal"])),
|
|
new Bitmap(Path.Combine(serverPath, data["ClearButtonImagesZhuYin"]["mouseOver"])),
|
|
new Bitmap(Path.Combine(serverPath, data["ClearButtonImagesZhuYin"]["mouseDown"])),
|
|
ClearButtonZhuYinSongs_Click);
|
|
|
|
pictureBoxZhuYinSongs.Controls.Add(clearButtonZhuYinSongs);
|
|
|
|
|
|
closeButtonZhuYinSongs = new Button { Name = "closeButtonZhuYinSongs" };
|
|
ConfigureButton(closeButtonZhuYinSongs, 730, 275, 72, 67,
|
|
new Bitmap(Path.Combine(serverPath, data["CloseButtonImagesZhuYin"]["normal"])),
|
|
new Bitmap(Path.Combine(serverPath, data["CloseButtonImagesZhuYin"]["mouseOver"])),
|
|
new Bitmap(Path.Combine(serverPath, data["CloseButtonImagesZhuYin"]["mouseDown"])),
|
|
CloseButtonZhuYinSongs_Click);
|
|
|
|
pictureBoxZhuYinSongs.Controls.Add(closeButtonZhuYinSongs);
|
|
}
|
|
|
|
|
|
private void InitializePhoneticSymbolBtns(Control control, EventHandler handler)
|
|
{
|
|
phoneticButtonsForSongs = new Button[35];
|
|
|
|
// 設置上排按鈕
|
|
int x = 8;
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
CreatePhoneticSymbolBtns(i, x, 65, control, handler);
|
|
x += 80;
|
|
}
|
|
|
|
x = 8;
|
|
for (int i = 10; i < 20; i++)
|
|
{
|
|
CreatePhoneticSymbolBtns(i, x, 135, control, handler);
|
|
x += 80;
|
|
}
|
|
|
|
x = 40;
|
|
for (int i = 20; i < 29; i++)
|
|
{
|
|
CreatePhoneticSymbolBtns(i, x, 205, control, handler);
|
|
x += 80;
|
|
}
|
|
// 設置下排按鈕
|
|
x = 88;
|
|
for (int i = 29; i < 32; i++)
|
|
{
|
|
CreatePhoneticSymbolBtns(i, x, 275, control, handler);
|
|
x += 80;
|
|
}
|
|
x += 80;
|
|
for (int i = 32; i < 35; i++)
|
|
{
|
|
CreatePhoneticSymbolBtns(i, x, 275, control, handler);
|
|
x += 80;
|
|
}
|
|
}
|
|
|
|
|
|
private void CreatePhoneticSymbolBtns(int index, int x, int y, Control control, EventHandler handler)
|
|
{
|
|
try
|
|
{
|
|
// 加載配置數據
|
|
var data = LoadBtnConfigData();
|
|
// 創建語音按鈕並設置其屬性
|
|
phoneticButtonsForSongs[index] = new Button
|
|
{
|
|
Name = $"phoneticButtonsForSongs_{ButtonsPhoneticSymbols[index]}", // 按鈕名稱設為語音符號名稱
|
|
};
|
|
if (index == 31)
|
|
{
|
|
ConfigureButton(phoneticButtonsForSongs[index], x, y, 154, 67,
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_normal"])),
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_mouseOver"])),
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_mouseDown"])),
|
|
handler);
|
|
}
|
|
else
|
|
{
|
|
ConfigureButton(phoneticButtonsForSongs[index], x, y, 72, 67,
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_normal"])),
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_mouseOver"])),
|
|
new Bitmap(Path.Combine(serverPath, data["PhoneticButtonImages"][$"button{index}_mouseDown"])),
|
|
handler);
|
|
}
|
|
|
|
|
|
|
|
// 設置按鈕的 Tag 屬性為對應的語音符號
|
|
phoneticButtonsForSongs[index].Tag = ButtonsPhoneticSymbols[index];
|
|
|
|
// 將按鈕添加到表單的控制項集合中
|
|
control.Controls.Add(phoneticButtonsForSongs[index]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error creating button at index {index}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
// 初始化注音輸入框 (RichTextBox),設定外觀、事件處理及位置大小
|
|
private void InitializeInputBoxZhuYinSongs()
|
|
{
|
|
try
|
|
{
|
|
string fontName = "Times New Roman";
|
|
float fontSize = 26;
|
|
FontStyle fontStyle = FontStyle.Regular;
|
|
Color foreColor = Color.Black;
|
|
|
|
inputBoxZhuYinSongs = new RichTextBox
|
|
{
|
|
Name = "inputBoxZhuYinSongs",
|
|
ForeColor = foreColor, // 設定文字顏色
|
|
Font = new Font(fontName, fontSize / 900 * Screen.PrimaryScreen.Bounds.Height, fontStyle),
|
|
ScrollBars = RichTextBoxScrollBars.None // 禁用滾動條
|
|
};
|
|
|
|
// 調整輸入框大小與位置
|
|
ResizeAndPositionControl(inputBoxZhuYinSongs, 10, 12, 455, 47);
|
|
|
|
// 將輸入框加入到 UI 控制項
|
|
pictureBoxZhuYinSongs.Controls.Add(inputBoxZhuYinSongs);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error initializing inputBoxZhuYinSongs: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
// 在 pictureBoxZhuYinSongs 上顯示指定路徑的圖片,並根據設定調整其大小與位置。
|
|
private void ShowImageOnPictureBoxZhuYinSongs(string imagePath)
|
|
{
|
|
// 讀取圖片檔案並載入 Bitmap 物件
|
|
Bitmap originalImage = new Bitmap(imagePath);
|
|
|
|
// 設定 PictureBox 的圖片
|
|
pictureBoxZhuYinSongs.Image = originalImage;
|
|
|
|
// 調整 PictureBox 的大小與位置,使其符合設定
|
|
ResizeAndPositionPictureBox(pictureBoxZhuYinSongs,390, 360, 808, 356);
|
|
|
|
// 顯示 PictureBox
|
|
pictureBoxZhuYinSongs.Visible = true;
|
|
}
|
|
|
|
|
|
// 設定注音歌曲相關的 PictureBox、按鈕和輸入框的可見性。
|
|
private void SetZhuYinSongsAndButtonsVisibility(bool isVisible)
|
|
{
|
|
// 定義要執行的操作,設定各控件的可見性
|
|
System.Action action = () =>
|
|
{
|
|
try
|
|
{
|
|
// 暫停佈局邏輯,防止在調整控件可見性時觸發不必要的佈局計算,提升性能
|
|
SuspendLayout();
|
|
if (isVisible) SetUIVisible(pictureBoxZhuYinSongs);
|
|
else CloseUI(pictureBoxZhuYinSongs);
|
|
ResumeLayout();
|
|
PerformLayout();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 捕捉並輸出異常資訊,方便除錯
|
|
Console.WriteLine("Error in SetZhuYinSongsAndButtonsVisibility: " + ex.Message);
|
|
}
|
|
};
|
|
|
|
// 如果當前執行緒需要呼叫 Invoke 方法才能修改控件,則使用 Invoke
|
|
if (this.InvokeRequired)
|
|
{
|
|
this.Invoke(action);
|
|
}
|
|
else
|
|
{
|
|
action();
|
|
}
|
|
}
|
|
|
|
private void ZhuyinSearchSongsButton_Click(object sender, EventArgs e)
|
|
{
|
|
//更新搜尋模式按鈕的背景圖
|
|
|
|
UpdateSongSearchBtn(zhuyinSearchSongButton, zhuyinSearchSongActiveBackground);
|
|
// 讀取 config.ini 並獲取注音圖片的路徑
|
|
var configData = LoadBtnConfigData();
|
|
string imagePath = Path.Combine(serverPath, configData["ImagePaths"]["ZhuYinSongs"]);
|
|
//顯示注音歌曲圖片
|
|
ShowImageOnPictureBoxZhuYinSongs(Path.Combine(serverPath, imagePath));
|
|
|
|
|
|
//設定不同模式的UI顯示
|
|
SetWordCountSongsAndButtonsVisibility(false); // 隱藏字數搜尋相關控件
|
|
SetEnglishSongsAndButtonsVisibility(false);
|
|
SetPinYinSongsAndButtonsVisibility(false);
|
|
SetHandWritingForSongsAndButtonsVisibility(false);
|
|
SetSongIDSearchAndButtonsVisibility(false);
|
|
SetZhuYinSongsAndButtonsVisibility(true);
|
|
ResetinputBox();
|
|
pictureBoxZhuYinSongs.Visible = true;
|
|
}
|
|
|
|
private void ModifyButtonZhuYinSongs_Click(object sender, EventArgs e)
|
|
{
|
|
// 1. 確保輸入框 (inputBoxZhuYinSongs) 存在於目前的視窗控制項中
|
|
if (pictureBoxZhuYinSongs.Controls.Contains(inputBoxZhuYinSongs) && inputBoxZhuYinSongs.Text.Length > 0)
|
|
{
|
|
// 2. 刪除輸入框中的最後一個字元 (即移除最後一個注音符號)
|
|
inputBoxZhuYinSongs.Text = inputBoxZhuYinSongs.Text.Substring(0, inputBoxZhuYinSongs.Text.Length - 1);
|
|
}
|
|
}
|
|
|
|
private void ClearButtonZhuYinSongs_Click(object sender, EventArgs e)
|
|
{
|
|
// 1. 確保視窗內包含「注音輸入框」(inputBoxZhuYinSongs),且輸入框內有文字
|
|
if (pictureBoxZhuYinSongs.Controls.Contains(inputBoxZhuYinSongs) && inputBoxZhuYinSongs.Text.Length > 0)
|
|
{
|
|
// 2. 清空輸入框內容
|
|
inputBoxZhuYinSongs.Text = "";
|
|
}
|
|
}
|
|
|
|
private void CloseButtonZhuYinSongs_Click(object sender, EventArgs e)
|
|
{
|
|
// 隱藏注音輸入的圖片
|
|
pictureBoxZhuYinSongs.Visible = false;
|
|
zhuyinSearchSongButton.BackgroundImage = zhuyinSearchSongNormalBackground;
|
|
// 隱藏注音輸入的所有按鈕與介面元素
|
|
SetZhuYinSongsAndButtonsVisibility(false);
|
|
FindZhuYiSongs(); // 查詢歌曲
|
|
}
|
|
|
|
private void FindZhuYiSongs()
|
|
{
|
|
string searchText = inputBoxZhuYinSongs.Text;
|
|
// 在這裡添加搜尋歌曲的邏輯
|
|
// 例如:根據輸入框的內容搜尋歌曲
|
|
|
|
string query = string.IsNullOrWhiteSpace(searchText)
|
|
? "SELECT * FROM song_library_cache ORDER BY `song_id` DESC LIMIT 1000;"
|
|
: $"SELECT * FROM song_library_cache WHERE `phonetic_abbr` LIKE '{searchText}%' ORDER BY `song_name`;";
|
|
|
|
var searchResults = SearchSongs_Mysql(query);
|
|
// 重置分頁
|
|
currentPage = 0;
|
|
totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
|
|
// 更新多頁面面板的內容
|
|
multiPagePanel.currentPageIndex = 0;
|
|
multiPagePanel.LoadSongs(searchResults);
|
|
}
|
|
|
|
}
|
|
} |