来源 visual c++开发范例,IDE:vs2010.
分为
a.server(接收消息,显示接收到的数值).
b.client(打开server进程,向server主窗体发送消息).
//得到主窗口
HWND CMyJC006ClientDlg::GetServerMainWnd()
{
HWND hWnd;
if(0==EnumWindows((WNDENUMPROC)EnumWndProc,(LPARAM)&hWnd)) //find
{
return hWnd;
}
return NULL;
}
//枚举窗口的回调函数
BOOL CALLBACK EnumWndProc(HWND hWnd,LPARAM lParam)
{
TCHAR szWndText[MAX_PATH];
unsigned long dwProcessID;
GetWindowThreadProcessId(hWnd,&dwProcessID);
if(dwProcessID==g_ProcessId)
{
if(NULL==::GetParent(hWnd)) //is top window
{
*(HWND*)lParam=hWnd;
return FALSE; //means 0
}
}
return TRUE;
}
void CMyJC006ClientDlg::OnBnClickedButtonSend()
{
// TODO: 在此添加控件通知处理程序代码
//handle --wnd
UpdateData(TRUE); //to var
HWND hWnd=GetServerMainWnd();
if(hWnd==NULL)
{
AfxMessageBox(_T("获取MyJC006Server窗口句柄失败!"));
((CButton*)GetDlgItem(IDC_BUTTON_OPEN))->EnableWindow(TRUE);
((CButton*)GetDlgItem(IDC_BUTTON_SEND))->EnableWindow(FALSE);
((CEdit*)GetDlgItem(IDC_EDIT_SENDTEXT))->SetReadOnly(TRUE);
return;
}
//进程ID-->主窗口句柄
CWnd *pWnd=CWnd::FromHandle (hWnd); //wonder 22,get句柄server窗体
pWnd->SendMessage (WM_TEXTMESSAGE,this->m_nTextMessage,0);
}
1