Peacelyk
Peacelyk

Reputation: 1146

custom component click message

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!

Upvotes: 1

Views: 1881

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 595887

The VCL already tracks the WM_LBUTTONDOWN/UP messages for you. The csLButtonDown flag will be enabled in your component's ControlState property while the left mouse button is held down on your component (if the DragMode property is not set to dmAutomatic, that is). Your Paint() code can check for that flag and adjust its background drawing as needed.

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 612904

You can just override the MouseDown and MouseUp methods. Remember to check the value of the Button parameter!

Upvotes: 3

Ken White
Ken White

Reputation: 125669

You're not correctly testing for a mouse event. Try this:

if Message.Msg = WM_LBUTTONDOWN then
  // Some code
else
  inherited;

BTW, TMessage.LParam and TMessage.WParam are parameters passed with a specific message type (like WM_LBUTTONDOWN), and have different meanings depending on what TMessage.Msg actually is. There should never be a case where you get a generic message like TMessage and check only the WParam or LParam.

Upvotes: 1

Related Questions