Mega Code Archive

 
Categories / Delphi / Hardware
 

Restrice mouse pointer to a control or to an area

Title: Restrice mouse-pointer to a control or to an area Question: How do I restrict the mouse-cursor to a certain control or a certain area? Answer: Here is a project of mine. It shows you, how to keep the mouse-pointer to either the form or to a tbitbtn (could be almost any other control). unit Unit1; { Restricct Mouse to certain areas - here: to the Form only (Form1) to the tBitBtn, clicked on (BitBtn_RestrictMouseToBitBtn) Programmed by: Omer Yasar Can - omercan@home.nl ALI BABA Computin' for www.delphi3000.com } interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, Buttons; type TForm1 = class(TForm) BitBtn_RestrictMouseToForm: TBitBtn; BitBtn_RestrictMouseToBitBtn: TBitBtn; StatusBar1: TStatusBar; BitBtn_Off: TBitBtn; procedure BitBtn_RestrictMouseToFormClick(Sender: TObject); procedure BitBtn_OffClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BitBtn_RestrictMouseToBitBtnClick(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.BitBtn_OffClick(Sender: TObject); begin ClipCursor(nil); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ClipCursor(nil); end; //============================================================================== procedure TForm1.BitBtn_RestrictMouseToFormClick(Sender: TObject); var Rect: TRect; begin Rect.Left := Left; Rect.Top := Top; Rect.Right := Left+Width; Rect.Bottom := Top+Height; ClipCursor(@Rect); end; procedure TForm1.BitBtn_RestrictMouseToBitBtnClick(Sender: TObject); var Rect: TRect; var pnt : TPoint; begin Pnt:=BitBtn_RestrictMouseToBitBtn.ClientToScreen(Point(0,0)); Rect.Left := Pnt.x; Rect.Top := Pnt.y; Rect.Right := Rect.Left + BitBtn_RestrictMouseToBitBtn.Width; Rect.Bottom := Rect.Top + BitBtn_RestrictMouseToBitBtn.Height; ClipCursor(@Rect); end; //============================================================================== end. ------------------------------------------------------------------------ I give you the dfm-file to it as well: object Form1: TForm1 Left = 334 Top = 276 Width = 228 Height = 185 Caption = 'Delphi-programming' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnClose = FormClose PixelsPerInch = 96 TextHeight = 13 object BitBtn_RestrictMouseToForm: TBitBtn Left = 16 Top = 16 Width = 145 Height = 25 Caption = 'Restric Mouse to &Form' TabOrder = 0 OnClick = BitBtn_RestrictMouseToFormClick end object BitBtn_RestrictMouseToBitBtn: TBitBtn Left = 24 Top = 48 Width = 153 Height = 25 Caption = 'Restrict Cursor to this &BitBtn' TabOrder = 1 OnClick = BitBtn_RestrictMouseToBitBtnClick end object StatusBar1: TStatusBar Left = 0 Top = 138 Width = 220 Height = 19 Panels = item Text = 'Dir:\RestrictMouse' Width = 105 end item Text = 'Restrict mouse' Width = 50 end SimplePanel = False end object BitBtn_Off: TBitBtn Left = 128 Top = 104 Width = 75 Height = 25 Caption = '&Off' TabOrder = 3 OnClick = BitBtn_OffClick end end