在建基于对话框的程序时,向导会产生这样的代码:
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
请问:if (IsIconic())里面的代码什么时候执行??? 看向导注释,好像是当对话框最小化时才执行,可是我给标题栏加上了MinimizeBox,最小化时也不执行呀,怎么让if (IsIconic())里面的代码执行???????
向导注释如下:
If you add a minimize button to your dialog, you will need the code below to draw the icon. For MFC applications using the document/view model, this is automatically done for you by the framework.
里面的代码的确应该是在对话框最小化时运行的,
不过通常对话框最小化的时候,不会有WM_PAINT消息发送,自然平时也就看不到执行了。
要让里面的代码执行,很简单,在对话框中加个按钮,在其Click时执行以下代码:
ShowWindow( SW_SHOWMINIMIZED );
SendMessage( WM_PAINT );