`
ub137ub
  • 浏览: 15657 次
社区版块
存档分类
最新评论

VC++日记(非模态,自定义消息,窗体移动……)

 
阅读更多

VC++日记(非模态,自定义消息,窗体移动……)
2011年07月07日
  1:如何做非模态对话框[/b]
  1)非模态对话框就是要控制对话框,不让它接管所有的消息循环。而DoModal()函数事实上就是“劫持”了所有的消息循环,只要它没有返回,任何“非该对话框消息”都会被忽略。事实上,如果我们避开使用DoModal(),我们就避免了这一层限制。我们重载对话窗的
  [b]BOOL Create(
LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 函数,在用到该对话窗的时候,像创建任意一个CWnd一样,使用Creat来创建就可以了。当然,如果这样的话,很多数据成员的初始化工作就最好放到这里了。另:注意要调用基类的Creat函数,下面是重载Creat的一个实例:
  BOOL myDlg02::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  {
           // TODO: Add your specialized code here and/or call the base class
           m_bCapture = FALSE;
           CRect rect1;
       m_pview->GetWindowRect(rect1);
           m_pointToLeft = rect1.TopLeft();
           m_sizeOffset  = CSize(0,0);
           return CDialog::Create(IDD, pParentWnd);
  }
  2)使用非模态对话框时候,必须注意对话框跟视图类的“对话”。所以,我们往往提供一个Cview指针成员,用来和拥有该对话框的视图“沟通”。下面是一个例子:
  class myDlg02 : public CDialog
  {
  [/b]
  [b]…
[/b]
  [b]public:

           myDlg02(CWnd* pParent = NULL);   // standard constructor
           myDlg02(CView* pview);
           myDlg02(){m_pview=NULL;}
      CSize m_sizeOffset;
           CPoint m_pointToLeft;
           BOOL  m_bCapture;
           virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
  protected:
  CView* m_pview;[/b]
  [b]…
[/b]
  [b]…
[/b]
  [b]}

  同时呢,m_pview往往也是从对话框所发消息的目的地:
  void myDlg02::OnOK()
  {
      if (m_pview!=NULL)
      {
          UpdateData(TRUE);
          m_pview->PostMessage(WM_NICETOYOU,IDOK);
      }
      else
          CDialog::OnOK();
  }
  
  void myDlg02::OnCancel()
  {[/b]
  [b]    if (m_pview!=NULL)

          m_pview->PostMessage(WM_GOODBYE,IDCANCEL);[/b]
  [b]    else
[/b]
  [b]        CDialog::OnCancel();
[/b]
  [b]}
[/b]
  [b]上面的代码重载了
OnOKOnCancle函数,相当于控制了OKCancle按钮的行为――让该对话框的拥有者来控制它们好了~~
  2:如何使用用户消息[/b]
  先明白一点:消息是发送出去了,那么由谁来处理呢?――自然是接收消息的“客户”了啊。所以,消息响应函数必须由客户负责。那么,用户自定义消息的消息响应函数的格式是怎样的呢?――
  [b]afx_msg void OnmyDlgGBOK(WPARAM wParam,LPARAM lParam);

  afx_msg void OnmyDlgGBCancle(WPARAM wParam,LPARAM lParam);
  (在视图类的头文件中声明之)[/b]
  [b]还要在消息映射表中添加:
[/b]
  [b]         ON_MESSAGE(WM_NICETOYOU,OnmyDlgGBOK)

           ON_MESSAGE(WM_GOODBYE,OnmyDlgGBCancle)
  (在视图的CPP文件的消息映射表中添加)[/b]
  [b]其中
WM_NICETOYOU WM_GOODBYE是消息识别号。[/b]
  [b]这样,只要在必要的时候用视图的指针向视图发送消息,加上上面的链接,我们的消息响应函数就可以调用了。
[/b]
  [b]另:介绍一下
BOOL PostMessage( UINT message, WPARAM wParam = 0, LPARAM lParam = 0 ) [/b]
  [b]LRESULT
SendMessage( UINT message, WPARAM wParam = 0, LPARAM lParam = 0 )这两个函数。首先,至于他们关于寄送和发送的区别就不谈了,关键要注意他们的参数。第一个参数是消息识别号,第二个和第三个参数是发给消息响应消息的参数,正好与消息响应函数的参数列表对应。WPARAM和LPARAM这两个类型其实际上是Long,也就是说,它们实质上是void指针,可以是任何类型的化身,可以根据实际情况灵活的运用它们。
  3:如何动态控制窗体的位置和大小
  MoveWindow(CRect(m_pointToLeft,size),TRUE);
  那么,如何向在标题栏上一样对整个对话框或者窗体进行动态移动呢?
  {
  afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  CSize m_sizeOffset;
  CPoint m_pointToLeft;
  BOOL  m_bCapture;
  }
  
  {
  void myDlg02::OnLButtonDown(UINT nFlags, CPoint point)
  {
           // TODO: Add your message handler code here and/or call default
           ClientToScreen(&point);
           m_sizeOffset = point - m_pointToLeft;
           m_bCapture = TRUE;
           SetCapture();
           CDialog::OnLButtonDown(nFlags, point);
  }
  
  void myDlg02::OnLButtonUp(UINT nFlags, CPoint point)
  {
           // TODO: Add your message handler code here and/or call default
           m_bCapture = FALSE;
           ReleaseCapture();
           CDialog::OnLButtonUp(nFlags, point);
  }
  
  void myDlg02::OnMouseMove(UINT nFlags, CPoint point)
  {
           // TODO: Add your message handler code here and/or call default
           if (m_bCapture)
           {       
           ClientToScreen(&point);
           m_pointToLeft = point - m_sizeOffset;
           CRect rect;
           GetWindowRect(rect);
           CSize size(rect.Width(),rect.Height());
           this->MoveWindow(CRect(m_pointToLeft,size),TRUE);
           CDialog::OnMouseMove(nFlags, point);
           }
  }
   BOOL myDlg02::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  {
           // TODO: Add your specialized code here and/or call the base class
           m_bCapture = FALSE;
           CRect rect1;
      m_pview->GetWindowRect(rect1);
           m_pointToLeft = rect1.TopLeft();
           m_sizeOffset  = CSize(0,0);
           return CDialog::Create(IDD, pParentWnd);
  }
  }
  不知道为什么,在做窗体的移动时候如果使用客户坐标,移动的窗体老是出现跳动和闪烁,而改成屏幕坐标,这个问题就神秘的解决了[/b]
  [b]4
:如何控制窗体的初始化大小(最大,最小,默认)[/b]
  vc++ 窗口最大化方法
  一般的做法是在 C**App::InitInstance()中,修改成这样:
  [b]{

  //..
  [/b]
  [b]m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
  m_pMainWnd->UpdateWindow();
  return TRUE;..

  }
  或者,还在 CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中,添加:
  {
  //...
  cs.style |= WS_MAXIMIZE;
  //...
  }
  但是,这种方法的最大化会体现出窗体的有效变大的过程,这个是因为在ShowWindow之前,还有地方显示了窗体,做法是:
  CCommandLineInfo cmdInfo;
  ParseCommandLine(cmdInfo);
  // Dispatch commands specified on the command line
  //ParseCommandLine之后,ProcessShellCommand之前,添加这句!!!
  m_nCmdShow = SW_SHOWMAXIMIZED; 
  if (!ProcessShellCommand(cmdInfo))
  return FALSE;

  // The one and only window has been initialized, so show and update it.
  m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
  m_pMainWnd->UpdateWindow();
  5:如何控制控件的层叠次序[b][/b]
  Ctrl+d,单击控件,会看到控件上的代号会变化,代号小的更底层。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics