winmouse.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. This is unit implements a subset of the msmouse unit functionality
  7. for the gui win32 graph unit implementation
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit winmouse;
  15. interface
  16. { initializes the mouse with the default values for the current screen mode }
  17. Function InitMouse:Boolean;
  18. { shows mouse pointer,text+graphics screen support }
  19. Procedure ShowMouse;
  20. { hides mouse pointer }
  21. Procedure HideMouse;
  22. { reads mouse position in pixels (divide by 8 to get text position in standard
  23. text mode) and reads the buttons state:
  24. bit 1 set -> left button pressed
  25. bit 2 set -> right button pressed
  26. bit 3 set -> middle button pressed
  27. Have a look at the example program in the manual to see how you can use this }
  28. Procedure GetMouseState(var x,y, buttons :Longint);
  29. { returns true if the left button is pressed }
  30. Function LPressed:Boolean;
  31. { returns true if the right button is pressed }
  32. Function RPressed:Boolean;
  33. { returns true if the middle button is pressed }
  34. Function MPressed:Boolean;
  35. (*!!!!! the following functions aren't implemented yet:
  36. { positions the mouse pointer }
  37. Procedure SetMousePos(x,y:Longint);
  38. { returns at which position "button" was last pressed in x,y and returns the
  39. number of times this button has been pressed since the last time this
  40. function was called with "button" as parameter. For button you can use the
  41. LButton, RButton and MButton constants for resp. the left, right and middle
  42. button }
  43. Function GetLastButtonPress(button:Longint;var x,y:Longint): Longint;
  44. { returns at which position "button" was last released in x,y and returns the
  45. number of times this button has been re since the last time. For button
  46. you can use the LButton, RButton and MButton constants for resp. the left,
  47. right and middle button
  48. }
  49. Function GetLastButtonRelease (button : Longint; var x,y:Longint): Longint;
  50. { sets mouse's x range, with Min and Max resp. the higest and the lowest
  51. column (in pixels) in between which the mouse cursor can move }
  52. Procedure SetMouseXRange (Min,Max:Longint);
  53. { sets mouse's y range, with Min and Max resp. the higest and the lowest
  54. row (in pixels) in between which the mouse cursor can move}
  55. Procedure SetMouseYRange (Min,Max:Longint);
  56. { set the window coordinates in which the mouse cursor can move }
  57. Procedure SetMouseWindow(x1,y1,x2,y2:Longint);
  58. { sets the mouse shape in text mode: background and foreground color and the
  59. Ascii value with which the character on screen is XOR'ed when the cursor
  60. moves over it. Set to 0 for a "transparent" cursor}
  61. Procedure SetMouseShape(ForeColor,BackColor,Ascii:Byte);
  62. { sets the mouse ascii in text mode. The difference between this one and
  63. SetMouseShape, is that the foreground and background colors stay the same
  64. and that the Ascii code you enter is the character that you will get on
  65. screen; there's no XOR'ing }
  66. Procedure SetMouseAscii(Ascii:Byte);
  67. { set mouse speed in mickey's/pixel; default: horizontal: 8; vertical: 16 }
  68. Procedure SetMouseSpeed(Horizontal ,Vertical:Longint);
  69. { set a rectangle on screen that mouse will disappear if it is moved into }
  70. Procedure SetMouseHideWindow(x1,y1,x2,y2:Longint);
  71. *)
  72. Const
  73. LButton = 1; { left button }
  74. RButton = 2; { right button }
  75. MButton = 4; { middle button }
  76. Var
  77. MouseFound: Boolean;
  78. implementation
  79. uses
  80. windows,graph;
  81. var
  82. oldexitproc : pointer;
  83. mousebuttonstate : byte;
  84. function InitMouse : boolean;
  85. begin
  86. InitMouse:=MouseFound;
  87. end;
  88. procedure ShowMouse;
  89. begin
  90. Windows.ShowCursor(true);
  91. end;
  92. procedure HideMouse;
  93. begin
  94. Windows.ShowCursor(false);
  95. end;
  96. function msghandler(Window: HWnd; AMessage:UInt; WParam : WParam; LParam: LParam): Longint; stdcall;
  97. begin
  98. { we catch the double click messages here too, }
  99. { even if they never appear because the graph }
  100. { windows doesn't have the cs_dblclks flags }
  101. case amessage of
  102. wm_lbuttondblclk,
  103. wm_lbuttondown:
  104. mousebuttonstate:=mousebuttonstate or LButton;
  105. wm_rbuttondblclk,
  106. wm_rbuttondown:
  107. mousebuttonstate:=mousebuttonstate or RButton;
  108. wm_mbuttondblclk,
  109. wm_mbuttondown:
  110. mousebuttonstate:=mousebuttonstate or MButton;
  111. wm_lbuttonup:
  112. mousebuttonstate:=mousebuttonstate and not(LButton);
  113. wm_rbuttonup:
  114. mousebuttonstate:=mousebuttonstate and not(RButton);
  115. wm_mbuttonup:
  116. mousebuttonstate:=mousebuttonstate and not(MButton);
  117. end;
  118. msghandler:=0;
  119. end;
  120. Function LPressed : Boolean;
  121. begin
  122. LPressed:=(mousebuttonstate and LButton)<>0;
  123. end;
  124. Function RPressed : Boolean;
  125. begin
  126. RPressed:=(mousebuttonstate and RButton)<>0;
  127. end;
  128. Function MPressed : Boolean;
  129. begin
  130. MPressed:=(mousebuttonstate and MButton)<>0;
  131. end;
  132. Procedure GetMouseState(var x,y,buttons : Longint);
  133. var
  134. pos : POINT;
  135. begin
  136. buttons:=mousebuttonstate;
  137. GetCursorPos(@pos);
  138. ScreenToClient(GraphWindow,@pos);
  139. x:=pos.x;
  140. y:=pos.y;
  141. end;
  142. procedure myexitproc;
  143. begin
  144. exitproc:=oldexitproc;
  145. mousemessagehandler:=nil;
  146. end;
  147. begin
  148. mousemessagehandler:=@msghandler;
  149. oldexitproc:=exitproc;
  150. exitproc:=@myexitproc;
  151. mousebuttonstate:=0;
  152. MouseFound:=GetSystemMetrics(SM_MOUSEPRESENT)<>0;
  153. end.
  154. {
  155. $Log$
  156. Revision 1.6 2003-04-23 11:35:00 peter
  157. * wndproc definition fixed
  158. Revision 1.5 2003/04/23 11:22:12 peter
  159. * fixed msghandler declarations
  160. Revision 1.4 2002/09/07 16:01:29 peter
  161. * old logs removed and tabs fixed
  162. }