#GETWINDOWTEXT DELPHI WINDOWS#
When the user presses the button, the program simply calls a Windows API function, SetWindowText, which changes the text or caption of the window passed as the first parameter. (Handles are never determined by the program, and they have no predefined values they are determined by the system, which generates new values each time you run a program.) This value, in fact, is determined by Windows and is sent back to the application. If you run this program several times you'll generally get different values for the handle. Every time you run this program you'll get a different value. Therefore, in this procedure we can simply refer to the Caption of the form and its Handle property directly.įigure 9.1: The WHandle example shows the handle of the form window. Procedure TFormWHandle.FormCreate(Sender: TObject) Ĭaption := Caption + ' ' + IntToStr (Handle) īecause FormCreate is a method of the form's class, it can access other properties and methods of the same class directly. We call IntToStr to convert the numeric value of the handle into a string, and we append that to the caption of the form, as you can see in Figure 9.1: In the code, I respond to the OnCreate event of the form and the OnClick event of the button, as indicated by the following textual definition of the main form:Īs soon as the form is created, the program retrieves the handle of the window corresponding to the form, by accessing the Handle property of the form itself. The WHandle program has a simple form, containing just a button. To complete this description, here is a simple example demonstrating Windows handles. They become useful when you want to call a Windows API function that is not supported by Delphi. In Delphi, you seldom need to use handles directly, since they are hidden inside forms, bitmaps, and other Delphi objects.
#GETWINDOWTEXT DELPHI CODE#
In other words, a handle is an internal code you can use to refer to a specific element handled by the system, including a window, a bitmap, an icon, a memory block, a cursor, a font, a menu, and so on. This doesn't apply only to functions operating on windows other Windows API functions have as their first parameter a GDI handle, a menu handle, an instance handle, a bitmap handle, or one of the many other handle types. Many Windows API functions, in fact, have a handle as the first parameter.
![getwindowtext delphi getwindowtext delphi](https://www.snapfiles.com/screenfiles/getwindowtext.gif)
![getwindowtext delphi getwindowtext delphi](https://i.imgur.com/xmq2W.png)
From that point on, your application can ask the system to operate on window number 142-moving it, resizing it, reducing it to an icon, and so on. The system informs you that the window you are working with is window number 142, for example. For example, when you work with a window (or a Delphi form), the system gives you a handle to the window.
![getwindowtext delphi getwindowtext delphi](https://www.thewindowsclub.com/wp-content/uploads/2011/12/copy-window-text-400x207.png)
In Windows, a handle is a reference to an internal data structure of the system. Handle data types are implemented as numbers, but they are not used as such. The name of this data type is THandle, and the type is defined in the Windows unit as: Instead, we'll look at a few elements of Delphi's interaction with the operating system and a couple of techniques that Delphi programmers can benefit from.Īmong the data types introduced by Windows in Delphi, handles represent the most important group. You would only want to take this approach for very special cases, such as the development of new Delphi components based on unusual API calls, and I don't want to cover the details. Nonetheless, programmers who want to use some special techniques not supported by the VCL still have that option in Delphi. The cover of the 4th edition of Essential Pascal,ĭelphi provides a complete encapsulation of the low-level Windows API using Object Pascal and the Visual Component Library (VCL), so it is rarely necessary to build Windows applications using plain Pascal and calling Windows API functions directly.