C # calling SendMessage usage

Source: Internet
Author: User

function function: This function sends the specified message to one or more windows. This function invokes the window program for the specified window until the window program finishes processing the message and returns. This function is one of the primary means of message passing between applications and 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 the message. If this parameter is Hwnd_broadcast, the message is sent to all top-level windows in the system, including invalid or invisible non-owned windows, overwritten windows, and pop-up windows, but messages are not sent to child windows.

MSG: Specifies the message being sent.

WParam: Specifies additional message-specific information.

Iparam: Specifies additional message-specific information.

Return value: The return value specifies the result of the message processing, depending on the message being sent.

Note: Applications that require hwnd_broadcast communication should use function RegisterWindowMessage to obtain a unique message for communication between applications.

If the specified window is created by the calling thread, the window program is called immediately as a subroutine. If the specified window is created by a different thread, the system switches to that thread and invokes 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 namespaces:
Using System.Runtime.InteropServices;

Then write the API reference part of the code into the class inside
[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, the second is used to distinguish the constant value of other messages, the third is usually a message-related constant value, or a window or control handle, the third parameter is optional parameters, some messages to, some do not need, For example, clicking does not require this parameter,
Other messages, such as mouse movement may need to add some mouse parameters here; the fourth is usually a pointer to the in-memory data. In C #, the message needs to be defined as the original 16 binary digits in the Windows system, such as the const int wm_lbutton = 0x201; Defines the left-click message for the mouse. Detailed values are at the end.

For example:

Const intBm_click =0xf5; IntPtr Maindhwnd= FindWindow (NULL,"QQ User Login");//get the handle of QQ landing box hovertree.comif(Maindhwnd! =IntPtr.Zero) {INTPTR Childhwnd= FindWindowEx (Maindhwnd, IntPtr.Zero,NULL,"Login");//get the handle to the button    if(Childhwnd! =IntPtr.Zero) {SendMessage (Childhwnd, Bm_click,0,0);//Send a click button message    }    Else{MessageBox.Show ("No child windows found"); }}Else{MessageBox.Show ("No window found");}

Wmsg parameter constant value:

Create a window
const int wm_create = 0x01;
Send when a window is broken
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;
One window gets focus
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;
Sets whether the window can be redrawn
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 corresponding window's text to the buffer
const int wm_gettext = 0x0D;
Gets the length of the text associated with a window (does not contain null characters)
const int wm_gettextlength = 0x0E;
Ask for a window to repaint itself
const int WM_PAINT = 0x0F;
Send a signal when a window or application is about to close
const int wm_close = 0x10;
When the user chooses the End dialog box or the program calls the ExitWindows function itself
const int wm_queryendsession = 0x11;
Used to end a program run
const int wm_quit = 0x12;
Sends this message to an icon when the user window restores the previous size position
const int wm_queryopen = 0x13;
When the window background must be erased (example when the window changes size)
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 issues a WM_QUERYENDSESSION message, the message is sent to the application informing it that the conversation is over
const int wm_endsession = 0x16;
When the hidden or displayed window is sent this message to this window
const int Wm_showwindow = 0x18;
Send this message to the application which window is active and which is inactive
const int WM_ACTIVATEAPP = 0x1C;
This message is sent to all top-level windows when the system's font repository changes
const int wm_fontchange = 0x1D;
Send this message to all top-level windows when the system's time changes
const int wm_timechange = 0x1E;
Send this message to cancel an in-progress state (operation)
const int wm_cancelmode = 0x1F;
If the mouse causes the cursor to move through 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 a key on the mouse to send the message to//the current window
const int wm_mouseactivate = 0x21;
Send this message to the MDI child window//When the user clicks on the title bar of this window, or//when the window is activated, moves, changes the size
const int wm_childactivate = 0x22;
This message is sent by a computer-based training program that separates the user input message from the Wh_journalpalyback hook program
const int wm_queuesync = 0x23;
This message is sent to the window when it is going to change size or position
const int wm_getminmaxinfo = 0x24;
Send to minimized window when its icon is going to be redrawn
const int Wm_painticon = 0X26;
This message is sent to a minimized window only//when it's background must be redrawn before drawing the icon
const int WM_ICONERASEBKGND = 0X27;
Send this message to a dialog box program to change the focus position
const int WM_NEXTDLGCTL = 0x28;
This message is emitted whenever a print management queue increases or decreases a job
const int wm_spoolerstatus = 0x2A;
Send when the visual appearance of the Button,combobox,listbox,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 a lbs_wantkeyboardinput style issued to its owner in response to the WM_KEYDOWN message
const int wm_vkeytoitem = 0x2e;
This message is sent by a lbs_wantkeyboardinput-style list box to his owner in response to the WM_CHAR message
const int wm_chartoitem = 0x2F;
The program sends this message when the text is drawn to get the color of the control to be used
const int wm_setfont = 0x30;
The application sends this message to get the font of the current control drawing text
const int wm_getfont = 0X31;
The application sends this message to have a window associated with a hotkey
const int wm_sethotkey = 0x32;
The application sends this message to determine if a hot key is associated with a window
const int wm_gethotkey = 0X33;
This message is sent to the minimized window, and when the window is about to be dragged and the icon is not defined in its class, the application can return an icon or a handle to the cursor, which is displayed when the user drags and puts the icon
const int Wm_querydragicon = 0X37;
Send this message to determine the relative position of the newly added item in the ComboBox or ListBox
const int wm_compareitem = 0x39;
Display memory is very low.
const int wm_compacting = 0x41;
Call the SetWindowPos function or other window management function when sending this message to the size and position of the window that will be changed
const int wm_windowposchanging = 0x46;
Call the SetWindowPos function or other window management function when sending this message to the size and position of the window that has been changed
const int wm_windowposchanged = 0x47;
This message is sent when the system is about to enter a paused state
const int wm_power = 0x48;
This message is sent when an application passes data to another application
const int wm_copydata = 0x4A;
When a user cancels the program Log activation state, the message is submitted to the program
const int Wm_canceljourna = 0x4B;
Sends this message to its parent window when an event for a control has already occurred or if the control needs to get some information
const int wm_notify = 0x4E;
When the user chooses 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;
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 it will be sent to the focus window, if//There is no focus, send this message//currently activated window
const int WM_HELP = 0X53;
When the user has logged in or exited to send this message to all windows,//when the user log in 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
const int wm_userchanged = 0x54;
Common controls, custom controls, and their parent window use this message to determine whether a control is using an ANSI or Unicode structure
const int wm_notifyformat = 0X55;
This message is sent to this window when the user clicks the right button in a window
const int wm_contextmenu =??;
This message is sent to the window when the call to the SetWindowLong function will change the style of one or more windows
const int wm_stylechanging = 0x7C;
When you call the SetWindowLong function one or more window styles, send this message to that window
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 a handle to a large icon or small icon attached to a window
const int Wm_geticon = 0x7F;
The program sends this message to have a new large icon or small icon associated with a window
const int Wm_seticon = 0x80;
When a window is first created, this message is sent before the WM_CREATE message is sent
const int wm_nccreate = 0X81;
This message notifies a window that the non-client area is being destroyed
const int Wm_ncdestroy = 0x82;
This message is sent when the customer area of a window must be accounted for
const int wm_nccalcsize = 0x83;
Occurs when you move the mouse, hold or release the mouse
const int wm_nchittest = 0x84;
The program sends this message to a window when it (the window) 's frame must be drawn
const int wm_ncpaint = 0X85;
This message is sent to a window only if its non-client area needs to be changed to show whether it is active or inactive
const int wm_ncactivate = 0x86;
Send this message to a control associated with a dialog box program, widdows the control azimuth key and TAB key to enter this control through the
const int wm_getdlgcode = 0x87;
Send this message to the non-client area of the window when the cursor is moved within the non-client area of a window: the title bar of the form and the border body of the window
const int wm_ncmousemove = 0xA0;
Submits this message when the cursor presses the left mouse button at the same time in the non-client area of a window
const int wm_nclbuttondown = 0XA1;
This message is sent when the user releases the left mouse button while the cursor is in a window in non-client area 10
const int wm_nclbuttonup = 0XA2;
This message is sent when the user double-clicks the left mouse button while the cursor is in a non-client area 10
const int WM_NCLBUTTONDBLCLK = 0XA3;
This message is sent when the user presses the right mouse button while the cursor is in the non-client area of the window
const int wm_ncrbuttondown = 0XA4;
This message is sent when the user releases the right mouse button while the cursor is in the non-client area of the window
const int wm_ncrbuttonup = 0XA5;
This message is sent when the user double-clicks the right mouse button while the cursor is in the non-client area 10
const int WM_NCRBUTTONDBLCLK = 0XA6;
This message is sent when the user presses the middle mouse button while the cursor is in the non-client area of the window
const int wm_ncmbuttondown = 0XA7;
This message is sent when the user releases the middle mouse button while the cursor is in the non-client area of the window
const int wm_ncmbuttonup = 0XA8;
This message is sent when the user double-clicks the middle mouse button while the cursor is in the non-client area of the window
const int WM_NCMBUTTONDBLCLK = 0XA9;
Wm_keydown Press a key
const int WM_KEYDOWN = 0X0100;
Release a key
const int WM_KEYUP = 0X0101;
Press a key and have issued WM_KEYDOWN, WM_KEYUP message
const int WM_CHAR = 0x102;
Send this message to the window with focus when translating wm_keyup messages with the TranslateMessage function
const int WM_DEADCHAR = 0x103;
Submits this message to the window that has focus when the user presses the ALT key while pressing another key
const int wm_syskeydown = 0x104;
Submits this message to the window that has focus when the user releases a key while the ALT key is still pressed
const int wm_syskeyup = 0x105;
When the Wm_syskeydown message is translated by the TranslateMessage function, the message is submitted to the window that has the focus
const int WM_SYSCHAR = 0x106;
Send this message to the window with focus when the Wm_syskeydown message is translated by the TranslateMessage function
const int WM_SYSDEADCHAR = 0x107;
Sends this message to a dialog box before it is displayed, typically using this message to initialize the control and perform other tasks
const int wm_initdialog = 0x110;
When a user selects 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;
The window receives this message when the user chooses a command for the Window menu or//when the user chooses to maximize or minimize
const int wm_syscommand = 0x112;
A timer event has occurred
const int wm_timer = 0x113;
When a window standard horizontal scrollbar produces a scrolling event, this message is sent to that window and also to the control that owns it
const int wm_hscroll = 0x114;
When a window standard vertical scrollbar produces a scrolling event, this message is sent to that window, which is also sent to the control that owns it
const int wm_vscroll = 0x115;
This message is sent when a menu is about to be activated, it occurs on an item in the user's menu bar, or a menu key is pressed, which allows the program to change the menu before it is displayed
const int wm_initmenu = 0x116;
This message is sent when a drop-down menu or submenu is about to be activated, allowing the program to change the menu before it is displayed, without changing all
const int wm_initmenupopup = 0x117;
Send this message to the owner of the menu (typically a window) when the user selects a menu item
const int wm_menuselect = 0x11f;
When the menu has been activated the user presses a key (unlike the accelerator key) and sends this message to the owner of the menu
const int Wm_menuchar = 0x120;
Sends this message to its owner when a modal dialog box or menu enters an empty state, and a modal dialog box or menu enters an empty state, which waits in the queue without a message after processing one or several previous messages
const int wm_enteridle = 0x121;
Send this message to the owner window of the message box before Windows draws the message box, and in response to this message, the owner window can set the text and background color of the message box by using the handle of the given related display device
const int wm_ctlcolormsgbox = 0x132;
When an edit control is about to be drawn when this message is sent to its parent window by responding to this message, the owner window can set the text and background color of the edit box by using the handle of the given related display device
const int wm_ctlcoloredit = 0x133;

When a ListBox control is going to be drawn before sending this message to its parent window by responding to this message, the owner window can set the text and background color of the list box by using the handle of the given related display device
const int wm_ctlcolorlistbox = 0x134;
When a button control is about to be drawn when this message is sent to its parent window by responding to this message, the owner window can set the button's text and background color by using the handle of the given related display device
const int WM_CTLCOLORBTN = 0x135;
When a dialog box control is going to be drawn before sending this message to its parent window by responding to this message, the owner window can set the text background color of the dialog box by using the handle of the given related display device
const int WM_CTLCOLORDLG = 0x136;
When a scrollbar control is about to be drawn when this message is sent to its parent window by responding to this message, the owner window can set the background color of the scroll bar by using the handle of the given related display device
const int wm_ctlcolorscrollbar = 0x137;
When a static control is about to be drawn when this message is sent to its parent window by responding to this message, the owner window can set the text and background color of the static control by using the handle of the given related display device
const int wm_ctlcolorstatic = 0x138;
Sends this message when the mouse wheel is rotated a control that currently has focus
const int wm_mousewheel = 0X20A;
Double-click the middle mouse button
const int WM_MBUTTONDBLCLK = 0x209;
Release middle mouse button
const int wm_mbuttonup = 0x208;
Occurs when moving the mouse, same as Wm_mousefirst
const int wm_mousemove = 0x200;
Press the left mouse button
const int wm_lbuttondown = 0x201;
Release left mouse button
const int wm_lbuttonup = 0x202;
Double click left mouse button
const int WM_LBUTTONDBLCLK = 0x203;
Press the right mouse button
const int wm_rbuttondown = 0x204;
Release the right mouse button
const int wm_rbuttonup = 0x205;
Double-click the right mouse button
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;
const int mk_xbutton1 = 0x0020;
const int mk_xbutton2 = 0x0040;

http://www.cnblogs.com/sosoft/

C # calling SendMessage usage

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.