怎么样用mfc画一个矩形,然后用鼠标拖动这个矩形?
展开全部
响应WM_PAINT 、 WM_LBUTTONDOWN 和 WM_MOUSEMOVE 消息吧
在鼠标按下(WM_LBUTTONDOWN)时WM_MOUSEMOVE记录鼠标坐标,然后在WM_PAINT按照鼠标坐标画出矩形
代码如下:
头文件:
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
{
public:
CMainWindow();
protected:
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 void OnPaint();
DECLARE_MESSAGE_MAP();
protected:
CPoint m_ptMouse;
CRect rect;
bool m_bLButtonDown;
};
源文件:
#define _WIN32_WINNT 0x0600
#include <afxwin.h>
#include "Main.h"
CMyApp application;
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
this->Create(NULL,_T("Test"));
m_bLButtonDown=false;
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bLButtonDown=true;
}
void CMainWindow::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bLButtonDown==true)
{
if (rect.PtInRect(point))
{
m_ptMouse=point;
Invalidate();
}
}
}
void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bLButtonDown=false;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
rect.top=m_ptMouse.y-15;
rect.left=m_ptMouse.x-15;
rect.bottom=rect.top+30;
rect.right=rect.left+30;
dc.Rectangle(&rect);
}
在鼠标按下(WM_LBUTTONDOWN)时WM_MOUSEMOVE记录鼠标坐标,然后在WM_PAINT按照鼠标坐标画出矩形
代码如下:
头文件:
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
{
public:
CMainWindow();
protected:
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 void OnPaint();
DECLARE_MESSAGE_MAP();
protected:
CPoint m_ptMouse;
CRect rect;
bool m_bLButtonDown;
};
源文件:
#define _WIN32_WINNT 0x0600
#include <afxwin.h>
#include "Main.h"
CMyApp application;
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
this->Create(NULL,_T("Test"));
m_bLButtonDown=false;
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bLButtonDown=true;
}
void CMainWindow::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bLButtonDown==true)
{
if (rect.PtInRect(point))
{
m_ptMouse=point;
Invalidate();
}
}
}
void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bLButtonDown=false;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
rect.top=m_ptMouse.y-15;
rect.left=m_ptMouse.x-15;
rect.bottom=rect.top+30;
rect.right=rect.left+30;
dc.Rectangle(&rect);
}
追问
我是在picture里面。后面还画了一张png图。我用的InvalidateRect(PCRect)这样的话闪得好厉害。有啥子方法不这么闪哦。。
追答
呵呵,其实我是新手,方法很笨,研究了一下,发现把DC的绘图模式设为R2_NOT可以解决闪的问题,在头文件(Main.h)窗口类的protected中定义:
bool m_bDrawDefaultRectangle;
然后把源文件(Main.cpp)改成下面的代码:(由于R2_NOT会把图像反色,在矩形覆盖图片的部分,可能会对底部的图像的颜色造成影响,如果只是需要绘制方框,可以把Rectangle函数替换为FrameRect函数,绿色矩形是为了模拟PNG图像)
#define _WIN32_WINNT 0x0600
#include
#include "Main.h"
CMyApp application;
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
this->Create(NULL,_T("Test"));
m_bLButtonDown=false;
m_ptMouse.x=100;
m_ptMouse.y=100;
rect.top=m_ptMouse.y-15;
rect.left=m_ptMouse.x-15;
rect.bottom=rect.top+30;
rect.right=rect.left+30;
m_bDrawDefaultRectangle=true;
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bLButtonDown=true;
}
void CMainWindow::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bLButtonDown==true)
{
CClientDC dc(this);
if (rect.PtInRect(point))
{
m_ptMouse=point;
int nOldMode=dc.SetROP2(R2_NOT);
dc.Rectangle(&rect);
rect.top=m_ptMouse.y-15;
rect.left=m_ptMouse.x-15;
rect.bottom=rect.top+30;
rect.right=rect.left+30;
dc.Rectangle(&rect);
dc.SetROP2(nOldMode);
}
}
}
void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bLButtonDown=false;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
CBrush brush(RGB(34,177,76));
dc.FillRect(CRect(100,100,300,300),&brush);
if (m_bDrawDefaultRectangle==true)
{
int nOldMode=dc.SetROP2(R2_NOT);
dc.Rectangle(&rect);
dc.SetROP2(nOldMode);
}
m_bDrawDefaultRectangle=false;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询