views:

347

answers:

3

I need to detect OnMouseLeave event for TDateTimePicker component, but it doesn't contain such event in events list. Is there a way to detect it manually?

A: 

TDateTime is not a visual component, so it would not have a mouse leave event. Can you check the actual component (is it the date time picker?) The onExit event should handle the mouse leaving a visual control.

It would also be helpful to know which version of Delphi you are using

Thanks...

Sorry I could offer more help yesterday, the computer I was on doesn't have Delphi installed. To get the Mouse Leave event for a particular control that doesn't export it, try the following. (This code is on the form holding the date time picker control)

procedure TForm1.FormCreate(Sender: TObject);
begin
    application.OnMessage := AppMsg;
    // Save the windows hande of the date time picker...
    DTWind := DateTimePicker1.handle;
end;

procedure TForm1.AppMsg(var Msg: TMsg; var Handled: Boolean);
begin
  // If we find a mouse leave event, for the date/time picker,
  // then do something
  if (msg.message = 160) and
     (msg.hwnd = DTWind)
  then
  begin
     if dateTimePicker1.color = clBlue
     then dateTimePicker1.Color := clRed
     else  dateTimePicker1.Color := clBlue;
  end;
end;

Create a procedure to get application messages (AppMsg) and assign it to the OnMessage event of the application object. Save the windows handle of the dateTimePicker control.

Within the procedure, look for the mouse leave message from the DateTimePicker control, and then do whatever processing you want (if my example, I just play with the colors)

Hope this gives you a work-around.

Sparky
The title of the question states Delphi 7. The `OnExit` event does not handle mouse movement, only curosr movement.
_J_
Yep, TDateTimePicker, but OnExit is something totally different
Tofig Hasanov
Sorry, my browser cut off the version of Delphi... You could try capturing the onMessage Event and respond to the CMMouseLeave message.
Sparky
A: 

JVCL has a datetimepicker control and it should have OnMouseEnter/OnMouseLeave events regardless of Delphi version (AFAIK all JVCL controls have those).

utku_karatas
+2  A: 

You can unprotect CMMouseLeave procedure.

TDateTimePicker = class(ComCtrls.TDateTimePicker)   
   procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;  
 end;

 { TDateTimePicker }

 procedure TDateTimePicker.CMMouseLeave(var Message: TMessage);
 begin 
   {do something}; 
 end;
Linas
Whether it's protected doesn't matter. Message handlers can be private and still overrideable. The trick here is that you can put that class above your form-class declaration and then automatically get the new behavior without having to install a new component. To do it without scope tricks, simply assign a new value to the control's `WindowProc` property.
Rob Kennedy
This thing works only once, once OnMOuseLeave message has been caught, it doesn't work anymore. Does anybody know why?
Tofig Hasanov
For me it works each time you leave the control.
Linas
Ok, I found what was the problem, it works now! Thanks :) Another question arises, how can I determine, which component sent this message?
Tofig Hasanov