📄 mouse.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>UDDF - Mouse</TITLE>
<META NAME="Description" CONTENT="Mouse and Joystick section of the Delphi Developers FAQ" >
<META NAME="KeyWords" CONTENT="" >
</HEAD>
<BODY bgcolor="#FFFFFF">
<CENTER>
<IMG SRC="../images/uddf.jpg"> </CENTER>
<HR SIZE="6" color="#00FF00">
<CENTER><FONT SIZE="7" FACE="Arial Black" COLOR="RED">Mouse and Joystick</FONT></CENTER>
<P><H1><A NAME="mouse0">OnMouseLeave Event needed</P></A></H1>
<P><I>From: "Steve Davidson" <sdpixel@wolfenet.com></I></P>
<P>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.</P>
<HR><PRE>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;
</PRE><HR>
<P><H1><A NAME="mouse1">Tip: using animated cursors</P></A></H1>
<P><I>From: "John F. Goyvaerts" <johnfg@tornado.be></I></P>
Hi.
<P>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.</P>
<P>Here's an example:</P>
<P>(mycursor.ani is an animated cursor file. You can create those with Microsoft's aniedit.exe)</P>
<HR><PRE>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;
</PRE><HR>
<P>I hope I've helped out someone with this information.</P>
<P><H1><A NAME="mouse2">Building Mouse Hooks</P></A></H1>
<P><I>From: David Ullrich <ullrich@math.okstate.edu></I></P>
<HR><PRE>library Hookdemo;
uses
Beeper in '\DELDEMOS\HOOKDEMO\BEEPER.PAS';
exports
SetHook index 1,
UnHookHook index 2,
HookProc index 3;
begin
HookedAlready:=False;
end.
</PRE><HR>
<P>, where beeper.pas is like so:</P>
<HR><PRE>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.
</PRE><HR>
<P> 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 .</P>
<H1><A NAME="mouse3">acessing the joystick from Delphi</A></H1>
<I>From: "Mark Wilkinson" <markw@peninsula.starway.net.au></I>
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.<p>
<hr><pre>
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;
<hr></pre>
Make sure to include MMSYSTEM in your USES clause.
<p><H1><A NAME="mouse4">Highlight a comp by mouse move<img src="../images/new.gif" width=28 height=11 border=0 alt=" [NEW]"></p></A></H1>
<I>[Bent Normann Olsen, normann@greennet.gl]</I><P>
<PRE>I want to write a component which highlights when the user moves the
mouse over the comp.</PRE>
Use CM_MOUSEENTER and CM_MOUSELEAVE messages like:
<Hr><PRE> 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;
</PRE><HR>
...and then read FMouseInPos when painting the control, or in any way you
like to change the highlightning.
<P><H1><A NAME="mouse5">Capturing the mouse as it exits a control<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>
<PRE>Does anyone know of a message or technique I can use to capture the
mouse as it exits a window or control?</PRE>
<I>[Stanley, Jim, Jim.Stanley@jacobs.com]</I><P>
<HR><PRE>
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;</PRE><HR>
<I>[Hans Hill, hhill@onaustralia.com.au]</I><P>
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.
<HR SIZE="6" color="#00FF00">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
<TD>This page has been created with </TD>
<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -