Creating a Foundation Classes Dialog Box with Custom IconID: Q87976
|
Using the Microsoft Foundation Classes supplied with Microsoft C/C++
versions 7.0 and Microsoft Visual C++, you can create a main application
window based on a dialog box. However if a dialog is used as the
application's main window, and the dialog is created with a minimize
button and then minimized, the default application icon will be displayed.
This article briefly describes two approaches that can be taken to change
the default icon to a custom (application-defined) icon.
Starting with Visual C++ 4.0, AppWizard supports creation of dialog based
applications that have custom icons. The method described in this article
works with Visual C++ 4.0, but it is preferred to use the AppWizard
generated code.
The CFormView class is a useful alternative to the "dialog as main window
technique" when using Microsoft Foundation Classes, and provides additional
features. For more information on CFormView, see the documentation supplied
with Microsoft Visual C++, the VIEWEX and CHKBOOK samples, and query on the
following words in the Microsoft Knowledge Base:
CFormViewA discussion of CFormView based applications is beyond the scope of this article.
One technique for using a dialog as the main window is documented in the
"Class Libraries User's Guide" which shipped with Microsoft C/C++ version
7.0 on page 334, and in both versions via the TRACER sample application.
Neither of these techniques illustrates how to change the default icon to
a custom icon.
The first technique below, illustrated with code fragments, shows the
modifications required to allow the TRACER sample to display a custom icon
when minimized. This is the simplest approach to the problem.
The second technique is illustrated using a sample from the Software/Data
Library. While more complex, it illustrates how to use the Windows
::RegisterClass() API to register a class with an application defined icon.
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HCURSOR OnQueryDragIcon();
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_QUERYDRAGICON()
void CPromptDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
// Erase the icon background when placed over other app window
DefWindowProc(WM_ICONERASEBKGND, (WORD)dc.m_hDC, 0L);
// Center the icon
CRect rc;
GetClientRect(&rc);
rc.left = (rc.right - ::GetSystemMetrics(SM_CXICON)) >> 1;
rc.top = (rc.bottom - ::GetSystemMetrics(SM_CYICON)) >> 1;
// Draw the icon
HICON hIcon = AfxGetApp()->LoadIcon(AFX_IDI_STD_FRAME);
dc.DrawIcon(rc.left, rc.top, hIcon);
}
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CPromptDlg::OnEraseBkgnd(CDC* pDC)
{
if (IsIconic())
return TRUE;
else
return CDialog::OnEraseBkgnd(pDC);
}
HCURSOR CPromptDlg::OnQueryDragIcon()
{
return AfxGetApp()->LoadIcon(AFX_IDI_STD_FRAME);
}
In this example, AFX_IDI_STD_FRAME is the ID of an icon contained in
the application's resource file, and the dialog template contains the
WS_MINIMIZEBOX style.private and dialog and classTo register a private dialog class with a custom icon, do the following three steps:
Additional query words: kbinf 7.00 1.00 1.50 2.00 2.50 2.50 2.51 2.52
Keywords : kbMFC KbUIDesign kbVC
Version : 7.00 | 1.00 1.50 1.51 1.52 | 1
Platform : MS-DOS NT WINDOWS
Issue type :
Last Reviewed: July 26, 1999