Send a message, modify the selection of another program's drop-down box

Source: Internet
Author: User

Send a message to modify the selection of the drop-down boxes for other programs.

First, the conclusion.

public void SelectItem (int index)

{

Comboboxinfo CBI = new Comboboxinfo ();

Cbi.cbsize = System.Runtime.InteropServices.Marshal.SizeOf (CBI);

if (User32.getcomboboxinfo (Wnd, ref. CBI))

{

User32.sendmessage (Wnd, Comboboxmsg.cb_setcursel, index, 0);

int cbId = User32.getdlgctrlid (cbi.hwndlist);

int wParam = Wordhelper.getword (Comboboxmsg.cbn_selchange, cbId);

User32.sendmessage (Wnd, CONSTANT_WM. Wm_command, WParam, (int) cbi.hwndlist);

}

}

The User32 is an encapsulated User32.dll inside the Static class of API functions, well, learn this aspect of people will have it.

The process of groping for a little time, I hope "yimeimei" search can be found here, record the process.

I used to learn a little bit about MFC programming, which is now in C #. It is assumed that these (like Getcomboboxinfo) are taken for granted to those who know it, and there is no need to write them out specifically.

1. first Baidu, quickly find information

ComboBox of Windows messages

Send a false message to the drop-down menu to select, but only the first sentence can not excite the selectchange, a lot of business logic coupled on this

So we're going to have to make a select change message .

PostMessage (Comboboxhwnd, Cb_setcursel, SelectItem, 0);

PostMessage (Targethwnd,wm_command, Makewparam (GetWindowLong (Comboboxhwnd, gwl_id), Cbn_selchange), (LPARAM) Comboboxhwnd);

And

C # sends a message through SendMessage To change the value of another program's drop-down box control (ComboBox)

intptr mwh =  (INTPTR) Convert.ToInt32 (handle);                           //combobox
int result =  SendMessage (MWH, 0X014D, -1, SELECTSTR);             // change combobox span style= value of "font-family: Arial", selectstr The expected drop-down box option

int mwh_p = GetWindowLong (MWh,-8); gets The handle of the window to which the ComboBox belongs
            
IntPtr mwh2 = (IntPtr) convert.toint32 (mwh_p); convert The handle of the window to which the ComboBox belongs

String Cbn_selchange;

int cb_id = GetWindowLong (MWh,-12); gets The control ID of the ComboBox

Cbn_selchange = "0001" + string. Format ("{0:x4}", cb_id);

INTPTR S1 = (IntPtr) convert.toint32 (Tools.tod (Cbn_selchange, 16));

sendmessage (MWH2, 0X0111, S1, MWH);                     // to combobox The owning window sends wm_command command, section 3 parameters wparam Yes ( cbn_selchange (high)   +  control id (low))

SendMessage (MWH2, 0x0111, S1, MWh); can refer to Spy + captured message log

the parameters of the GetWindowLong can be consulted:

Http://blog.csdn.net/hnhyhongmingjiang/archive/2008/03/06/2154410.aspx

the parameters of the SendMessage can be consulted:

Http://topic.csdn.net/t/20050713/18/4142641.html

I followed my own procedure and made a change.

ntptr mwh = Wnd;

User32. SendMessage (MWh, comboboxmsg. Cb_setcursel, index, 0);

int mwh_p = User32. GetWindowLong (MWh, Getwindowlongflag. Gwl_hwndparent);

IntPtr mwh2 = (IntPtr)Convert. ToInt32 (mwh_p);

string cbn_selchange;

nt cb_id = User32. GetWindowLong (MWh, Getwindowlongflag. GWL_ID);

int S1 = wordhelper. Getword (comboboxmsg. Cbn_selchange, cb_id);

User32. SendMessage (MWH2, constant_wm. WM_COMMAND, S1, (int) MWh);

The result fails , and the last message has no effect.

The first Cb_setcursel message only modifies the display of the drop-down box and does not trigger the Selectchange event, and the program does not change substantially.

The second message is to trigger an event.

2. Use Spy + + to view the message and discover

<00377> 000C0BFA S wm_command wnotifycode:0001 wid:1000 hwndctl:001b08e4

Found modified into

User32.sendmessage (MWh, CONSTANT_WM. WM_COMMAND, 110734);

A 110734 is a decimal number of 001b08e4

Have effect!

That's what I said earlier. SendMessage The first parameter is wrong, it should be the handle to the ComboBox itself, rather than the parent form, was also encountered when looking for toolbar to send a message click button.

3. Then in Spy + +, 001b08e4 is a Combolbox class whose parent is the desktop.

Find combolbox information and get:

Combolbox:the class for the list box contained in a combo box.

in Windows (, before a new type of window was created, it must register a special class name to the system. Every window has its own class name, which could is used to tell the window ' s type. in the case of combo box, its edit box's class name is "edit" and its list box's class name is "Combolbox". Please note the This is the class name has the nothing to does with MFC classes. It is used by the operating system to identify the window types rather than a programming implementation.

How the handle from the ComboBox to the Combolbox is stuck, the query Combolbox not get useful information, but there is a place where the handle is the same as the handle to the listbox.

"Get the handle of the listbox inside the combo" in Chinese "no, not with" get the handle to the listbox in the ComboBox.

4. found with "Get ListBox in ComboBox"! (Google is not connected, the wall does not know the convenience of now, Bing search)

win32:how do I get the listbox for a ComboBox

I ' m writing some code and I need to get the window handle of the listbox associated with a combo box. When looking in Spy + +, it looks like the parent of the listbox was the desktop, not the combo box. How can I programmatically find the listbox window handle?

Answer

Send Cb_getcomboboxinfo to the combo box, and it fills in a comboboxinfo structure, which contains the W Indow handles to the edit control and the list box control.

Next search Cb_getcomboboxinfo and Comboboxinfo, found the Send message and call User32 function Getcomboboxinfo Two methods, send the message not specifically researched, I use Getcomboboxinfo.

[DllImport ("User32.dll")]
public static extern bool Getcomboboxinfo (IntPtr hWnd, ref comboboxinfo PCBI);

[StructLayout (layoutkind.sequential)]
Public struct Comboboxinfo {
Public Int32 cbsize;
Public RECT RcItem;
Public RECT Rcbutton;
Public comboboxbuttonstate buttonstate;
Public IntPtr Hwndcombo;
Public IntPtr Hwndedit;
Public IntPtr hwndlist;
}

Public enum Comboboxbuttonstate {
state_system_none = 0,
state_system_invisible = 0x00008000,
state_system_pressed = 0x00000008
}

comboboxinfo cbi = new comboboxinfo ();
cbi.cbsize =  System.Runtime.InteropServices.Marshal.SizeOf (CBI);
if (Getcomboboxinfo ( COMBOBOX1.HANDLE,&NBSP;REF&NBSP;CBI))  {
    if (Cbi.hwndedit == intptr.zero)  {
         throw new exception ("combobox must have the dropdown style!");
&NBSP;&NBSP;&NBSP;&NBSP;} Span style= "Color:rgb (0,0,255)" >
}

Above is from PInvoke, PInvoke is really convenient, good want to have an offline version! (Working computer can't surf the internet!) )

Finally, the code that I tested successfully was the beginning of the code.

Send a message, modify the selection of another program's drop-down box

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.