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 />

GetLastActivePopup(), which will find out the most recently activated pop-up window. 2)<br />

::ShowWindow(…), which will restore the original state of the window being minimized (parameter<br />

SW_RESTORE can be used for this purpose). 3) CWnd::SetForegroundWindw(), which will bring the child<br />

window to foreground if there is a such kind of window. Steps 1) and 3) are necessary here because a<br />

mainframe window may own some pop up windows (For example, a dialog box implemented by an SDI or<br />

MDI application). Once the mainframe window is brought to the top, we also need to bring its child pop up<br />

window to foreground.<br />

After this is done, we need to return a FALSE value, which indicates that the procedure of creating<br />

mainframe window, document and view is not successful. This will cause the application to exit.<br />

If no window with the same class name is found, we can proceed to register our own window class.<br />

This can be done by stuffing a WNDCLASS type object and passing the object address to function<br />

AfxRegisterClass(…), whose only parameter is a WNDCLASS type pointer:<br />

BOOL AFXAPI AfxRegisterClass(WNDCLASS *lpWndClass);<br />

In order to make sure that our application is the same with those implemented by default <strong>MFC</strong> window<br />

classes, we must stuff the class with appropriate values. Here is how this is done in the sample:<br />

……<br />

……<br />

memset(&wndcls, 0, sizeof(WNDCLASS));<br />

wndcls.style=CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;<br />

wndcls.lpfnWndProc=::DefWindowProc;<br />

wndcls.hInstance=AfxGetInstanceHandle();<br />

wndcls.hIcon=LoadIcon(IDR_MAINFRAME);<br />

wndcls.hCursor=LoadCursor(IDC_ARROW);<br />

wndcls.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);<br />

wndcls.lpszMenuName=NULL;<br />

wndcls.lpszClassName=ONCE_CLASSNAME;<br />

if(!AfxRegisterClass(&wndcls))<br />

{<br />

TRACE("Class Registration Failed\n");<br />

return FALSE;<br />

}<br />

The class name string is defined using ONCE_CLASSNAME macro. Of course, when we override function<br />

CMainFrame::OnPreCreateWindow(…), we need to replace the default class name with it. The following<br />

code fragment shows how this function is overridden in the sample:<br />

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

{<br />

cs.lpszClass=ONCE_CLASSNAME;<br />

return CMDIFrameWnd::PreCreateWindow(cs);<br />

}<br />

Before the application exits, we must unregister the window class if it has been registered successfully.<br />

For this purpose, a Boolean type variable m_bRegistered is declared in class COnceApp, which will be set to<br />

TRUE after the class is registered successfully. When overriding function CWinApp::ExitInstance()<br />

(which should be overridden if we want to do some cleanup job before the application exits), we need to<br />

unregister the window class name if the value of m_bRegistered is TRUE. The following is the overridden<br />

function implemented in the sample:<br />

int COnceApp::ExitInstance()<br />

{<br />

if(m_bRegistered == TRUE)<br />

{<br />

::UnregisterClass(ONCE_CLASSNAME, AfxGetInstanceHandle());<br />

}<br />

return CWinApp::ExitInstance();<br />

}<br />

394

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

Saved successfully!

Ooh no, something went wrong!