模仿網頁按鈕:滑鼠移入時切換為彩色圖片並播放音效,移出恢復灰階圖片。利用 TSpeedButton 的 Glyph 屬性切換圖片,SndPlaySound 播放記憶體中的 WAV 音效。
crush WAV crush.wav
start BITMAP one.bmp // 正常狀態 (灰階)
start1 BITMAP two.bmp // 滑鼠進入狀態 (彩色)
編譯資源:brcc32 app.rc 產生 app.res,專案加入 {$R app.res} 或直接加入 app.rc。
Graphics::TBitmap *one, *two;
char *chWavHandle;
one = new Graphics::TBitmap();
two = new Graphics::TBitmap();
one->Handle = LoadBitmap(HInstance, "one");
two->Handle = LoadBitmap(HInstance, "two");
HRSRC hRes = FindResource(HInstance, "crush", "WAV");
HGLOBAL hGlob = LoadResource(HInstance, hRes);
chWavHandle = (char*)LockResource(hGlob);
StartSpeedButton->Glyph->Assign(two); // 初始顯示灰階
delete chWavHandle; delete two; delete one;
bool blStart = true; // true 表示可亮起
// SpeedButton OnMouseMove
void __fastcall TForm1::StartSpeedButtonMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (blStart) {
StartSpeedButton->Glyph->Assign(one); // 切換彩色
SndPlaySound(chWavHandle, SND_MEMORY | SND_SYNC); // 播放音效
blStart = false;
}
}
// Form OnMouseMove (偵測滑鼠移出按鈕)
void __fastcall TForm1::FormMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (!blStart) {
StartSpeedButton->Glyph->Assign(two); // 恢復灰階
blStart = true;
}
}
Caption 空白Cursor = crHandPoint (手型游標)Flat = true (扁平外觀)ShowHint = trueOnMouseMove 必須有足夠區域覆蓋按鈕周圍,否則滑鼠快速移出可能漏過事件;可再加最外層容器雙重保險。#include <mmsystem.h> 並連結 winmm.lib (BCB 專案自動連結)。