C # SendMessage usage

Source: Internet
Author: User

Function: This function sends a specified message to one or more windows. This function calls the window program for the specified window until the window program finishes processing the message and returns it. This function is one of the main means to transmit messages between applications.
Function prototype: LRESULT SendMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM IParam );

Parameters:

HWnd: the handle of the window whose window program will receive messages. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows, however, messages are not sent to subwindows.

Msg: Specifies the message to be sent.

WParam: Specify the additional message.

IParam: Specify the attached message.

Return Value: the return value specifies the message processing result, depending on the sent message.

Note: applications that need to communicate with HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for communications between applications.

If the specified window is created by the calling thread, the window program is called as a subroutine immediately. If the specified window is created by different threads, the system switches to the thread and calls the appropriate window program. Messages between threads are processed only when the thread executes the message retrieval code. The sending thread is blocked until the receiving thread finishes processing the message.

Use this function in C # To first import the namespace:
Using System. Runtime. InteropServices;

Then write the code of the API reference and put it into the class.
[DllImport ("user32.dll", EntryPoint = "SendMessage")]
Private static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, int lParam );

This function has four parameters: the first is the window handle, the window can be any type of screen object, and the second is the constant value used to distinguish other messages; the third parameter is usually a message-related constant value, or a window or control handle. The third parameter is an optional parameter, for example, you do not need to click this parameter,
For other messages, such as moving the mouse, you may need to add some mouse parameters here. The fourth is usually a pointer to the data in the memory. In C #, the message must be defined as the original hexadecimal number in windows, for example, const int WM_Lbutton = 0x201; // defines the left-click message of the mouse. The detailed value is at the end.

For example:

Const int BM_CLICK = 0xF5; IntPtr maindHwnd = FindWindow (null, "QQ User Logon"); // obtain the QQ login box handle if (maindHwnd! = IntPtr. Zero) {IntPtr childHwnd = find1_wex (maindHwnd, IntPtr. Zero, null, "login"); // obtain the button handle if (childHwnd! = IntPtr. zero) {SendMessage (childHwnd, BM_CLICK, 0, 0); // send the message of the click button} else {MessageBox. show ("subwindow not found") ;}} else {MessageBox. show ("no window found ");}

 

Constant Value of the wMsg parameter:

// Create a window
Const int WM_CREATE = 0x01;
// Send when a window is damaged
Const int WM_DESTROY = 0x02;
// Move a window
Const int WM_MOVE = 0x03;
// Change the size of a window
Const int WM_SIZE = 0x05;
// A window is activated or inactive
Const int WM_ACTIVATE = 0x06;
// Obtain the focus in a window
Const int WM_SETFOCUS = 0x07;
// A window loses focus
Const int WM_KILLFOCUS = 0x08;
// A window changes to the Enable state.
Const int WM_ENABLE = 0x0A;
// Set whether the window can be re-painted
Const int WM_SETREDRAW = 0x0B;
// The application sends this message to set the text of a window
Const int WM_SETTEXT = 0x0C;
// The application sends this message to copy the text of the corresponding window to the buffer zone.
Const int WM_GETTEXT = 0x0D;
// Obtain the length of the text related to a window (excluding empty characters)
Const int WM_GETTEXTLENGTH = 0x0E;
// Ask a window to redraw itself
Const int WM_PAINT = 0x0F;
// Send a signal when a window or application is closed
Const int WM_CLOSE = 0x10;
// When the user selects the end dialog box or the program calls the ExitWindows Function
Const int WM_QUERYENDSESSION = 0x11;
// Used to end the program running
Const int WM_QUIT = 0x12;
// Send the message to an icon when the user window recovers the previous size position.
Const int WM_QUERYOPEN = 0x13;
// When the window background must be erased (for example, when the window size changes)
Const int WM_ERASEBKGND = 0x14;
// Send this message to all top-level windows when the system color changes
Const int WM_SYSCOLORCHANGE = 0x15;
// When the system process sends the WM_QUERYENDSESSION message, the message is sent to the application to notify the application of whether the conversation ends.
Const int WM_ENDSESSION = 0x16;
// Send this message to the hidden or displayed window
Const int WM_SHOWWINDOW = 0x18;
// Send this message to the application. Which window is activated and which is not activated?
Const int WM_ACTIVATEAPP = 0x1C;
// Send this message to all top-level windows when the font resource library of the system changes
Const int WM_FONTCHANGE = 0x1D;
// Send this message to all top-level windows when the system time changes
Const int WM_TIMECHANGE = 0x1E;
// Send this message to cancel an ongoing touch (Operation)
Const int WM_CANCELMODE = 0x1F;
// If the mouse causes the cursor to move in a window and the mouse input is not captured, a message is sent to a window.
Const int WM_SETCURSOR = 0x20;
// When the cursor is in an inactive window and the user is pressing the mouse key to send this message to the current window
Const int WM_MOUSEACTIVATE = 0x21;
// Send this message to the MDI subwindow // when the user clicks the title bar of this window, or // when the window is activated, moved, and changed
Const int WM_CHILDACTIVATE = 0x22;
// This message is sent by a computer-based training program. The user input message is separated by the WH_JOURNALPALYBACK hook program.
Const int WM_QUEUESYNC = 0x23;
// This message is sent to the window when it is about to change the size or position
Const int WM_GETMINMAXINFO = 0x24;
// Send it to the minimized window when its icon is to be repainted
Const int WM_PAINTICON = 0x26;
// This message is sent to a minimal window, only // The background must be repainted before it is painted.
Const int WM_ICONERASEBKGND = 0x27;
// Send this message to a dialog box program to change the focus position
Const int WM_NEXTDLGCTL = 0x28;
// Send this message whenever a job is added or removed in the print management queue
Const int WM_SPOOLERSTATUS = 0x2A;
// Send a message when the visual appearance of button, combobox, listbox, and menu changes
Const int WM_DRAWITEM = 0x2B;
// When button, combo box, list box, list view control, or menu item is created
Const int WM_MEASUREITEM = 0x2C;
// This message has an LBS_WANTKEYBOARDINPUT style that is sent to its owner to respond to the WM_KEYDOWN message.
Const int WM_VKEYTOITEM = 0x2E;
// This message is sent to its owner by a list box in the LBS_WANTKEYBOARDINPUT style to respond to the WM_CHAR message.
Const int WM_CHARTOITEM = 0x2F;
// When drawing text, the program sends this message to obtain the color used by the Control
Const int WM_SETFONT = 0x30;
// The application sends this message to obtain the font of the text drawn by the current control.
Const int WM_GETFONT = 0x31;
// The application sends this message to connect a window to a hotkey.
Const int WM_SETHOTKEY = 0x32;
// The application sends this message to determine whether the hotkey is associated with a window.
Const int WM_GETHOTKEY = 0x33;
// The message is sent to the minimal window. When the window is to be dragged and its class does not have a defined icon, the application can return an icon or a cursor handle, this icon or cursor is displayed when you drag and drop an icon.
Const int WM_QUERYDRAGICON = 0x37;
// Send this message to determine the relative position of the newly added items in combobox or listbox
Const int WM_COMPAREITEM = 0x39;
// The Display memory is very small.
Const int WM_COMPACTING = 0x41;
// When the size and position of the window to which the message is sent are about to be changed, call the setwindowpos function or other window management functions.
Const int WM_WINDOWPOSCHANGING = 0x46;
// Call the setwindowpos function or other window management functions when the size and position of the window to which the message is sent have been changed.
Const int WM_WINDOWPOSCHANGED = 0x47;
// Send this message when the system is about to pause
Const int WM_POWER = 0x48;
// Send this message when one application transmits data to another application
Const int WM_COPYDATA = 0x4A;
// When a user cancels the activation status of the program log, submit the message to the program
Const int WM_CANCELJOURNA = 0x4B;
// When an event of a control has occurred or the control needs to obtain some information, send the message to its parent window
Const int WM_NOTIFY = 0x4E;
// When the user selects an input language or the hotkey of the input language changes
Const int WM_INPUTLANGCHANGEREQUEST = 0x50;
// Send this message to the affected top-level window when the platform site has been changed
Const int WM_INPUTLANGCHANGE = 0x51;
// Send this message to the application when the program has initialized the windows Help routine
Const int WM_TCARD = 0x52;
// The message indicates that the user presses F1. If a menu is activated, the user sends the message to the menu associated with the window; otherwise, the message is sent to the window with focus, if no focus exists at the moment, send the message to the // currently activated window.
Const int WM_HELP = 0x53;
// Send this message to all windows after the user has logged on or exited. // The system updates the user's specific settings when the user logs on or exits, the system sends this message immediately when the user updates the settings.
Const int WM_USERCHANGED = 0x54;
// Public controls, custom controls, and their parent Windows use this message to determine whether the controls use the ANSI or UNICODE Structure
Const int wm_policyformat = 0x55;
// Right-click a user in a window and send the message to this window.
// Const int WM_CONTEXTMENU = ??;
// Send this message to the window when the SETWINDOWLONG function is called to change the style of one or more windows
Const int WM_STYLECHANGING = 0x7C;
// Send this message to the window when you call the SETWINDOWLONG function in one or more window styles
Const int WM_STYLECHANGED = 0x7D;
// Send this message to all windows when the resolution of the monitor changes
Const int WM_DISPLAYCHANGE = 0x7E;
// This message is sent to a window to return the handle of the large icon or small icon associated with a window.
Const int WM_GETICON = 0x7F;
// The program sends this message to associate a new large icon or small icon with a window
Const int WM_SETICON = 0x80;
// When a window is created for the first time, the message is sent before the WM_CREATE message is sent.
Const int WM_NCCREATE = 0x81;
// This message notifies a window in which a non-customer zone is being destroyed
Const int WM_NCDESTROY = 0x82;
// Send this message when the customer region of a window must be calculated
Const int WM_NCCALCSIZE = 0x83;
// When you move the mouse, hold it down or release it
Const int WM_NCHITTEST = 0x84;
// The program sends this message to a window. When the frame of the window must be drawn
Const int WM_NCPAINT = 0x85;
// This message is sent to a window only when its non-customer zone needs to be changed to show whether the message is active or not.
Const int WM_NCACTIVATE = 0x86;
// Send this message to a control associated with the dialog box program. The widdows control orientation key and TAB key enable the input to enter the control through the corresponding
Const int WM_GETDLGCODE = 0x87;
// When the cursor moves in a non-customer area of a window, the message is sent to the non-customer area of the window: The title bar of the form and the border body of the window.
Const int WM_NCMOUSEMOVE = 0xA0;
// This message is submitted when the cursor is pressed with the left mouse button in a non-customer area of a window.
Const int WM_NCLBUTTONDOWN = 0xA1;
// When the user releases the left mouse button and a window with the cursor sends this message in non-customer Zone 10
Const int WM_NCLBUTTONUP = 0xA2;
// When you double-click the left mouse button and a window with the cursor sends this message in non-customer Zone 10
Const int WM_NCLBUTTONDBLCLK = 0xA3;
// This message is sent when you right-click the user and the cursor is in a non-customer area of the window.
Const int WM_NCRBUTTONDOWN = 0xA4;
// This message is sent when the user releases the right mouse and the cursor is in a non-customer area of the window.
Const int WM_NCRBUTTONUP = 0xA5;
// When you double-click a user and right-click the mouse, a window with the cursor sends the message in non-customer Zone 10
Const int WM_NCRBUTTONDBLCLK = 0xA6;
// Send this message when you press the middle mouse button and the cursor is in a non-customer area of the window
Const int WM_NCMBUTTONDOWN = 0xA7;
// This message is sent when the user releases the middle mouse button and the cursor is in a non-customer area of the window.
Const int WM_NCMBUTTONUP = 0xA8;
// This message is sent when you double-click the middle mouse button and the cursor is in a non-customer area of the window.
Const int WM_NCMBUTTONDBLCLK = 0xA9;
// Press the next key for WM_KEYDOWN
Const int WM_KEYDOWN = 0x0100;
// Release a key
Const int WM_KEYUP = 0x0101;
// Press a key and the WM_KEYDOWN and WM_KEYUP messages have been sent.
Const int WM_CHAR = 0x102;
// Send the message to the window with focus when you use the translatemessage function to translate the WM_KEYUP message
Const int WM_DEADCHAR = 0x103;
// When you press the ALT key and other keys, submit the message to the window with focus
Const int WM_SYSKEYDOWN = 0x104;
// When you release a key and press the ALT key, submit the message to the window with focus
Const intwm_syskeyup = 0x105;
// When the WM_SYSKEYDOWN message is translated by the TRANSLATEMESSAGE function, submit the message to the window with focus
Const int WM_SYSCHAR = 0x106;
// When the WM_SYSKEYDOWN message is translated by the TRANSLATEMESSAGE function, send this message to the window with focus
Const int WM_SYSDEADCHAR = 0x107;
// Send this message to a dialog box program before it is displayed. This message is usually used to initialize the control and execute other tasks.
Const int WM_INITDIALOG = 0x110;
// When you select a menu command item or when a control sends a message to its parent window, a shortcut key is translated
Const int WM_COMMAND = 0x111;
// When the user selects a command in the window menu or // when the user selects maximize or minimize, the window will receive this message
Const int WM_SYSCOMMAND = 0x112;
// A timer event occurs.
Const int WM_TIMER = 0x113;
// When a standard horizontal scroll bar of a window generates a rolling event, this message is sent to that window and the control that owns it.
Const int WM_HSCROLL = 0x114;
// When a standard vertical scroll bar of a window generates a rolling event, this message is sent to that window and the control that owns it.
Const int WM_VSCROLL = 0x115;
// Send this message when a menu is to be activated. It occurs in a user menu bar or presses a menu key, which allows the program to change the menu before display.
Const int WM_INITMENU = 0x116;
// Send this message when a drop-down menu or sub-menu is to be activated. It allows the program to change the menu before it is displayed, instead of changing all
Const int WM_INITMENUPOPUP = 0x117;
// Send this message to the menu owner (usually a window) when a menu item is selected)
Const int WM_MENUSELECT = 0x11F;
// When the menu has been activated, the user presses a key (different from the acceleration key) and sends this message to the menu owner
Const int WM_MENUCHAR = 0x120;
// Send this message to its owner when a modal dialog box or menu enters the idle state, when a modal dialog box or menu enters the no-load status, it waits for no messages in the queue after processing one or more previous messages.
Const int WM_ENTERIDLE = 0x121;
// Send the message to the owner window of the message box before drawing a message box in windows, in the owner window, you can set the text and background color of the message box by using the given handle of the display device.
Const int WM_CTLCOLORMSGBOX = 0x132;
// Send this message to its parent window when an edit control is to be drawn, in the owner window, you can set the text and background color of the edit box by using the given handle of the display device.
Const int WM_CTLCOLOREDIT = 0x133;

