想在对话框窗口中实现如下鼠标事件,不知道怎么弄,请教各位达人.
在对话框内任意位置按下鼠标左键开始出现虚线的矩形,大小随着鼠标拖拽而变化,放开左键后矩形消失.想达到鼠标在windows桌面上的的这种操作一样的效果.
不知道有没有表达清楚,唉,肯定各位指教

解决方案 »

  1.   

    重载OnLButtonDown  OnLButtonUp   OnMouseMove,down的时候记下初始点,UP的时候记下终点,MOVE的时候有个过度点,在MOVE里写个函数,来擦画线,
    如:int Oldmode,minx,miny;
    minx=min(pDoc->Width*m_nZoom,rctClient.right)-16*m_nZoom;
    miny=min(pDoc->Height*m_nZoom,rctClient.bottom);
    point.x=min(point.x,minx-1);
    point.y=min(point.y,miny-1);
    m_pDC->SelectStockObject(BLACK_PEN);
    Oldmode=m_pDC->SetROP2(R2_NOT);
    //去除旧框
    m_pDC->MoveTo(0,ptstart.y/m_nZoom*m_nZoom);
    m_pDC->LineTo(minx,ptstart.y/m_nZoom*m_nZoom);
    m_pDC->MoveTo(0,ptOld.y/m_nZoom*m_nZoom);
    m_pDC->LineTo(minx,ptOld.y/m_nZoom*m_nZoom);
    //画新框
    m_pDC->MoveTo(0,ptstart.y/m_nZoom*m_nZoom);
    m_pDC->LineTo(minx,ptstart.y/m_nZoom*m_nZoom);
    m_pDC->MoveTo(0,point.y/m_nZoom*m_nZoom);
    m_pDC->LineTo(minx,point.y/m_nZoom*m_nZoom);
    ptOld=point;
    m_pDC->SetROP2(Oldmode);
      

  2.   

    帮你写了一个
    // DrawLineDlg.h : header file
    //#if !defined(AFX_DRAWLINEDLG_H__52ED76FE_F760_4063_8C40_E4533813ED02__INCLUDED_)
    #define AFX_DRAWLINEDLG_H__52ED76FE_F760_4063_8C40_E4533813ED02__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
    // CDrawLineDlg dialogclass CDrawLineDlg : public CDialog
    {
    // Construction
    public:
    CDrawLineDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
    //{{AFX_DATA(CDrawLineDlg)
    enum { IDD = IDD_DRAWLINE_DIALOG };
    // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CDrawLineDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    HICON m_hIcon;
    CRect rcDrag;
    BOOL m_bDrawing; // Generated message map functions
    //{{AFX_MSG(CDrawLineDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    private:
    void DrawLines();
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_DRAWLINEDLG_H__52ED76FE_F760_4063_8C40_E4533813ED02__INCLUDED_)
    // DrawLineDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "DrawLine.h"
    #include "DrawLineDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDrawLineDlg dialogCDrawLineDlg::CDrawLineDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDrawLineDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CDrawLineDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_bDrawing = FALSE;
    }void CDrawLineDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDrawLineDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CDrawLineDlg, CDialog)
    //{{AFX_MSG_MAP(CDrawLineDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    ON_WM_LBUTTONUP()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDrawLineDlg message handlersBOOL CDrawLineDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CDrawLineDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CDrawLineDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CDrawLineDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CDrawLineDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    m_bDrawing = TRUE;
    rcDrag.left=rcDrag.right=point.x;
    rcDrag.top=rcDrag.bottom=point.y;
    DrawLines(); CDialog::OnLButtonDown(nFlags, point);
    }void CDrawLineDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if (m_bDrawing)
    {

    DrawLines();
    rcDrag.bottom=point.y;
    rcDrag.right=point.x;
    DrawLines(); }
    CDialog::OnMouseMove(nFlags, point);
    }void CDrawLineDlg::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    m_bDrawing = FALSE;
    DrawLines();
    CDialog::OnLButtonUp(nFlags, point);
    }void CDrawLineDlg::DrawLines()
    {
    CClientDC dc(this);
    dc.SetROP2(R2_NOT);
    dc.MoveTo(rcDrag.left,rcDrag.top);
    dc.LineTo(rcDrag.left,rcDrag.bottom);
    dc.LineTo(rcDrag.right,rcDrag.bottom);
    dc.LineTo(rcDrag.right,rcDrag.top);
    dc.LineTo(rcDrag.left,rcDrag.top);
    }