11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13. Adding Special Features to Application<br />

Function CMainFrame::OnSendStr(…) is listed as follows, it demonstrates how message<br />

MSG_SENDSTRING is handled in “MsgRcv”. Like a normal WM_MESSAGE type message handler, this function<br />

has two parameters that are used to pass WPARAM and LPARAM. In the sample, WPARAM parameter is used to<br />

pass the window handle of “Sender”, from which we can obtain a CWnd type pointer and use it to send<br />

MSG_RECEIVED message back. After that the value obtained from LPARAM parameter is added to the list view.<br />

The following is this function:<br />

LONG CMainFrame::OnSendStr(UINT wParam, LONG lParam)<br />

{<br />

CWnd *pWnd;<br />

CString szText;<br />

pWnd=CWnd::FromHandle((HWND)wParam);<br />

ASSERT(pWnd != NULL);<br />

szText.Format("%d", (int)lParam);<br />

pWnd->PostMessage(g_uMsgReceived);<br />

CListCtrl &lc=((CMsgRcvView *)GetActiveView())->GetListCtrl();<br />

lc.InsertItem(lc.GetItemCount(), szText);<br />

}<br />

return (LONG)TRUE;<br />

On the “Sender” side, if the user presses “Send” button, we call function CWnd::FindWindow(…) to find<br />

the mainframe window of “MsgRcv”. If it exists, message MSG_SENDSTRING will be sent to it, with the<br />

WPARAM parameter set to the window handle of the dialog box and LPARAM parameter set to the number<br />

contained in the edit box:<br />

void CSenderDlg::OnButtonSend()<br />

{<br />

CWnd *pWnd;<br />

}<br />

UpdateData(TRUE);<br />

pWnd=CWnd::FindWindow(CLASS_NAME_RECIEVER, NULL);<br />

if(pWnd != NULL && ::IsWindow(pWnd->m_hWnd))<br />

{<br />

pWnd->PostMessage(g_uMsgSendStr, (WPARAM)GetSafeHwnd(), (LPARAM)m_nSent);<br />

}<br />

Message MSG_RECEIVED is mapped to function CSenderDlg::OnReceive(…). Upon receiving message<br />

MSG_RECEIVED, we simply increment the counter and display the new value in the dialog box:<br />

LONG CSenderDlg::OnReceive(UINT wParam, LONG lParam)<br />

{<br />

m_nCount++;<br />

UpdateData(FALSE);<br />

}<br />

return (LONG)TRUE;<br />

It is fun to play with the two applications, because they implement the simplest communication<br />

protocol: sending the message, replying with the acknowledgment. By using message communication, we<br />

can send only simple information (like integers) to another application. If we want to send complex data<br />

structure to other processes, we need to use other memory sharing techniques such as file mapping along<br />

with message sending.<br />

13.9 Z-Order<br />

Z-order represents the third dimension of a window besides its x and y position. Under Windows, a<br />

window can be placed before or after another window, and none of the two windows can have a same Z-<br />

order (This means if the two windows share a common area, one of them must be overlapped by the other).<br />

413

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!