API之FindWindowEx和SendMessage

最近在VC6.0开发中碰到了两个函数,经过一番搜索查阅,特记录于此。

FindWindowEx

// The FindWindowEx function retrieves a handle to a window
// whose class name and window name match the specified strings.
// The function searches child windows, beginning with the one following
// the specified child window. This function does not perform a case-sensitive search.
// 翻译:该函数获取一个指定类名和窗口标题字符串(大小写不敏感)都匹配的子窗口的句柄。
//
// HWND FindWindowEx(
//                     HWND hwndParent,             // handle to parent window父窗口句柄,如果为NULL,则指 桌面
//            HWND hwndChildAfter,  // handle to child window直系子窗口该窗口和此函数要查
//找的窗口
即返回的结果窗口是兄弟关系
//  
                  LPCTSTR lpszClass,            // class name类名,编辑框为EDIT,组合框为COMBOBOX等
//                     LPCTSTR lpszWindow       // window name标题名字字符串
//    )

CBN_SELCHANGE 

利用SendMessage向指定的组合框发送消息, 直接引用MSDN中关于CBN_SELCHANGE的描述:
The CBN_SELCHANGE notification message is sent when the user changes the current selection in the list box of a combo box. The user can change the selection by clicking in the list box or by using the arrow keys. The parent window of the combo box receives this notification in the form of a WM_COMMAND message with CBN_SELCHANGE in the high-order word of the wParam parameter.

CBN_SELCHANGE
idComboBox = (int) LOWORD(wParam); // identifier of combo box 
hwndComboBox = (HWND) lParam; // handle to combo box 

To get the index of the current selection, send the CB_GETCURSEL message to the control.
The CBN_SELCHANGE notification message is not sent when the current selection is set using the CB_SETCURSEL  message. 

也就是说,需要先发送CB_SETCURSEL消息后,再发送CBN_SELCHANGE消息才能响应事件,举例如下:
::SendMessage(hwndCombo, CB_SETCURSEL, 1, 0);
int id = ::GetDlgCtrlID(hwndCombo);
::SendMessage(hwndDialog, WM_COMMAND, MAKEWPARAM(id, CBN_SELCHANGE), (LPARAM)hwndCombo);
注意第二次发送消息时,第一个参数hwndDialog是父窗口的句柄,与组合框的句柄要区别开来,第三个参数用到了MAKEWPARM函数

你可能感兴趣的:(VC/MFC,C++,API,MSDN)