11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16. Context Sensitive Help<br />

……<br />

}<br />

}<br />

if(::DdeNameService(m_dwInst, m_hszServiceName, NULL, DNS_REGISTER))<br />

{<br />

szRlt="DDE initialized successfully!";<br />

}<br />

else szRlt="Fail to initialize DDE: Fail to register name service!";<br />

break;<br />

Printf((LPCSTR)szRlt);<br />

Printf("\r\n");<br />

Here CMainFrame::m_dwInst is a DWORD type static variable and CMainFram::DdeCallback(…) is a<br />

static member function. This is because a callback function must be either a global function or a static<br />

member function. Function CMainFrame::Hszize() can be used to obtain a string handle for the name<br />

service and store the handle in static variable CMainFrame::m_hszServiceName. When the application exits,<br />

the name service is unregistered and also, the DDE is uninitialized there. This is implemented in WM_CLOSE<br />

message handler:<br />

void CMainFrame::OnClose()<br />

{<br />

::DdeNameService(m_dwInst, NULL, NULL, DNS_UNREGISTER);<br />

UnHszize();<br />

::DdeUninitialize(m_dwInst);<br />

}<br />

CFrameWnd::OnClose();<br />

Also, the string handle is freed here. In the sample, functions CMainFrame::Hszize() and<br />

CMainFrame:: UnHszize() are implemented for obtaining and freeing string handles respectively:<br />

void CMainFrame::Hszize()<br />

{<br />

m_hszServiceName=::DdeCreateStringHandle(m_dwInst, m_szService, NULL);<br />

Printf("Service name: %s\r\n", m_szService);<br />

}<br />

void CMainFrame::UnHszize()<br />

{<br />

::DdeFreeStringHandle(m_dwInst, m_hszServiceName);<br />

}<br />

Here CMainFrame::m_szService is a static variable.<br />

Monitoring DDE Activities<br />

To monitor the activities of the server, function CMainFrame::Printf(…) is implemented in the<br />

sample. This function imitates the well-known function prinf(…), and outputs a formatted string to the<br />

client window. We can use it exactly the same way as we use function printf(…). Since the view of the<br />

sample is based on class CEditView, it is easy for us to display text in the client window. Since the client<br />

window is solely used for displaying information, we need to set the window’s ES_READONLY style before<br />

the it is created:<br />

BOOL CDDESrvView::PreCreateWindow(CREATESTRUCT& cs)<br />

{<br />

BOOL bPreCreated = CEditView::PreCreateWindow(cs);<br />

cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);<br />

cs.style |= ES_READONLY;<br />

}<br />

return bPreCreated;<br />

Because function CEditCtrl::ReplaceSel(…) is used to output text to the edit view in function<br />

CMainFrame::Printf(…), we further need to prevent the cursor position from being changed by the user<br />

460

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

Saved successfully!

Ooh no, something went wrong!