返回窗口中指定參數(shù)ID的子元素的句柄,可以通過返回的句柄對窗口內(nèi)的子元素進(jìn)行操作。
根據(jù)繼承關(guān)系,有如下幾類:
1).CWindow::GetDlgItem
HWND GetDlgItem( int nID)const;
說明——
參數(shù) nID:接收消息的控件的標(biāo)識;
返回值:標(biāo)識所標(biāo)記的控制窗口句柄;
2).CWnd::GetDlgItem
CWnd* GetDlgItem ( int nID ) const;
void CWnd::GetDlgItem( int nID, HWND* phWnd ) const;
說明——
參數(shù) nID:接收消息的控件的標(biāo)識;
參數(shù)phWnd:子類窗口的指針;
返回值:標(biāo)識所標(biāo)記的控件(或子類窗口)的指針;
Example
// Uses GetDlgItem to return a pointer to a user interface control.
CEdit* pBoxOne;
pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
GotoDlgCtrl(pBoxOne);
3).Windows SDK
HWND GetDlgItem(
HWND hDlg, // handle to dialog box
int nIDDlgItem // control identifier
標(biāo)識含有控件的對話框。
指定將被檢索的控件標(biāo)識符。
返回值:如果函數(shù)調(diào)用成功則返回值為給定控件的窗口句柄。如果函數(shù)調(diào)用失敗,則返回值為NULL,表示為一個無效的對話框句柄或一個不存在的控件。若想獲得更多錯誤信息,請調(diào)用GetLastError函數(shù)。
備注:可以通過使用任何父子窗口對來使用GetDlgltem函數(shù),而不僅只是對話框。只要hDlg參數(shù)指定一個父窗口,且子窗口有一個獨(dú)立的標(biāo)識符(象CreateWindow中hMenu參數(shù)指定的或創(chuàng)建子窗口的CreateWindowEx指定的那樣),GetDlgltem就會返回一個有效的句柄到子窗口。
Windows CE:GetDlgltem函數(shù)只為對話框中的直接于控制工作,它不通過嵌套的對話框來搜尋。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;頭文件:winuser.h;庫文件:user32.lib。
多數(shù)以GetDlgitem(控件標(biāo)識符);來使用
IntPtr hwnd;
IntPtr hwnd1;
IntPtr hwnd2;
IntPtr hwnd3;
IntPtr hwnd4;
hwnd = FindWindow(null, "QQ2011");
hwnd1 = GetDlgItem(hwndQQ, 0);
hwnd2 = GetDlgItem(hwnd1, 0);
hwnd3 = GetDlgItem(hwnd2, 894);
SendMessage(hwnd3, 194, 0, this.txtInput.Text);
hwnd4 = GetDlgItem(hwnd1, 1);
SendMessage(hwnd4, 245, 0, Convert.ToString(0));/*
GetDlgItem Function
The GetDlgItemfunction retrieves a handle to a control in the specified dialog box.
Syntax
HWND GetDlgItem(
HWND hDlg, int nIDDlgItem);
Parameters
hDlg[in] Handle to the dialog box that contains the control. nIDDlgItem[in] Specifies the identifier of the control to be retrieved.
Return Value
If the function succeeds, the return value is the window handle of the specified control.
If the function fails, the return value is NULL, indicating an invalid dialog box handle or a nonexistent control. To get extended error information, call GetLastError.
Remarks
You can use the GetDlgItemfunction with any parent-child window pair, not just with dialog boxes. As long as the hDlgparameter specifies a parent window and the child window has a unique identifier (as specified by the hMenuparameter in the CreateWindowor CreateWindowExfunction that created the child window), GetDlgItemreturns a valid handle to the child window.