滑鼠未經過時顯示灰階圖示;滑鼠進入時切換為彩色亮麗圖示、滑鼠變手型、可選播放音效;滑鼠離開恢復原狀。類似網頁按鈕的 hover 效果。
start.bmp (16×16 或 32×32)start1.bmp (同尺寸,色彩飽和)crush.wav (短促)用記事本建立 mysrc.rc:
crush WAV crush.wav
start BITMAP start.bmp
start1 BITMAP start1.bmp
加入專案 (Project → Add to Project) 或用 brcc32.exe 編譯為 .res 再用 #pragma resource "mysrc.res" 連結。
TSpeedButton (名為 StartSpeedButton)。Caption 空、Cursor = crHandPoint、Flat = true、Glyph = 無 (設計時不指定)、ShowHint = true、Hint 設提示文字。Graphics::TBitmap *bmpNormal, *bmpHover;
char *wavHandle;
bmpNormal = new Graphics::TBitmap();
bmpHover = new Graphics::TBitmap();
bmpNormal->Handle = LoadBitmap(HInstance, "start");
bmpHover->Handle = LoadBitmap(HInstance, "start1");
// 載入音效資源
HRSRC hRes = FindResource(HInstance, "crush", "WAV");
HGLOBAL hGlob = LoadResource(HInstance, hRes);
wavHandle = (char*)LockResource(hGlob);
// 預設顯示 Hover 圖示 (或 Normal,視設計而定)
StartSpeedButton->Glyph->Assign(bmpHover);
void __fastcall TForm1::StartSpeedButtonMouseEnter(TObject *Sender)
{
StartSpeedButton->Glyph->Assign(bmpHover);
if (wavHandle) sndPlaySound(wavHandle, SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
}
void __fastcall TForm1::StartSpeedButtonMouseLeave(TObject *Sender)
{
StartSpeedButton->Glyph->Assign(bmpNormal);
}
此法不需繼承新元件,直接利用 TSpeedButton 事件與資源檔即可實現豐富動態效果。