254 lines
9.3 KiB
C#
254 lines
9.3 KiB
C#
using System;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Windows.Forms;
|
||
using NAudio.Wave;
|
||
using WMPLib;
|
||
using System.Collections.Generic;
|
||
|
||
namespace DualScreenDemo
|
||
{
|
||
public partial class PrimaryForm : Form
|
||
{
|
||
private WindowsMediaPlayer mediaPlayer;
|
||
private IWavePlayer waveOut;
|
||
private AudioFileReader audioFileReader;
|
||
private PictureBox pictureBoxSceneSoundEffects;
|
||
private Button constructionButton;
|
||
private Button marketButton;
|
||
private Button drivingButton;
|
||
private Button airportButton;
|
||
private Button officeButton;
|
||
private Button closeButton;
|
||
|
||
private void InitializeMediaPlayer()
|
||
{
|
||
mediaPlayer = new WindowsMediaPlayer();
|
||
}
|
||
private void ConfigureImageButton(Button button, int posX, int posY, int width, int height,
|
||
string imagePath, EventHandler clickEventHandler)
|
||
{
|
||
// 你的基準解析度(設計時以這個為標準)
|
||
const float baseWidth = 1920f;
|
||
const float baseHeight = 1080f;
|
||
|
||
// 實際螢幕解析度
|
||
float actualWidth = Screen.PrimaryScreen.Bounds.Width;
|
||
float actualHeight = Screen.PrimaryScreen.Bounds.Height;
|
||
|
||
// 計算縮放比例
|
||
float scaleX = actualWidth / baseWidth;
|
||
float scaleY = actualHeight / baseHeight;
|
||
|
||
// 套用縮放比例到位置與大小
|
||
int scaledX = (int)(posX * scaleX);
|
||
int scaledY = (int)(posY * scaleY);
|
||
int scaledWidth = (int)(width * scaleX);
|
||
int scaledHeight = (int)(height * scaleY);
|
||
|
||
// 載入圖片並調整按鈕尺寸
|
||
Bitmap image = new Bitmap(imagePath);
|
||
button.SetBounds(scaledX, scaledY, scaledWidth, scaledHeight);
|
||
|
||
button.BackgroundImage = image;
|
||
button.BackgroundImageLayout = ImageLayout.Stretch;
|
||
|
||
// 按鈕樣式
|
||
button.FlatStyle = FlatStyle.Flat;
|
||
button.FlatAppearance.BorderSize = 0;
|
||
button.FlatAppearance.MouseDownBackColor = Color.Transparent;
|
||
button.FlatAppearance.MouseOverBackColor = Color.Transparent;
|
||
|
||
// 點擊事件
|
||
if (clickEventHandler != null)
|
||
button.Click += clickEventHandler;
|
||
|
||
this.Controls.Add(button);
|
||
}
|
||
|
||
private void InitializeSoundEffectButtons()
|
||
{
|
||
|
||
constructionButton = new Button
|
||
{
|
||
Name = "constructionButton",
|
||
};
|
||
string path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_工地.png");
|
||
ConfigureImageButton(constructionButton, 1183, 634, 148, 64,
|
||
path, ConstructionButton_Click);
|
||
this.Controls.Add(constructionButton);
|
||
|
||
|
||
marketButton = new Button
|
||
{
|
||
Name = "marketButton",
|
||
};
|
||
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_市場.png");
|
||
ConfigureImageButton(marketButton, 1394, 634, 148, 63,
|
||
path, MarketButton_Click);
|
||
this.Controls.Add(marketButton);
|
||
|
||
|
||
drivingButton = new Button
|
||
{
|
||
Name = "drivingButton",
|
||
};
|
||
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_開車.png");
|
||
ConfigureImageButton(drivingButton, 1183, 720, 148, 63,
|
||
path, DrivingButton_Click);
|
||
this.Controls.Add(drivingButton);
|
||
|
||
|
||
airportButton = new Button
|
||
{
|
||
Name = "airportButton",
|
||
};
|
||
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_機場.png");
|
||
ConfigureImageButton(airportButton, 1394, 720, 148, 63,
|
||
path, AirportButton_Click);
|
||
this.Controls.Add(airportButton);
|
||
|
||
|
||
officeButton = new Button
|
||
{
|
||
Name = "officeButton",
|
||
};
|
||
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_辦公室.png");
|
||
ConfigureImageButton(officeButton, 1183, 806, 148, 64,
|
||
path, OfficeButton_Click);
|
||
this.Controls.Add(officeButton);
|
||
|
||
|
||
closeButton = new Button
|
||
{
|
||
Name = "closeButton",
|
||
};
|
||
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_關閉.png");
|
||
ConfigureImageButton(closeButton, 1394, 806, 150, 63,
|
||
path, CloseButton_Click);
|
||
this.Controls.Add(closeButton);
|
||
}
|
||
|
||
private void SoundEffectButton_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
SetHotSongButtonsVisibility(false);
|
||
SetNewSongButtonsVisibility(false);
|
||
SetSingerSearchButtonsVisibility(false);
|
||
SetSongSearchButtonsVisibility(false);
|
||
|
||
if (!pictureBoxSceneSoundEffects.Visible)
|
||
{
|
||
ShowImageOnPictureBoxSceneSoundEffects(Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效.png"));
|
||
SetPictureBoxSceneSoundEffectsAndButtonsVisibility(true);
|
||
}
|
||
else
|
||
{
|
||
TogglePictureBoxSceneSoundEffectsButtonsVisibility();
|
||
}
|
||
}
|
||
|
||
private void ConstructionButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\1857.mp3");
|
||
private void MarketButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\13472_Audio Trimmer.mp3");
|
||
private void DrivingButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\kc1.mp3");
|
||
private void AirportButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\xm2401.m4a");
|
||
private void OfficeButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\y1640.m4a");
|
||
private void CloseButton_Click(object sender, EventArgs e) => TogglePictureBoxSceneSoundEffectsButtonsVisibility();
|
||
|
||
private void PlaySound(string filePath)
|
||
{
|
||
waveOut?.Dispose();
|
||
audioFileReader?.Dispose();
|
||
|
||
waveOut = new WaveOutEvent();
|
||
audioFileReader = new AudioFileReader(Path.Combine(Application.StartupPath, filePath));
|
||
waveOut.Init(audioFileReader);
|
||
waveOut.Play();
|
||
}
|
||
|
||
private void PlayMediaSound(string filePath)
|
||
{
|
||
mediaPlayer.URL = Path.Combine(Application.StartupPath, filePath);
|
||
mediaPlayer.controls.play();
|
||
}
|
||
|
||
public void PlayApplauseSound()
|
||
{
|
||
mediaPlayer.URL = Path.Combine(Application.StartupPath,"sounds" ,"zs.m4a");
|
||
mediaPlayer.controls.play();
|
||
}
|
||
// 按鈕位置需要更改,底圖需要更改
|
||
private void ShowImageOnPictureBoxSceneSoundEffects(string imagePath)
|
||
{
|
||
|
||
if (File.Exists(imagePath))
|
||
{
|
||
try
|
||
{
|
||
// 使用 Image.FromFile 載入後 clone,避免檔案 lock & GDI 錯誤
|
||
using (Image original = Image.FromFile(imagePath))
|
||
{
|
||
Bitmap image = new Bitmap(original);
|
||
pictureBoxSceneSoundEffects.Image = image;
|
||
}
|
||
|
||
ResizeAndPositionPictureBox(pictureBoxSceneSoundEffects, 850, 450,
|
||
pictureBoxSceneSoundEffects.Image.Width, pictureBoxSceneSoundEffects.Image.Height);
|
||
|
||
pictureBoxSceneSoundEffects.Visible = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("圖片載入失敗:" + ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("圖片檔案不存在:" + imagePath, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
|
||
private void TogglePictureBoxSceneSoundEffectsButtonsVisibility()
|
||
{
|
||
|
||
bool areButtonsVisible = pictureBoxSceneSoundEffects.Visible;
|
||
|
||
|
||
SetPictureBoxSceneSoundEffectsAndButtonsVisibility(!areButtonsVisible);
|
||
}
|
||
|
||
|
||
private void SetPictureBoxSceneSoundEffectsAndButtonsVisibility(bool isVisible)
|
||
{
|
||
|
||
pictureBoxSceneSoundEffects.Visible = isVisible;
|
||
|
||
if (isVisible)
|
||
{
|
||
|
||
pictureBoxSceneSoundEffects.BringToFront();
|
||
}
|
||
|
||
|
||
List<Button> soundEffectButtons = new List<Button>
|
||
{
|
||
constructionButton,
|
||
marketButton,
|
||
drivingButton,
|
||
airportButton,
|
||
officeButton,
|
||
closeButton
|
||
};
|
||
|
||
|
||
foreach (Button button in soundEffectButtons)
|
||
{
|
||
button.Visible = isVisible;
|
||
if (isVisible)
|
||
{
|
||
button.BringToFront();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |