Mouse and Joystick
  1. OnMouseLeave Event needed
  2. Tip: using animated cursors
  3. Building Mouse Hooks
  4. acessing the joystick from Delphi
  5. Highlight a comp by mouse move
  6. Capturing the mouse as it exits a control

OnMouseLeave Event needed

From: "Steve Davidson" <sdpixel@wolfenet.com>

All descendants of TComponent send a CM_MOUSEENTER and CM_MOUSELEAVE message when the mouse enters and leaves the bounds of the component. You will need to write a message handler for the respective messages if you wish to respond to them.


procedure CMMouseEnter(var msg:TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE;
..
..
..
procedure MyComponent.CMMouseEnter(var msg:TMessage);
begin
    inherited;
    {respond to mouse enter}
end;

procedure MyComponent.CMMouseLeave(var msg: TMessage);
begin
    inherited;
    {respond to mouse leave}
end;

Tip: using animated cursors

From: "John F. Goyvaerts" <johnfg@tornado.be>

Hi.

I was told there have been some questions in this newsgroup about animated cursors. I don't know if this was the real issue, but using animated cursors in your applications is very easy.

Here's an example:

(mycursor.ani is an animated cursor file. You can create those with Microsoft's aniedit.exe)


const crMyCursor = 1;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Load the cursor. Needs to be done only once
  Screen.Cursors[crMyCursor] :=
LoadCursorFromFile('c:\mystuff\mycursor.ani');
  // Use the cursor with this form
  Cursor := crMyCursor;
end;

I hope I've helped out someone with this information.

Building Mouse Hooks

From: David Ullrich <ullrich@math.okstate.edu>


library Hookdemo;

uses

  Beeper in '\DELDEMOS\HOOKDEMO\BEEPER.PAS';


exports
       SetHook index 1,
       UnHookHook index 2,
       HookProc index 3;

begin
   HookedAlready:=False;
end.

, where beeper.pas is like so:


unit Beeper;

interface

uses Wintypes,Winprocs,Messages;

function SetHook:Boolean;export;
function UnHookHook:Boolean;export;
function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;export;

var HookedAlready:Boolean;

implementation

var
   ourHook:HHook;


function SetHook:Boolean;
begin
if HookedAlready then exit;
ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
HookedAlready:=True;
end;

function UnHookHook:Boolean;
begin
UnHookWindowsHookEx(ourHook);
HookedAlready:=False;
end;

function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;
begin
   if (wParam=WM_LBUTTONDOWN) then MessageBeep(0);
   result:=CallNextHookEx(ourHook,Code,wParam,lParam);
end;

end.

Now if you call the SetHook function from an application there's a beep everytime you press the left mouse button - this continues until you call the UnHookHook function. In an actual application you're supposed to call CallNextHookEx immediately and do nothing else if code < 0 .

acessing the joystick from Delphi

From: "Mark Wilkinson" <markw@peninsula.starway.net.au> Actually, with Delphi 2 its a breeze. The following was taken from working code, so you'll have to adapt it for your own purposes, but it should point you in the right direction.


var
  myjoy: tjoyinfo;
begin
  joygetpos(joystickid1,@myjoy);
  trackbar1.position := myjoy.wypos;
  trackbar2.position := myjoy.wxpos;
  radiobutton1.checked := (myjoy.wbuttons and joy_button1)>0;
  radiobutton2.checked := (myjoy.wbuttons and joy_button2)>0;
end;

Make sure to include MMSYSTEM in your USES clause.

Highlight a comp by mouse move

[Bent Normann Olsen, normann@greennet.gl]

I want to write a component which highlights when the user moves the
mouse over the comp.
Use CM_MOUSEENTER and CM_MOUSELEAVE messages like:
  TYourObject = class(TAnyControl)
  ...
  private
    FMouseInPos : Boolean;
    procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
  ...
  end;

implementation

  procedure TYourObject.CMMouseEnter(var AMsg: TMessage);
  begin
    FMouseInPos := True;
    Refresh;
  end;

  procedure TYourObject.CMMouseLeave(var AMsg: TMessage);
  begin
    FMouseInPos := False;
    Refresh;
  end;

...and then read FMouseInPos when painting the control, or in any way you like to change the highlightning.

Capturing the mouse as it exits a control

Does anyone know of a message or technique I can use to capture the
mouse as it exits a window or control?
[Stanley, Jim, Jim.Stanley@jacobs.com]


procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if (X = 0) or (X = Panel1.Width) or (Y = 0) or (Y = Panel1.Width) then
    Screen.Cursor := crHSplit
  else
    Screen.Cursor := crDefault;
end;

[Hans Hill, hhill@onaustralia.com.au]

A technique I use is to use the OnMouseMove event for ALL my components, (including the Panels these components sit on). This way, when the mouse moves off (say) a Button, then the Panel.OnMouseMove is invoked. Its kludgy but it suits my purpose. BTW, be buggered if I could find any control messages prefixed CM_ and all mouse messages occur within a specific client area. The first mouse message sent for any control is the MouseMove followed by Click, double-click what ever, there is no mouse-exit - but then some other control has the mouse-move and is capturing mouse events.


Please email me and tell me if you liked this page.