Mega Code Archive

 
Categories / Delphi / Graphic
 

Coordinates of any control

Title: Coordinates of any control Question: How can I get the coordinates of any Control -- where the Form does not have to be its Parent -- also have the pupmenu popup on this control by use of code. Find the mouse-coordinates too Answer: Here the pas-file -- unit1.pas unit Unit1; { Get the Coordinates of any Control on the Form, whereas this Control does not have to have the Form1 itself as its Parent; here, I piut a Panel (Panel1) or the Form (Form1), on the Panel, I put a second Panel (Panel2) and finally on this Panel2 I put ComboBox1 of which coordinates I go for. Also, it shows how to make a PopupMenu appear on that (or any other of course) Control, by Code (a control, again, that does not have to have Form(1) as its parent. How to read out the whereaboubt of your cursor will be shown to you... Omer Yasar Can, Holland/Turkey } interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls, Menus; type TForm1 = class(TForm) Panel1: TPanel; Panel2: TPanel; ComboBox1: TComboBox; StatusBar1: TStatusBar; BitBtn_Cursor: TBitBtn; BitBtn_cordinatesofanycontrol: TBitBtn; PopupMenu1: TPopupMenu; N1st1: TMenuItem; N2nd1: TMenuItem; N3rd1: TMenuItem; procedure BitBtn_CursorClick(Sender: TObject); procedure BitBtn_cordinatesofanycontrolClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.BitBtn_CursorClick(Sender: TObject); var pt: tpoint; begin //give out the actual coordinates of the mouse GetCursorPos( pt ); pt := ScreenToClient( pt ); StatusBar1.Panels[1].Text := 'Mouse: ' + ' ' + inttostr ( pt.X ) + ' ' + inttostr ( pt.Y ); end; procedure TForm1.BitBtn_cordinatesofanycontrolClick(Sender: TObject); var popupappearFromLeft,popupappearFromTop: integer; begin //give the true coordinates of a control (ComboBox) even if it is not having Form1 as its parent //hee: Panel1 put on Form1, Panel2 put on Panel1 and ComboBox1 put on Panel2 StatusBar1.Panels[2].Text := 'ComboBox: ' + ' ' + inttostr ( ComboBox1.ClientOrigin.X - Form1.ClientOrigin.X ) + ' ' + inttostr ( ComboBox1.ClientOrigin.Y - Form1.ClientOrigin.Y ); //make popupmenu1 appear in the middle of the Control (ComboBox1) popupappearFromLeft := ComboBox1.Width div 2; popupappearFromTop := ComboBox1.Height div 2; PopupMenu1.Popup( ComboBox1.ClientOrigin.X + popupappearFromLeft, ComboBox1.ClientOrigin.Y + popupappearFromTop); end; end. Get_Coordinates_of_any_Control.dpr program Get_Coordinates_of_any_Control; uses Forms, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. -------------------------------- Unit1.dfm object Form1: TForm1 Left = 358 Top = 409 Width = 550 Height = 380 Caption = 'Delphi-programming -- HOW TO -- Cathegory: Visible/Runtime' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Panel1: TPanel Left = 24 Top = 40 Width = 249 Height = 169 Caption = 'Panel1' TabOrder = 1 object Panel2: TPanel Left = 16 Top = 8 Width = 201 Height = 81 Caption = 'Panel2' TabOrder = 0 object ComboBox1: TComboBox Left = 24 Top = 48 Width = 145 Height = 21 ItemHeight = 13 TabOrder = 0 Text = 'ComboBox1' end end end object StatusBar1: TStatusBar Left = 0 Top = 327 Width = 542 Height = 19 Panels = item Text = '(Dir:)\Get Coordinates of any Control' Width = 185 end item Text = 'Mouse:' Width = 100 end item Text = 'ComboBox:' Width = 50 end end object BitBtn_Cursor: TBitBtn Left = 48 Top = 224 Width = 75 Height = 25 Caption = 'Cursor' TabOrder = 3 OnClick = BitBtn_CursorClick end object BitBtn_cordinatesofanycontrol: TBitBtn Left = 128 Top = 224 Width = 75 Height = 25 Caption = 'Control...' TabOrder = 0 OnClick = BitBtn_cordinatesofanycontrolClick end object PopupMenu1: TPopupMenu Left = 240 Top = 168 object N1st1: TMenuItem Caption = '1st' end object N2nd1: TMenuItem Caption = '2nd' end object N3rd1: TMenuItem Caption = '3rd' end end end