发送消息(Sending a Message)
SendMessage函数被用来直接发送消息到一个窗口过程。SendMessage函数调用一个窗口过程并等待这个窗口过程处理消息然后返回一个结果。
一个消息可以被发送给系统中的任何窗口,只需要一个窗口句柄。系统利用这个句柄来决定哪个窗口过程应该接收这个消息。
在处理一个可能由其他线程发送的消息之前,一个窗口过程应该首先调用InSendMessage函数。如果这个函数返回TRUE,那么窗口过程应该在其他任何导致线程放弃控制的函数之前调用ReplyMessage函数,就像下面例子显示的一样:
case WM_USER + 5:
if (InSendMessage())
ReplyMessage(TRUE);
DialogBox(hInst, "MyDialogBox", hwndMain, (DLGPROC) MyDlgProc);
break;
许多消息可能会被发送给对话框的控件。这些控件消息设置控件的外观、行为和内容或者获取关于控件的信息。例如,CB_ADDSTRING消息可以添加一个字符串到一个组合框中,BM_SETCHECK消息可以设置复选框或单选按钮的状态。
利用SendDlgItemMessage函数发送一个消息到一个控件,指定这个控件的标识符和包含这个控件的对话框窗口的句柄。下面的例子,取自于一个对话框过程,从组合框的编辑控件中复制一个字符串到列表框。这个例子使用了SendDlgItemMessage函数来发送一个CB_ADDSTRING消息到组合框中。
HWND hwndCombo;
int cTxtLen;
PSTR pszMem;
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDD_ADDCBITEM:
// Get the handle of the combo box and the
// length of the string in the edit control
// of the combo box.
hwndCombo = GetDlgItem(hwndDlg, IDD_COMBO);
cTxtLen = GetWindowTextLength(hwndCombo);
// Allocate memory for the string and copy
// the string into the memory.
pszMem = (PSTR) VirtualAlloc((LPVOID) NULL,
(DWORD) (cTxtLen + 1),
MEM_COMMIT,
PAGE_READWRITE);
GetWindowText(hwndCombo, pszMem,cTxtLen + 1);
// Add the string to the list box of the
// combo box and remove the string from the
// edit control of the combo box.
if (*pszMem != NULL)
{
SendDlgItemMessage(hwndDlg, IDD_COMBO,
CB_ADDSTRING, 0,
(DWORD)((LPSTR) pszMem));
SetWindowText(hwndCombo, (LPSTR) NULL);
}
// Free the memory and return.
VirtualFree(pszMem, 0, MEM_RELEASE);
return TRUE;
//
// Process other dialog box commands.
//
}
//
// Process other dialog box messages.
//
}
SendMessage函数被用来直接发送消息到一个窗口过程。SendMessage函数调用一个窗口过程并等待这个窗口过程处理消息然后返回一个结果。
一个消息可以被发送给系统中的任何窗口,只需要一个窗口句柄。系统利用这个句柄来决定哪个窗口过程应该接收这个消息。
在处理一个可能由其他线程发送的消息之前,一个窗口过程应该首先调用InSendMessage函数。如果这个函数返回TRUE,那么窗口过程应该在其他任何导致线程放弃控制的函数之前调用ReplyMessage函数,就像下面例子显示的一样:
case WM_USER + 5:
if (InSendMessage())
ReplyMessage(TRUE);
DialogBox(hInst, "MyDialogBox", hwndMain, (DLGPROC) MyDlgProc);
break;
许多消息可能会被发送给对话框的控件。这些控件消息设置控件的外观、行为和内容或者获取关于控件的信息。例如,CB_ADDSTRING消息可以添加一个字符串到一个组合框中,BM_SETCHECK消息可以设置复选框或单选按钮的状态。
利用SendDlgItemMessage函数发送一个消息到一个控件,指定这个控件的标识符和包含这个控件的对话框窗口的句柄。下面的例子,取自于一个对话框过程,从组合框的编辑控件中复制一个字符串到列表框。这个例子使用了SendDlgItemMessage函数来发送一个CB_ADDSTRING消息到组合框中。
HWND hwndCombo;
int cTxtLen;
PSTR pszMem;
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDD_ADDCBITEM:
// Get the handle of the combo box and the
// length of the string in the edit control
// of the combo box.
hwndCombo = GetDlgItem(hwndDlg, IDD_COMBO);
cTxtLen = GetWindowTextLength(hwndCombo);
// Allocate memory for the string and copy
// the string into the memory.
pszMem = (PSTR) VirtualAlloc((LPVOID) NULL,
(DWORD) (cTxtLen + 1),
MEM_COMMIT,
PAGE_READWRITE);
GetWindowText(hwndCombo, pszMem,cTxtLen + 1);
// Add the string to the list box of the
// combo box and remove the string from the
// edit control of the combo box.
if (*pszMem != NULL)
{
SendDlgItemMessage(hwndDlg, IDD_COMBO,
CB_ADDSTRING, 0,
(DWORD)((LPSTR) pszMem));
SetWindowText(hwndCombo, (LPSTR) NULL);
}
// Free the memory and return.
VirtualFree(pszMem, 0, MEM_RELEASE);
return TRUE;
//
// Process other dialog box commands.
//
}
//
// Process other dialog box messages.
//
}