1、关闭模态对话框:EndDialog
关闭非模态对话框:DestroyWindow
2、为对话框添加最大化和最小化按钮
ModifyStyle(0,WS_MAXIMIZEBOX);
ModifyStyle(0,WS_MINIMIZEBOX);
3、使基于对话框的程序不在任务栏中显示
BOOL CDemoApp::InitInstance()
{
CFrameWnd* pWnd = new CFrameWnd();
pWnd->Create(NULL,NULL);
CDemoDlg dlg(pWnd);
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
delete pWnd;
return FALSE;
}
BOOL CDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ModifyStyleEx(WS_EX_APPWINDOW,0);
return 0;
}
4、不显示对话框
afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);
WM_WINDOWPOSCHANGING
void CDemoDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
lpwndpos->flags &= ~SWP_SHOWWINDOW;
CDialog::OnWindowPosChanging(lqwndpos);
}
5、加载菜单
m_Menu.LoadMenu(IDR_MENU);
SetMenu(&m_Menu);
6、对话框加载工具栏
if (!m_wndToolBar.Create(this))
{
return FALSE;
}
if (!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
return FALSE;
}
CRect rcOldClient;
GetClientRect(rcOldClient);
CRect rcNewClient;
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
0,reposQuery,rcNewClient);
CRect rcMain;
GetWindowRect(rcMain);
rcMain.right += rcOldClient.Width() - rcNewClient.Width();
rcMain.bottom += rcOldClient.Height() - rcNewClient.Height();
MoveWindow(rcMain,FALSE);
CRect rcChild;
CPoint point(rcNewClient.left - rcOldClient.left,rcNewClient.top - rcOldClient.top);
CWnd* pChildWnd = GetWindow(GW_CHILD);
while (pChildWnd != NULL)
{
pChildWnd->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(point);
pChildWnd->MoveWindow(rcChild,FALSE);
pChildWnd = pChildWnd->GetNextWindow();
}
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
7、在对话框中加入视图View
void CDemoView::OnDraw(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut(rect.Width()/2,rect.Height()/2,"Hello World!");
// TODO: add draw code here
}
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CDemoView);
context.m_pCurrentDoc = NULL;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = (CFrameWnd*)this;
m_pView = (CDemoView*)context.m_pNewViewClass->CreateObject();
if (!m_pView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0),this,AFX_IDW_PANE_FIRST,&context))
{
return FALSE;
}
CRect rect;
GetClientRect(rect);
m_pView->MoveWindow(rect);
void CViewTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_pView != NULL)
{
CRect rect;
GetClientRect(rect);
m_pView->MoveWindow(rect);
}
}
8、窗口停靠
afx_msg void OnWindowPosChanging(WINDOWPOS FAR* lpwndpos);
ON_WM_WINDOWPOSCHANGING()
void CDockTestDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CDialog::OnWindowPosChanging(lpwndpos);
CRect rect;
SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
if (abs(lpwndpos->x - rect.left) <= 50)
{
lpwndpos->x = rect.left;
lpwndpos->cy - rect.Height();
}
CRect temprect;
GetWindowRect(&temprect);
if (abs(lpwndpos->x + temprect.Width() - rect.right)<= 50)
{
lpwndpos->x = rect.right - temprect.Width();
}
else if (abs(lpwndpos->y) <= 50)
{
lpwndpos->y = 0;
}
else if (abs(lpwndpos->y + temprect.Height() - rect.Height()) <= 50)
{
lpwndpos->y = rect.Height() - temprect.Height();
}
}
9、在客户区移动对话框
afx_msg UINT OnNcHitTest(CPoint point);
ON_WM_NCHITTEST()
UINT CDemoDlg1::OnNcHitTest(CPoint point)
{
UINT nValue = CDialog::OnNcHitTest(point);
if (nValue == HTCLIENT)
{
nValue = HTCAPTION;
}
return nValue;
}