首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

C++ Builder 設計網頁動態按鈕 (SpeedButton + 資源檔完整流程)

     編輯:VC 教學

目標效果

滑鼠移入按鈕區域 → 游標變手型、按鈕圖示切換為高亮版本、播放提示音效;滑鼠移出 → 還原。純程式碼實現,無需 Flash/Photoshop 輸出大型動畫檔。

素材準備

  • one.bmp:正常狀態圖示
  • two.bmp:滑鼠懸停狀態圖示 (色彩更鮮豔)
  • crush.wav:短促音效

資源檔建立

純文字檔 app.rc:

crush WAV crush.wav
start BITMAP one.bmp
start1 BITMAP two.bmp

加入專案或編譯為 .res 連結。

元件配置

  • TSpeedButton 名為 StartSpeedButton
  • 屬性:Caption 空、Cursor = crHandPoint、Flat = true、Glyph = 無、ShowHint = true

程式碼實作

表單類別宣告

private:
    Graphics::TBitmap *bmpNormal, *bmpHover;
    char *wavHandle;

OnCreate 初始化

bmpNormal = new Graphics::TBitmap();
bmpHover  = new Graphics::TBitmap();
bmpNormal->Handle = LoadBitmap(HInstance, "one");
bmpHover->Handle  = LoadBitmap(HInstance, "two");

HRSRC hRes = FindResource(HInstance, "crush", "WAV");
HGLOBAL hGlob = LoadResource(HInstance, hRes);
wavHandle = (char*)LockResource(hGlob);

// 初始顯示正常狀態
StartSpeedButton->Glyph->Assign(bmpNormal);

事件處理

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);
}

完整可編譯專案結構,適合直接套用於需網頁風格互動按鈕的桌面應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved