再问一个简单问题,怎么使鼠标移过Shape时改变Shape的颜色

joshstone 2002-10-10 09:33:53
就是说鼠标放在Shape上是黑的,移出Shape的范围又变成白的了!
...全文
49 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbc 2002-10-11
  • 打赏
  • 举报
回复
这是另外一种解决方法:它只需要重载shape的windowproc即可:
.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TShape *Shape1;
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
void __fastcall proc(TMessage &message);
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)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Shape1->WindowProc = proc;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::proc(TMessage &message)
{
if (message.Msg == CM_MOUSEENTER)
Shape1->Brush->Color = clBlack;
if (message.Msg == CM_MOUSELEAVE)
Shape1->Brush->Color = clWhite;
Shape1->Dispatch(&message);
}

以上给你的两种方法都可以实现,就看你怎么用的了
gudufeixiang 2002-10-11
  • 打赏
  • 举报
回复
使用Tshape的onmousemove
xylwinfast 2002-10-10
  • 打赏
  • 举报
回复
Shape1 的事件onmousemove为:
Shape1->Brush->Color=clRed;
Shape1的背景对象(如form1)的事件onmousemove为:
Shape1->Brush->Color=clWhite;
joshstone 2002-10-10
  • 打赏
  • 举报
回复
上面的,你试试改变shape的颜色看看?:)
tramp_man 2002-10-10
  • 打赏
  • 举报
回复
使用Tshape的onmousemove事件。
zbc 2002-10-10
  • 打赏
  • 举报
回复
你可以在自己的程序里面来处理CM_MOUSEENTER与CM_MOUSELEAVE消息的,给你一个easy的例子,创建一个TShape组件的子类:
.h
//---------------------------------------------------------------------------

#ifndef Shape3H
#define Shape3H
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TShape3 : public TShape
{
private:
protected:
public:
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(CM_MOUSEENTER,TMessage,OnMouseEnter);
MESSAGE_HANDLER(CM_MOUSELEAVE,TMessage,OnMouseLeave);
END_MESSAGE_MAP(TShape)
__fastcall TShape3(TComponent* Owner);
__published:
protected:
void __fastcall OnMouseEnter(TMessage &e);
void __fastcall OnMouseLeave(TMessage &e);
};
//---------------------------------------------------------------------------
#endif
.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Shape3.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(TShape3 *)
{
new TShape3(NULL);
}
//---------------------------------------------------------------------------
__fastcall TShape3::TShape3(TComponent* Owner)
: TShape(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TShape3::OnMouseEnter(TMessage &e)
{
this->Brush->Color = clBlack;
}
void __fastcall TShape3::OnMouseLeave(TMessage &e)
{
this->Brush->Color = clWhite;
}

namespace Shape3
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TShape3)};
RegisterComponents("Samples", classes, 0);
}
}
//---------------------------------------------------------------------------

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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