自定义组件点击消息
我正在编写一个从速度按钮派生的小组件。我需要做的实际上是覆盖绘画方法,因为我想改变外观。 现在我已经达到了我想在单击按钮时提供不同背景颜色的地步。但是,我无法找到一种方法来捕获组件中的鼠标左键单击消息。
到目前为止我所使用的:
procedure KeyboardButton.WndProc(var Message: TMessage);
begin
if Message.LParam = VK_LBUTTON then
begin
//Some code
end
else
inherited;
end;
当我单击按钮 Message.LParam 不是 1 时,这不起作用。
我也尝试过...
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
好吧,我知道 CM_MOUSELEAVE 不是代表鼠标单击的消息。但也许有一条像 CM_MOUSECLICK 这样的消息???但我找不到它。到底,谁能告诉我什么是 CM_XXXX,因为我在 msdn 上找不到任何内容?似乎是 Delphi 特定的消息。
提前致谢!
I am writing a little component which i derive from speed button. All i need to do is actually override the paint method because i would like to change the appearance.
Now i've reached to the point where i would like to give different background color when button is clicked. However, i can't find a way to catch left mouse button click message in my component.
What i've used so far:
procedure KeyboardButton.WndProc(var Message: TMessage);
begin
if Message.LParam = VK_LBUTTON then
begin
//Some code
end
else
inherited;
end;
Which doesn't work as when i click on the button Message.LParam is not 1.
Also i tried...
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
Well, i know that CM_MOUSELEAVE is not a message that represents mouse click. But maybe there's a message like CM_MOUSECLICK??? I couldn't find it though. At all, can anyone please tell me what is CM_XXXX as i can't find anything from msdn? Seems like Delphi specific messages.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VCL 已经为您跟踪
WM_LBUTTONDOWN/UP
消息。当在组件上按住鼠标左键时,将在组件的 ControlState 属性中启用csLButtonDown
标志(如果 DragMode 属性未设置为dmAutomatic< /代码>,即)。您的
Paint()
代码可以检查该标志并根据需要调整其背景绘制。The VCL already tracks the
WM_LBUTTONDOWN/UP
messages for you. ThecsLButtonDown
flag will be enabled in your component'sControlState
property while the left mouse button is held down on your component (if the DragMode property is not set todmAutomatic
, that is). YourPaint()
code can check for that flag and adjust its background drawing as needed.您只需重写 MouseDown 和 MouseUp 方法即可。请记住检查 Button 参数的值!
You can just override the MouseDown and MouseUp methods. Remember to check the value of the Button parameter!
您没有正确测试鼠标事件。试试这个:
BTW、
TMessage.LParam
和TMessage.WParam
是使用特定消息类型(例如WM_LBUTTONDOWN
)传递的参数,并且具有不同的含义取决于TMessage.Msg
实际上是什么。 绝对不应该出现这样的情况:您收到诸如TMessage
之类的通用消息,并且仅检查WParam
或LParam
。You're not correctly testing for a mouse event. Try this:
BTW,
TMessage.LParam
andTMessage.WParam
are parameters passed with a specific message type (likeWM_LBUTTONDOWN
), and have different meanings depending on whatTMessage.Msg
actually is. There should never be a case where you get a generic message likeTMessage
and check only theWParam
orLParam
.