superstar_v2/PrimaryFormParts/PrimaryForm.SoundEffects.cs

228 lines
8.1 KiB
C#
Raw Normal View History

2025-06-20 13:10:55 +08:00
using System;
using System.Drawing;
2025-04-07 16:54:10 +08:00
using System.IO;
2025-06-20 13:10:55 +08:00
using System.Windows.Forms;
2025-04-07 16:54:10 +08:00
using NAudio.Wave;
using WMPLib;
2025-06-20 13:10:55 +08:00
using System.Collections.Generic;
2025-04-07 16:54:10 +08:00
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();
}
2025-06-20 13:10:55 +08:00
private void ConfigureImageButton(Button button, int posX, int posY, int width, int height,
string imagePath, EventHandler clickEventHandler)
{
Bitmap image = new Bitmap(imagePath);
button.SetBounds(posX, posY, image.Width, image.Height);
// 載入圖片
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);
}
2025-04-07 16:54:10 +08:00
private void InitializeSoundEffectButtons()
{
constructionButton = new Button
{
Name = "constructionButton",
};
2025-06-20 13:10:55 +08:00
string path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_工地.png");
ConfigureImageButton(constructionButton, 1183, 634, 148, 64,
path, ConstructionButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(constructionButton);
marketButton = new Button
{
Name = "marketButton",
};
2025-06-20 13:10:55 +08:00
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_市場.png");
ConfigureImageButton(marketButton, 1394, 634, 148, 63,
path, MarketButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(marketButton);
drivingButton = new Button
{
Name = "drivingButton",
};
2025-06-20 13:10:55 +08:00
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_開車.png");
ConfigureImageButton(drivingButton, 1183, 720, 148, 63,
path, DrivingButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(drivingButton);
airportButton = new Button
{
Name = "airportButton",
};
2025-06-20 13:10:55 +08:00
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_機場.png");
ConfigureImageButton(airportButton, 1394, 720, 148, 63,
path, AirportButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(airportButton);
officeButton = new Button
{
Name = "officeButton",
};
2025-06-20 13:10:55 +08:00
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_辦公室.png");
ConfigureImageButton(officeButton, 1183, 806, 148, 64,
path, OfficeButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(officeButton);
closeButton = new Button
{
Name = "closeButton",
};
2025-06-20 13:10:55 +08:00
path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_關閉.png");
ConfigureImageButton(closeButton, 1394, 806, 150, 63,
path, CloseButton_Click);
2025-04-07 16:54:10 +08:00
this.Controls.Add(closeButton);
}
private void SoundEffectButton_Click(object sender, EventArgs e)
{
SetHotSongButtonsVisibility(false);
SetNewSongButtonsVisibility(false);
SetSingerSearchButtonsVisibility(false);
SetSongSearchButtonsVisibility(false);
if (!pictureBoxSceneSoundEffects.Visible)
{
2025-06-20 13:10:55 +08:00
ShowImageOnPictureBoxSceneSoundEffects(Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效.png"));
2025-04-07 16:54:10 +08:00
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()
{
2025-06-20 13:10:55 +08:00
mediaPlayer.URL = Path.Combine(Application.StartupPath,"sounds" ,"zs.m4a");
2025-04-07 16:54:10 +08:00
mediaPlayer.controls.play();
}
2025-06-20 13:10:55 +08:00
// 按鈕位置需要更改,底圖需要更改
2025-04-07 16:54:10 +08:00
private void ShowImageOnPictureBoxSceneSoundEffects(string imagePath)
{
2025-06-20 13:10:55 +08:00
if (File.Exists(imagePath))
{
// 直接載入完整圖
Bitmap image = new Bitmap(imagePath);
2025-04-07 16:54:10 +08:00
2025-06-20 13:10:55 +08:00
// 顯示在 PictureBox 上
pictureBoxSceneSoundEffects.Image = image;
2025-04-07 16:54:10 +08:00
2025-06-20 13:10:55 +08:00
// 設定 PictureBox 的大小與位置(依你的需要調整)
ResizeAndPositionPictureBox(pictureBoxSceneSoundEffects, 850, 450, image.Width , image.Height);
2025-04-07 16:54:10 +08:00
2025-06-20 13:10:55 +08:00
pictureBoxSceneSoundEffects.Visible = true;
}
else
{
Console.WriteLine("圖片檔案不存在:" + imagePath);
}
2025-04-07 16:54:10 +08:00
}
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();
}
}
}
}
}