发布网友 发布时间:2022-09-13 14:48
共1个回答
热心网友 时间:2024-02-24 08:17
响应WM_PAINT 、 WM_LBUTTONDOWN 和 WM_MOUSEMOVE 消息吧追答呵呵,其实我是新手,方法很笨,研究了一下,发现把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;
}