《精通Visual C++ 实效编程280例》读书笔记(第四章 对话框)

来源:互联网 发布:单片机窗帘控制系统 编辑:程序博客网 时间:2024/05/02 02:32

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;
       }

至此,第四章对话框记录完毕,接下来是第五章:框架和文档视图。这一章可能要等一段时间才能更新。因为看了几个例子发现自己动于MFC中文档、视图、框架三者的关系知之甚少,只知道框架好像是标题栏菜单栏工具栏什么的,视图盖在最上面,其他的就完全不知道了。所以决定回过头去看深入浅出MFC,所以中间可能要加一篇《深入浅出MFC》的读书笔记。这本书的第五章,我们暂且用一个很官方的说法,叫做:无限期搁置。其实也不是,看完《深入浅出MFC》再说吧。