To get a control instance from a control handle

Source: Internet
Author: User

Create a Delphi project, place a tedit and TButton, and enter some text in Edit1.


{---------------Gets the control instance through the control handle-----------------------------------------------------------principle, see Classes.pas Unit, 13045 line <Delphi7>---------------------------------------principles see Classes.pas Unit, 11613 lines <Delphi2007>-------- ----------------------------principle See Classes.pas Unit, 13045 lines <Delphi2010>------------------------------------principles See Classes.pas unit, 13512 rows <DelphiXE>-----------------------} function Getinstancefromhwnd (const hwnd:cardinal):
Twincontrol;

  Type pobjectinstance = ^tobjectinstance;            tobjectinstance = packed record code:byte;
    {Short Jump $E 8}       Offset:integer; {Calcjmpoffset (Instance, @Block ^. Code); } next:pobjectinstance;
    {Mainwndproc Address}         Self:pointer;
{Control object address} end;
var wc:pobjectinstance;
  Begin Result: = nil;
  WC: = Pointer (GetWindowLong (HWnd, GWL_WNDPROC)); If WC <> nil then begin Result: = WC.
  Self;
End

End
Procedure Tform1.btn1click (Sender:tobject); Begin BTN1. Caption: = Tedit (Getinstancefromhwnd (edt1. Handle)).
Text;
 End


See how we got the text in the Edit1. Very magical, understand the VCL code, it is not so magical.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.