// When a list box control sends this message to its parent window before being drawn, it responds to this message, in the owner window, you can set the text and background colors of the list box by using the given handle of the display device.
Const int WM_CTLCOLORLISTBOX = 0x134;
// Send this message to its parent window when a button control is to be drawn by responding to this message, in the owner window, you can set the text and background color of the button by using the handle of the specified display device.
Const int WM_CTLCOLORBTN = 0x135;
// When a dialog box control sends this message to its parent window before being drawn, it responds to this message, in the owner window, you can set the text background color of the dialog box by using the corresponding display device handle.
Const int WM_CTLCOLORDLG = 0x136;
// Send this message to its parent window when a scroll bar control is to be drawn by responding to this message, in the owner window, you can set the background color of the scroll bar by using the corresponding display device handle.
Const int WM_CTLCOLORSCROLLBAR = 0x137;
// Send this message to its parent window when a static control is to be drawn by responding to this message, in the owner window, you can set the text and background color of the static control by using the given handle of the display device.
Const int WM_CTLCOLORSTATIC = 0x138;
// Send the message a control with focus when the mouse wheel turns
Const int WM_MOUSEWHEEL = 0x20A;
// Double-click the middle mouse button
Const int WM_MBUTTONDBLCLK = 0x209;
// Release the middle mouse button
Const int WM_MBUTTONUP = 0x208;
// When the mouse is moved, it is the same as WM_MOUSEFIRST.
Const int WM_MOUSEMOVE = 0x200;
// Press the left mouse button
Const int WM_LBUTTONDOWN = 0x201;
// Release the left mouse button
Const int WM_LBUTTONUP = 0x202;
// Double-click the left mouse button
Const int WM_LBUTTONDBLCLK = 0x203;
// Right-click
Const int WM_RBUTTONDOWN = 0x204;
// Right-click release
Const int WM_RBUTTONUP = 0x205;
// Right-click
Const int WM_RBUTTONDBLCLK = 0x206;
// Press the middle mouse button
Const int WM_MBUTTONDOWN = 0x207;

Const int WM_USER = 0x0400;
Const int MK_LBUTTON = 0x0001;
Const int MK_RBUTTON = 0x0002;
Const int MK_SHIFT = 0x0004;
Const int MK_CONTROL = 0x0008;
Const int MK_MBUTTON = 0x0010;
Constint MK_XBUTTON1 = 0x0020;
Const int MK_XBUTTON2 = 0x0040;

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.