点击快了会闪烁,有解决方案吗?

chinayu2007 IT运维经理  2013-07-11 08:45:17

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

...全文
113 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chinayu2007 2013-07-24
引用 2 楼 chinayu2007 的回复:
妖哥有邮箱吗?代码发给你,有空研究一下。还有个问题,就是按钮第一次点击时,没有产生“按下”事件,但有“抬起”事件
按钮第一次点击时,没有产生“按下”事件,因为被后面的发生事件覆盖了
回复
ccrun.com 2013-07-11
cbfans at 163 dot com 发来瞅瞅。
回复
chinayu2007 2013-07-11
妖哥有邮箱吗?代码发给你,有空研究一下。还有个问题,就是按钮第一次点击时,没有产生“按下”事件,但有“抬起”事件
回复
ccrun.com 2013-07-11
你这个代码优化的空间很大。 首先尽可能开启组件的双缓冲。然后,在窗口过程中绘制用到的那些图片,你应该在程序开始时就载入,画的时候直接用就行了,而不是每次绘制时才从文件中获取,这样效率会很低。
回复
回复
yifuzhiming 2013-07-11
这个就是传说中的按钮自绘,没用过
回复
chinayu2007 2013-07-11
引用 3 楼 ccrun 的回复:
cbfans at 163 dot com 发来瞅瞅。
妖哥,已发到您的邮箱。 另发一个彩色按钮类,是继承TButton类来的,好象靠谱一点。
回复
相关推荐
发帖
C++ Builder
创建于2007-08-02

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
帖子事件
创建了帖子
2013-07-11 08:45
社区公告
暂无公告