This article explains in detail how Delphi obtains handles and sends messages in the form of examples. The specific usage instructions are as follows:
Find the handle of another window:
handle := FindWindow(nil,PChar('Title of the window'));//Find the form handle
Find subforms:
childHandle := FindWindowEx(handle,0,'subform class','subform title');
There is also an API for enumerating subforms.
EnumChildWindows(main body handle, @callback function, user parameters);
To use this function, you need to write a callback function yourself, 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;beginh:= FindWindow(nil,'abc.txt - Notepad');h:= FindWindowEx(h,0,'edit', nil);SendMessage(h,WM_SETTEXT,255,Integer(PChar('I'm here to measure')));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); Press the left mouse button SendMessage(Button.Handle,WM_LBUTTONUP, 0,0); Raise the left mouse button SendMessage(Edit.Handle,WM_SETTEXT,255,Integer(PChar('abc'))); Pass the text SendMessage(Edit.Handle,WM_Char,Wparam('Q'),2); Pass the 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 cut, copy, paste
Implement any key combination:
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 the window function of a window to send a message to that window. This function does not return unless the message is processed
SendMessage contains 4 parameters:
1. hwnd 32-bit window handle window can be any type of screen object, because Win32 can maintain handles to most visual objects
2. wMsg is a constant value used to distinguish other messages. These constants can be predefined constants in Windows units or customized constants.
3. wParam is usually a constant value related to the message, or it may be the handle of the window or control
4. lParam is usually a pointer pointing to data in memory. Since WParm, lParam and Pointer are all 32-bit, they can be converted to each other.
Detailed description of wMsg function:
★WM_Create creates a window
★WM_DESTROY Sent when a window is destroyed
★WM_MOVE Move a window
★WM_SIZE Change the size of a window
★WM_ACTIVATE A window is activated or deactivated
★WM_SETFOCUS A window gets focus
★WM_KILLFOCUS A window loses focus
★WM_ENABLE changes a window to the Enable state
★WM_SETREDRAW Set whether the window can be redrawn
★WM_SETTEXT The application sends this message to set the text of a window
★WM_GETTEXT The application sends this message to copy the text of the corresponding window to the buffer.
★WM_GETTEXTLENGTH Gets the length of text related to a window (excluding null characters)
★WM_PAINT asks a window to repaint itself
★WM_CLOSE sends a signal when a window or application is to be closed.
★WM_QUERYENDSESSION The user chooses to end the dialog box or the program calls the ExitWindows function itself
★WM_QUIT is used to end program execution
★WM_QUERYOPEN When the user window is restored to its previous size and 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 When the system color changes, send this message to all top-level windows
★After the WM_QUERYENDSESSION message, this message is sent to the application to notify it whether the conversation is over.
★WM_SHOWWINDOW This message is sent to this window when the window is hidden or displayed.
★WM_ACTIVATEAPP sends this message to the application which window is active and which is inactive.
★WM_FONTCHANGE This message is sent to all top-level windows when the system's font resource library changes.
★WM_TIMECHANGE Send this message to all top-level windows when the system time changes
★WM_CANCELMODE Send this message to cancel an ongoing mode (operation)
★WM_SETCURSOR If the mouse causes the cursor to move in a window
★WM_ENDSESSION When the system process issues and mouse input is not captured, a message is sent to a window.
★WM_MOUSEACTIVATE sends this message to the current window when the cursor is in an inactive window and the user is pressing a mouse button.
★WM_CHILDACTIVATE Send this message to the MDI child window when the user clicks the title bar of this window, or when the window is activated, moved, or resized
★WM_QUEUESYNC This message is sent by a computer-based training program, and the user input message is separated by the hook program of WH_JOURNALPALYBACK
★WM_GETMINMAXINFO This message is sent to the window when it is about to change size or position.
★WM_PAINTICON Sent to a minimized window when its icon is about to be redrawn
★WM_ICONERASEBKGND This message is sent to a minimized window only if its background must be redrawn before drawing the icon.
★WM_NEXTDLGCT★Send this message to a dialog box program to change the focus position
★WM_SPOOLERSTATUS This message is issued every time a job is added or subtracted from the print management queue.
★WM_DRAWITEM Sent 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 issued to its owner in response to the WM_KEYDOWN message
★WM_CHARTOITEM This message is sent by an LBS_WANTKEYBOARDINPUT style list box to its owner in response to the WM_CHAR message
★WM_SETFONT When drawing text, the program sends this message to get the color to be used by the control.
★WM_GETFONT The application sends this message to get the font of the text drawn by the current control.
★WM_SETHOTKEY The application sends this message to associate a window with a hotkey.
★WM_GETHOTKEY The application sends this message to determine whether the hotkey is associated with a window
★WM_QUERYDRAGICON This message is sent to a minimized window. When this window is about to be dragged and dropped and there is no icon defined in its class, the application can return an icon or cursor handle. The system displays this icon or cursor when the user drags and drops the icon.
★WM_COMPAREITEM Send this message to determine the relative position of newly added items in combobox or listbox
★WM_COMPACTING shows that the memory is very low.
★WM_WINDOWPOSCHANGING Send this message to the setwindowpos function or other window management functions when the size and position of the window are about to be changed.
★WM_WINDOWPOSCHANGED Send this message to the setwindowpos function or other window management functions when the size and position of the window have been changed.
★WM_POWER This message is sent when the system is about to enter the pause state.
★WM_COPYDATA This message is sent when one application passes data to another application
★WM_CANCELJOURNA When a user cancels the program log activation state, submit this message to the program
★WM_NOTIFY When an event of a control has occurred or the control needs to get some information, this message is sent to its parent window.
★WM_INPUTLANGCHANGEREQUEST When the user selects an input language, or the hotkey of the input language changes
★WM_INPUTLANGCHANGE This message is sent to the affected top-level window when the platform scene has been changed.
★WM_TCARD This message is sent to the application when the program has initialized the windows help routine.
★WM_HELP This message shows that the user pressed F1. If a menu is activated, this message will be sent to the menu associated with this window. Otherwise, it will be sent to the window with focus. If there is currently no focus, this message will be sent. for the currently active window
★WM_USERCHANGED This message is sent to all windows after the user has logged in or logged out. When the user logs in or logs out, the system updates the user's specific setting information. The system sends this message immediately when the user updates the settings.
★WM_NOTIFYFORMAT Public controls, custom controls and their parent windows use this message to determine whether the 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;