MFC常用方法总结二

16.对话框自动停靠在屏幕边


    const int DETASTEP = 50;
     BOOL AdjustPos(CWnd *pWnd, CRect* lpRect)
     {
        //自动靠边
        int iSX = GetSystemMetrics(SM_CXFULLSCREEN);
        int iSY = GetSystemMetrics(SM_CYFULLSCREEN);
        RECT rWorkArea;
        BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0);

        CRect rcWA;
        if ( !bResult )
        {
            //如果调用不成功就利用GetSystemMetrics获取屏幕面积
            rcWA = CRect(0,0,iSX,iSY);
        }
        else
            rcWA = rWorkArea;

        int iX = lpRect->left;
        int iY = lpRect->top;
        if ( iX < rcWA.left + DETASTEP && iX!=rcWA.left )
        {
            //调整左
            pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
            lpRect->OffsetRect(rcWA.left-iX,0);
            AdjustPos(lpRect);
            return TRUE;
        }
        if ( iY < rcWA.top + DETASTEP && iY!=rcWA.top )
        {
            //调整上
            pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE);
            lpRect->OffsetRect(0,rcWA.top-iY);
            AdjustPos(lpRect);
            return TRUE;
        }
        if ( iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width() )
        {
            //调整右
            pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
            lpRect->OffsetRect(rcWA.right-lpRect->right,0);
            AdjustPos(lpRect);
            return TRUE;
        }
        if ( iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect->Height() )
        {
            //调整下
            pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
            lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
            return TRUE;
        }
        return FALSE;
    }
    //然后在ONMOVEING事件中使用如下过程调用
    CRect r=*pRect;
    AdjustPos(this, &r);
    *pRect=(RECT)r;


17.单击窗口任意位置都可拖动窗口

    方法一:
     添加 WM_LBUTTONDOWN 的消息映射
     void CTest6Dlg::OnLButtonDown(UINT nFlags, CPoint point)
     {
          PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);

          CDialog::OnLButtonDown(nFlags, point);
     }

    方法二:
   
 添加 WM_NCHITTEST 的消息映射
    注意:在classwizard->message中找不到
WM_NCHITTEST的,需要在选项卡class info->message filter中选择window后该消息才会出现在message中。
 
     void CTest6Dlg::OnNCHitTest(CPoint point)
     {
            return HTCAPTION;
      //    return CDialog::
OnNCHitTest(point);
     }

     或者参考
        http://msdn.microsoft.com/msdnmag/issues/02/12/CQA/default.aspx


18.用Enter键替换Tab键实现焦点切换
     BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
     {
        if ( pMsg->message == WM_KEYDOWN )
          {
              if ( pMsg->wParam == VK_RETURN )
                   pMsg->wParam = VK_TAB;
          }
          return CDialog::PreTranslateMessage(pMsg);
     }

19.在对话框添加快捷键
     (1) 在CXXXApp中类中添加声明
        HACCEL m_haccel;
     (2) 在resource view中右键点击树的根目录,选择insert,添加一个新的Accelerator,默认ID为IDR_ACCELERATOR1。
         在其中添加相应菜单的快捷键。
     (3) 在BOOL CXXXApp::InitInstance()中添加代码
        m_haccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
     (4) 添加CXXXApp类的 ProcessMessageFilter 消息映射函数
         BOOL CTest6App::ProcessMessageFilter(int code, LPMSG lpMsg)
         {
              if ( m_haccel )
              {
                  if ( ::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg) )
                       return TRUE;
              }
              return CWinApp::ProcessMessageFilter(code, lpMsg);
         }

或者参考
How to use accelerator keys and a main menu on the dialog box in Visual C++
http://support.microsoft.com/kb/100770/en-us
Adding Hot Keys to your Application
http://msdn.microsoft.com/msdnmag/issues/1200/c/default.aspx


20.对话框全屏
    int cx, cy;
    HDC dc = ::GetDC(NULL);
    cx = GetDeviceCaps(dc,HORZRES) + GetSystemMetrics(SM_CXBORDER);
    cy = GetDeviceCaps(dc,VERTRES) + GetSystemMetrics(SM_CYBORDER);
    ::ReleaseDC(0,dc);

    // Remove caption and border
    SetWindowLong(m_hWnd, GWL_STYLE,
                    GetWindowLong(m_hWnd, GWL_STYLE) & (~(WS_CAPTION | WS_BORDER)));

    // Put window on top and expand it to fill screen
    ::SetWindowPos(m_hWnd, HWND_TOPMOST,
          -(GetSystemMetrics(SM_CXBORDER)+1),
          -(GetSystemMetrics(SM_CYBORDER)+1),
          cx+1,cy+1, SWP_NOZORDER);
    或参考
        http://www.codeguru.com/cpp/w-d/dislog/dialog-basedapplications/article.php/c1837/


21.控制对话框最大最小尺寸
    (1) 对话框的属性的必须是resizing的
    (2) 打开classwizard->class info标签页->message filter中选择window
    (3) 添加 WM_GETMINMAXINFO 消息映射
        void CTest6Dlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
        {
             lpMMI->ptMinTrackSize = CPoint(200, 200);
        }


22. 创建无模式对话框
Creating a Modeless Dialog Box with MFC Libraries
http://support.microsoft.com/kb/103788/EN-US/

Visual C++ MFC Samples      
MODELESS Sample: Uses a CDialog Object as a Modeless Dialog Box
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/_sample_mfc_MODELESS.asp


23.在对话框中改变菜单项状态(enable/disable, check/uncheck, change text)

You cannot change the state of a menu item from its command user-interface handler if the menu is attached to a dialog box in Visual C++
http://support.microsoft.com/kb/242577/en-us


24. 按下F1出现帮
Context-Sensitive Help in a CDialog Object
http://support.microsoft.com/kb/141724/en-us
msdn中的介绍
http://msdn2.microsoft.com/en-us/library/dyd1yfww.aspx


或者如果你要屏蔽按下F1后出现的“
找不到*.hlp文件”的提示对话框
添加 WM_HELPINFO 消息映射
BOOL CTest6Dlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
     return TRUE;
    //return CDialog::OnHelpInfo(pHelpInfo);//屏蔽该句
}


25. 对话框初始化设置输入焦点的问题

默认情况下,对话框初始化显示的焦点按照在对话框编辑期间设置的tab order的第一个控件来设置的。(设置tab order可在对话框的resource view中用Ctrl+D显示出来,点鼠标进行顺序设置)。如果想人为的改变初始化时的输入焦点,可在对话框的OnInitDialog中把 return  TRUE; 改为 return  FALSE;
MSDN上的解释如下:

Return Value

Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.


 

26. 在对话框间传递数据

CDlg1::OnButton1()
{
      CDlg2 dlg2;
      dlg2.m_str = _T("你好"; )
      dlg2.m_bJudge = TRUE;
      dlg2.DoModal();
}
 
//Dlg2.h
public:
     CString m_str;
     BOOL m_bJudge;
 
 
//Dlg2.cpp
CDlg2::OnInitDialog()
{
    if (m_bJudge)
        GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值