Delphi implementation gets the handle and sends the message method _delphi

Source: Internet
Author: User
Tags ord

In this paper, the method of Delphi acquiring the handle and sending the message is described in detail, and the specific usage is as follows:

To find a handle to another window:

Handle: = FindWindow (Nil,pchar (' Title of Window '));//Find the form handle

Find child form:

Childhandle: = FindWindowEx (handle,0, ' subform class ', ' Sub-form title '); 

There is also an API for enumerating the subforms,

Enumchildwindows (cast body handle, @ CALLBACK function, user parameter);

Use this function to write a callback function of your own, such as:

function Enumchildproc (Ahwnd:hwnd; param:lparam): Boolean; stdcall;
SendMessage (HANDLE,MESSAGE,WL,RL)
 unit Unit1;

interface uses windows,messages,tlhelp32,sysutils,classes,graphics,controls,forms,dialogs,stdctrls;

 Type Tform1=class (Tform) procedure formcreate (sender:tobject); 
Private {Private declarations} public {public declarations} end;
var Form1:tform1; Implementation {$R *.
DFM} procedure Tform1.formcreate (Sender:tobject);
var A,b:pansichar;
H:hwnd;
Begin h:= FindWindow (Nil, ' abc.txt-Notepad ');
h:= FindWindowEx (h,0, ' edit ', nil);
SendMessage (H,wm_settext,255,integer (' I measure it '));
ShowMessage (IntToStr (h));

End 
SendMessage (Treeview.handle,tvm_setbkcolor,0,rgb (255,0,0));  Set the TV background color
SendMessage (button.handle,wm_lbuttondown,0,0);   The left mouse button press
SendMessage (button.handle,wm_lbuttonup,0,0); The left mouse button lifts
SendMessage (Pchar (' abc ')) Edit.handle,wm_settext,255,integer the text
SendMessage (edit.handle,wm_  Char,wparam (' Q '), 2);  Pass character
SendMessage (button.handle,bm_setstyle,bs_radiobutton,1);  Change button style
SendMessage (combobox.handle,cb_setdroppedwidth,300,0); Change Cbdownwidth

Wm_cut, Wm_copy and Wm_paste shearing, copying, pasting

Implement any combination key:

Keybd_event (Vk_control, Mapvirtualkey (Vk_control, 0), 0, 0);
keybd_event (Ord (' V '), Mapvirtualkey (Ord (' V '), 0), 0, 0);
keybd_event (Ord (' V '), Mapvirtualkey (Ord (' V '), 0), KEYEVENTF_KEYUP, 0);
Keybd_event (Vk_control, Mapvirtualkey (Vk_control, 0), KEYEVENTF_KEYUP, 0);

SendMessageA Description: Call a window function, send a message to that window. The function does not return unless the message is processed

The SendMessage contains 4 parameters:
1. HWND 32-bit Window handle window can be any type of screen object, because Win32 can maintain the handle of most visual objects
2. wmsg A constant value that distinguishes other messages, which can be predefined constants in Windows cells or custom constants
3. WParam is usually a constant value associated with a message, or it may be a handle to a window or control
4. LParam is usually a pointer to in-memory data. Since Wparm, lparam, and pointer are all 32-bit, they can be converted to each other

wmsg function Detailed Description:

★wm_create Create a window
★wm_destroy send when a window is corrupted
★wm_move move a window
★wm_size change the size of a window
★wm_activate A window is activated or out of active state
★wm_setfocus a window to get focus
★wm_killfocus a window loses focus
★wm_enable a window to change to a ENABLE state
★wm_setredraw set whether the window can be repaint
★wm_settext application sends this message to set the text of a window
The ★wm_gettext application sends this message to copy the corresponding window's text to the buffer
★wm_gettextlength gets the length of text associated with a window (not including null characters)
★wm_paint requires a window to repaint itself
★wm_close send a signal when a window or application is to be closed
★wm_queryendsession user chooses the End dialog box or the program calls the ExitWindows function itself
★wm_quit to end a program run
★wm_queryopen the user window to a previous size position, send this message to an icon
★WM_ERASEBKGND when the window background must be erased (for example, when the window changes size)
★wm_syscolorchange send this message to all top-level windows when the system color changes
★wm_queryendsession message, this message is sent to the application to inform it whether the conversation is over
★wm_showwindow when the hidden or displayed window is sent this message to this window
★wm_activateapp send this message to the application which window is active and which is inactive
★wm_fontchange send this message to all top-level windows when the font repository for the system changes
★wm_timechange send this message to all top-level windows when the system time changes
★wm_cancelmode send this message to cancel some kind of ongoing touch (operation)
★wm_setcursor If the mouse causes the cursor to move through a window
★wm_endsession sends a message to a window when the system process is issued and the mouse input is not captured
★wm_mouseactivate when the cursor is in an inactive window and the user is pressing a key on the mouse to send this message to the current window
★wm_childactivate send this message to the MDI child window when the user clicks on the title bar of the window, or when the window is activated, moves, changes size
★wm_queuesync This message is sent by a computer-based training program to isolate user input messages through a wh_journalpalyback hook program
★wm_getminmaxinfo this message to the window when it's going to change size or position
★wm_painticon is sent to the minimized window when its icon will be redraw
★WM_ICONERASEBKGND this message to a minimized window, and its background must be repaint before the icon is drawn.
★wm_nextdlgct★ send this message to a dialog box program to change the focus position
★wm_spoolerstatus this message whenever a print management queue increases or decreases a job
★wm_drawitem send when the visual appearance of Button,combobox,listbox,menu changes
★wm_measureitem when Button, ComboBox, ListBox, ListView Control, or MenuItem is created
★wm_vkeytoitem This message has a lbs_wantkeyboardinput style sent to its owner to respond to the WM_KEYDOWN message
★wm_chartoitem This message is sent to his owner by a lbs_wantkeyboardinput-style list box to respond to WM_CHAR messages
★wm_setfont when text is drawn the program sends this message to get the color the control wants to use
★wm_getfont application sends this message to get the font of the current control's drawing text
★wm_sethotkey application sends this message so that a window is connected to a hotkey
★wm_gethotkey application sends this message to determine if a hot key is associated with a window
★wm_querydragicon This message is sent to the minimized window, when the window is to be dragged and no icon is defined in its class, the application can return an icon or cursor handle, and the system displays the icon or cursor when the user drags the icon
★wm_compareitem send this message to determine the relative position of the newly added item in the ComboBox or ListBox
★wm_compacting shows little memory.
★wm_windowposchanging send this message to the size and position of the window will be changed to call the SetWindowPos function or other window management functions
★wm_windowposchanged send this message to the size and position of the window has been changed to call the SetWindowPos function or other window management functions
★wm_power send this message when the system is about to go into a paused state
★wm_copydata send this message when an application passes data to another application
★wm_canceljourna when a user cancels the program Log activation state, submits this message to the program
★wm_notify send this message to its parent window when an event has occurred for a control or if the control needs to get some information
★wm_inputlangchangerequest when the user chooses an input language, or a hotkey for the input language changes
★wm_inputlangchange send this message to the most top-level window affected when the platform site has been changed
★wm_tcard send this message to the application when the program has initialized the Windows Help routine
★wm_help This message shows that the user pressed the F1, if a menu is active, send this message to the menu associated with this window, otherwise send it to the window with focus, if there is currently no focus, send this message to the currently active window
★wm_userchanged when the user has logged in or exited to send this message to all the Windows, when the user login or exit the system updates the user's specific settings information, the system will send this message as soon as the user updates the settings
★wm_notifyformat common controls, custom controls, and their parent Windows use this message to determine whether a control uses ANSI or Unicode structure

 function Enumchildwindowsproc (hwnd, Lparam:integer): Boolean;  
  var buffer:array[0..255] of Char;  
 Begin Result: = True;  
 GetClassName (hwnd,buffer,256);  
 If Strpas (Buffer) = ' Edit ' THEN begin SendMessage (Hwnd,wm_gettext,256,lparam);  
 Result:=false;  
  End  
  
  End  
  Procedure Tform1.button1click (Sender:tobject);  
 var Hwnd:integer;  
  BUFFER:ARRAY[0..255] of Char;  
 Begin hwnd: = FindWindow (' Cabinetwclass ', nil);  
 If hwnd<> 0 THEN BEGIN Enumchildwindows (hwnd, @EnumChildWindowsProc, Longint (@buffer [0]));  
 Caption: = Strpas (buffer);  
  End 
End; 

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.