如何实现点一下对话框外面的区域,自动隐藏对话框

建立名为My的对话框程序.实现如下步骤:

MyDlg.h中加入:

    class CShowWindow1Dlg : public CDialog
    {
     public:
         int m_cx;
         int m_cy;
    };

    MyDlg.cpp:

    //定义消息映象,处理鼠标单击及激活
    BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
        ON_WM_LBUTTONDOWN()//添加
        ON_WM_ACTIVATE()//添加
    END_MESSAGE_MAP()

    void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
        CRect rect;
        GetClientRect(&rect);
        rect.InflateRect(m_cx, m_cy);
  
        //Release dialog if the user click outside it.
        if(!rect.PtInRect(point))
        {
           EndDialog(IDCANCEL);
        }

        CDialog::OnLButtonDown(nFlags, point);
    }

    void CMyDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
    {
        CDialog::OnActivate(nState, pWndOther, bMinimized);

        if( nState == WA_ACTIVE || nState == WA_CLICKACTIVE)
            SetCapture();
        else
            ReleaseCapture();
    }

    BOOL CMyDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
        .....
        
        OSVERSIONINFO info;
        memset((char*)&info, 0, sizeof(OSVERSIONINFO));
        info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        if(GetVersionEx(&info))
        {  //we don't run on Win32s, so check only two values
           if(info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
           {  //On windows 95
              m_cx = GetSystemMetrics(SM_CXFIXEDFRAME);
              m_cy = GetSystemMetrics(SM_CYFIXEDFRAME);
           }
           else
           {  //On NT
              m_cx = GetSystemMetrics(SM_CXDLGFRAME);
              m_cy = GetSystemMetrics(SM_CYDLGFRAME);
           }
        }
    }

说明:
     1)WM_ACTIVATE消息在ClassWizard中没有,按如下步骤添加,右击CMyDlg,Add Windows Message Handle,接着在Filter for messages available to中选Window,New Windows messages/events列表中就会出现WM_ACTIVATE,选中,点击Add Handler
     2)SM_CXDLGFRAME,SM_CYDLGFRAME   NT中取得有WS_DLGFRAMEstyle风格的窗口的高和宽 95中已经废弃而采用SM_CX_FIXEDFRAMESM_CYFIXEDFRAME

你可能感兴趣的:(windows,filter,user,Class,dialog)