Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TComboBox with onMouse events 1

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

For some drag drop operations of data between a combobox and a listview I need the OnMouseOver and OnMouseUp events for the ComboBox. Does anyone know how to do this or where I can find a TComboBox with these events added?

Thanks,
Raoul
 
Hi,

Try to catch the Mouse-Messages directly from the Windows-System:

Code:
  procedure Handle_WM_MouseMove(var msg:TMessage); message WM_MOUSEMOVE;  
  procedure Handle_WM_LButtonUp(var msg:TMessage); message WM_LBUTTONUP; // left button
  procedure Handle_WM_MButtonUp(var msg:TMessage); message WM_MBUTTONUP; // middle button
  procedure Handle_WM_RButtonUp(var msg:TMessage); message WM_RBUTTONUP; // right button

for all procedures:
msg.LParamLo is the X-coordinate
msg.LParamHi is the X-coordinate

Leonardo
 
Thanks, If all of your procedures return the mouse position then:

procedure Handle_WM_LButtonUp(var msg:TMessage); message WM_LBUTTONUP; // left button

is the only one I need.

But I can't figure out how to implement it.

I have looked around for examples but still can't get it to work.

a simple test which is not working
Code:
procedure TForm1.Handle_WM_LButtonUp(var msg:TMessage);
begin
  if Msg.LParam = WM_LBUTTONUP then
     edit1.Text := 'up'
  else
     edit1.Text := 'down'
end;

Could you explain a bit further how to use this?

Thanks,
Raoul
 


Code:
procedure WMLButtonUp(var Msg:TMessage); message WM_LBUTTONUP;
procedure WMLButtonDown(var Msg:TWMMouse); message WM_LBUTTONDOWN;

---------------------------------------

procedure TForm1.WMLButtonUp(var Msg: TMessage);
begin
  Edit1.Text:='Up  x:'+IntToStr(Msg.LParamLo)+'  y:'+IntToStr(Msg.LParamHi);
  inherited;
end;

procedure TForm1.WMLButtonDown(var Msg: TWMMouse);
begin
  Edit1.Text:='Down  x:'+IntToStr(Msg.XPos)+'  y:'+IntToStr(Msg.YPos);
  inherited;
end;

I have used the default message, TMessage, for up and the mouse version, TWMMouse, for down. Both approaches are equally valid. See Windows API help for interpretation of messages.

However, if you put this code in your form, it will only capture mouse operations for the form, not the components on it.

Back to your original question.

Can you not accomplish what you need using OnDragOver and OnDragDrop events? No, you wouldn't have asked the question.

To get the events for a combo box you need to subclass TComboBox (i.e create your own).

The code is remarkably simple, because the events are already there, all you need to is publish them:

Code:
TMyComboBox=class(TComboBox)
published
  OnMouseDown;
  OnMouseMove;
  OnMouseUp;
  OnMouseWheel;
  OnMouseWheelDown;
  OnMouseWheelUp;
end;

Be sure to set the ComboBox Style to csDropDownList.

If you don't want to jump into component writing just yet, I do another post with a way round it.
 
How to use OnMouseUp event for TComboBox without subclassing

1. Create the event itself manually. e.g.

Code:
procedure ComboMouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);

----------------------------------

procedure TForm1.ComboMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Edit1.Text:=Format('MouseUp  x:%d  y:%d',[X,Y]);
end;

2. Assign the event to a protected property in the Form's OnCreate event, thus:

Code:
type
  TFrigComboBox=class(TComboBox);
{Note the above declaration is outside of any method}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TFrigComboBox(ComboBox1).OnMouseUp:=ComboMouseUp;
end;

3. Be sure to set Style property of the ComboBox to csDropDownStyle.

Have fun
Simon
 
Thanks a lot for all the information!

I tried your second suggestion and it is working now.

Regards,
Raoul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top