在xp下speedbutton的背景色问题

yangzhenhai 2003-05-21 11:02:22
我在winxp下运行,发现speedbutton的down属性设了true以后,背景色变成了白色。
但是我希望它的背景色是灰的,怎么设。
我修改了speedbutton的winproc事件,当重画时,修改背景色为灰,但是不行。
如果down属性设为false,又可以。
请问是怎么回事?
...全文
178 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
我的背景色明明色灰色,speedbutton怎么变成白色了
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
怎么不让他变
duduwolf 2003-05-21
  • 打赏
  • 举报
回复
当down=true时,speedbutton的背景色就自动变为他的parent的颜色了
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
up
jishiping 2003-05-21
  • 打赏
  • 举报
回复
多看看源程序。我就是看(调试)源程序,才知道如何解决的。
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
恩!终于解决了,你是怎么搞定的?去哪可以找到这方面的资料?
我的email yangzhenhai000@163.com
初学bcb问题多多,到时候还得向你多请教!
jishiping 2003-05-21
  • 打赏
  • 举报
回复
刚刚试了一下,看了一下效果和源程序,这样改写:
void __fastcall TMySpeedButton::WndProc(TMessage& Msg)
{
TRect Rect;
TControlCanvas* Canvas;
Graphics::TBitmap* Bmp;

TControl::WndProc(Msg);
if (Msg.Msg!=WM_PAINT) return;

Canvas = new TControlCanvas;
Canvas->Control = this;
Bmp = new Graphics::TBitmap;
Rect = this->ClientRect;
Bmp->Width = Rect.Width();
Bmp->Height = Rect.Height();
Bmp->Canvas->CopyRect(Rect,
Canvas, Rect);
Bmp->Transparent = true;
for(int n=0; n<2; n++) {
if (n==1 && !Btn->Down) break;

//对于Down的情形,需要做2次处理
Bmp->TransparentColor = n==1 ?
clBtnHighlight : clBtnFace;
Canvas->Brush->Color = FColor;
Canvas->FillRect(Rect);
Canvas->Draw(Rect.left, Rect.top, Bmp);
}
delete Bmp; delete Canvas;
}
「已注销」 2003-05-21
  • 打赏
  • 举报
回复
up
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
就是抄你上次的代码。除了down这种情况,其它都挺好的。
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
我做的一个组件,myspeedbutton,我定义了一个color属性,就当是背景色了
这是第一个文件myspeedbutton.h

//---------------------------------------------------------------------------

#ifndef MySpeedButtonH
#define MySpeedButtonH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Buttons.hpp>
#include <Controls.hpp>

class PACKAGE TMySpeedButton : public TSpeedButton
{
private:
Graphics::TColor FColor;
void __fastcall DrawItem(Graphics::TColor Value);
protected:
void __fastcall WndProc(TMessage& Msg);
public:
void __fastcall SetColor(Graphics::TColor Value);
__fastcall TMySpeedButton(TComponent* Owner);
__published:
__property Graphics::TColor Color = {read=FColor, write=SetColor, stored=IsColorStored, default=12632256};
};

//---------------------------------------------------------------------------

这是第二个文件myspeedbutton.cpp
#include <vcl.h>

#pragma hdrstop

#include "MySpeedButton.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TMySpeedButton *)
{
new TMySpeedButton(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMySpeedButton::TMySpeedButton(TComponent* Owner)
: TSpeedButton(Owner)
{
}

void __fastcall TMySpeedButton::SetColor(Graphics::TColor Value) {
FColor = Value;
this->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TMySpeedButton::WndProc(TMessage& Msg)
{
TRect Rect;
TControlCanvas* Canvas;
Graphics::TBitmap* Bmp;

TControl::WndProc(Msg);
if (Msg.Msg!=WM_PAINT) return;

Canvas = new TControlCanvas;
Canvas->Control = this;
Bmp = new Graphics::TBitmap;
Rect = this->ClientRect;
Bmp->Width = Rect.Width();
Bmp->Height = Rect.Height();
Bmp->Canvas->CopyRect(Rect,
Canvas, Rect);
Bmp->Transparent = true;
Bmp->TransparentColor = clBtnFace;
Canvas->Brush->Color = FColor;
Canvas->FillRect(Rect);
Canvas->Draw(Rect.left, Rect.top, Bmp);
delete Bmp;
delete Canvas;
}
//---------------------------------------------------------------------------
namespace Myspeedbutton
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMySpeedButton)};
RegisterComponents("Mybtn", classes, 0);
}
}
//---------------------------------------------------------------------------
jishiping 2003-05-21
  • 打赏
  • 举报
回复
肯定是你写错了,将你的代码贴出来。
yangzhenhai 2003-05-21
  • 打赏
  • 举报
回复
to jsp:
我就是用你上面的这个办法。不过我是重载了speedbutton控件的winproc方法做的。一切都很好。
不过就是按了down以后,就控制不了了。用了那个方法,也还是改变不了颜色,他总是白的。
因为我文本的颜色规定是白的,所以就看不清button上的字了。
非常着急,我看到有人做过的,可以让speedbutton按下去以后,底色也是灰的。应该是可以实现的,就是不知道他是怎么做的。
jishiping 2003-05-21
  • 打赏
  • 举报
回复
这是VCL故意将颜色设成不一样,让用户可以一下子就能看出按钮被按下去了,我觉得这
没有什么不好。如果你认为这样不好的话,可以参看我之前解答的一个帖子:
http://expert.csdn.net/Expert/TopicView1.asp?id=1796055
这个帖子里,是针对Form上有多个按钮写的,如果只有一个按钮,代码还可以简化。

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