点击快了会闪烁,有解决方案吗?
.h文件
//---------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TButton *Button1;
TButton *Button2;
private:// User declarations
TWndMethod OldProc;
void __fastcall NewProc(TMessage &Msg);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
//---------------------------------------
.cpp文件
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//SetWindowLong(Button1->Handle, GWL_STYLE, GetWindowLong(Button1->Handle,GWL_STYLE) | BS_OWNERDRAW );
OldProc = Button1->WindowProc;
Button1->WindowProc = NewProc;
DoubleBuffered = true;
//创建圆角矩形按钮,去掉这两句就是默认的矩形区域
HRGN hRgnR = CreateRoundRectRgn(0,0,Button1->Width,Button1->Height,45,45);
SetWindowRgn(Button1->Handle,hRgnR,TRUE);
}
//---------------------------------------
//用到三张图片,分别表示几种状态:正常(移出),移入(抬起),按下
void __fastcall TForm1::NewProc(TMessage &Msg)
{
TRect Rect;
Graphics::TBitmap* Bmp;
TControlCanvas* Canvas;
OldProc(Msg);
if(Msg.Msg == CM_MOUSEENTER)//鼠标移入
{
Bmp = new Graphics::TBitmap;
Bmp->LoadFromFile("over2.bmp");
Bmp->Transparent = true;
Bmp->TransparentColor = clWhite;
Canvas = new TControlCanvas;
Canvas->Control = Button1;
Canvas->Brush->Style= bsClear;
Canvas->CopyRect(TRect(0,0,228,58), Bmp->Canvas,TRect(0,0,228,58));
Canvas->Font->Color=clBlack;
Canvas->TextOutA(23,10,"正常");
delete Bmp; delete Canvas;
}
if(Msg.Msg == CM_MOUSELEAVE)//鼠标移出
{
Bmp = new Graphics::TBitmap;
Bmp->LoadFromFile("normal2.bmp");
Bmp->Transparent = true;
Bmp->TransparentColor = clWhite;//背景透明色
Canvas = new TControlCanvas;
Canvas->Control = Button1;
Canvas->Brush->Style= bsClear; //文字透明
Canvas->Draw(0, 0, Bmp);
Canvas->Font->Color=clBlack;
Canvas->Brush->Style= bsClear;
Canvas->TextOutA(23,10,"正常");
delete Bmp; delete Canvas;
}
if(Msg.Msg == WM_LBUTTONDOWN) //鼠标按下
{
Bmp = new Graphics::TBitmap;
Bmp->LoadFromFile("down2.bmp");
Bmp->Transparent = true;
Bmp->TransparentColor = clWhite;
Canvas = new TControlCanvas;
Canvas->Control = Button1;
Canvas->Control->Parent->DoubleBuffered =true;
Canvas->CopyRect(TRect(0,0,228,58), Bmp->Canvas,TRect(0,0,228,58));
Canvas->Font->Color=clBlack;
Canvas->Brush->Style= bsClear;
Canvas->TextOutA(23,10,"按下");
delete Bmp; delete Canvas;
return;
}
if(Msg.Msg == WM_LBUTTONUP) //鼠标抬起
{
Bmp = new Graphics::TBitmap;
Bmp->LoadFromFile("over2.bmp");
Canvas = new TControlCanvas;
Canvas->Control = Button1;
Canvas->CopyRect(TRect(0,0,228,58), Bmp->Canvas,TRect(0,0,228,58));
Canvas->Font->Color=clBlack;
Canvas->Brush->Style= bsClear;
Canvas->TextOutA(23,10,"抬起");
delete Bmp; delete Canvas;
return;
}
if (Msg.Msg == WM_PAINT || Msg.Msg==BM_SETSTATE)//常规状态
{
Bmp = new Graphics::TBitmap;
Bmp->Transparent = true;
Bmp->TransparentColor = clWhite;
Canvas = new TControlCanvas;
Canvas->Control = Button1;
Canvas->Control->Parent->DoubleBuffered =true;
if(Msg.WParam == MK_LBUTTON)
{
Bmp->LoadFromFile("down2.bmp");
}
else
Bmp->LoadFromFile("normal2.bmp");
Canvas->CopyRect(TRect(0,0,228,58), Bmp->Canvas,TRect(0,0,228,58));
Canvas->Brush->Style= bsClear;
if(Msg.WParam == MK_LBUTTON)
{
Canvas->Font->Color=clBlack;
Canvas->TextOutA(23,10,"按下");
}
else
{
Canvas->Font->Color=clBlack;
Canvas->TextOutA(23,10,"正常");
}
delete Bmp; delete Canvas;
}
}