VC++如何用鼠标拖出一个矩形?
我拖出来的都是那种带残影的。。。代码如下:voidCDlg::OnMouseMove(UINTnFlags,CPointpoint){//TODO:在此添加消息处理程序代...
我拖出来的都是那种带残影的。。。
代码如下:
void CDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(nFlags&MK_LBUTTON)
{
if(RectF==false)
{
m_ptBegin.x=point.x;
m_ptBegin.y=point.y;
RectF=true;
}
else
{
CClientDC dc(this);
if(!(m_ptBegin.x==-1||m_ptBegin.y==-1||m_ptEnd.x==-1||m_ptEnd.y==-1))
UpdateData(true);
CPen pen(PS_SOLID,1,RGB(0,255,255));
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.SelectObject(&pen);
m_ptEnd.x=point.x;
m_ptEnd.y=point.y;
dc.Rectangle(CRect(m_ptBegin,m_ptEnd));
dc.SelectObject(pOldBrush);
pOldBrush->DeleteObject();
pBrush->DeleteObject();
}
}
CDialog::OnMouseMove(nFlags, point);
}
残影图如图所示:
请问到底是什么原因?
我要实时拉出矩形,就像画图程序里面那种效果一样,当然矩形当中应该是透明的。。。 展开
代码如下:
void CDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(nFlags&MK_LBUTTON)
{
if(RectF==false)
{
m_ptBegin.x=point.x;
m_ptBegin.y=point.y;
RectF=true;
}
else
{
CClientDC dc(this);
if(!(m_ptBegin.x==-1||m_ptBegin.y==-1||m_ptEnd.x==-1||m_ptEnd.y==-1))
UpdateData(true);
CPen pen(PS_SOLID,1,RGB(0,255,255));
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.SelectObject(&pen);
m_ptEnd.x=point.x;
m_ptEnd.y=point.y;
dc.Rectangle(CRect(m_ptBegin,m_ptEnd));
dc.SelectObject(pOldBrush);
pOldBrush->DeleteObject();
pBrush->DeleteObject();
}
}
CDialog::OnMouseMove(nFlags, point);
}
残影图如图所示:
请问到底是什么原因?
我要实时拉出矩形,就像画图程序里面那种效果一样,当然矩形当中应该是透明的。。。 展开
展开全部
OnMouseMove是在鼠标移动时的响应函数,如果只是画矩形的话不冲如要用这个函桐型数实现。我的方法:
在OnLButtonDown中保存起始点,在OnLButtonUp中记录另一个点,两个点称为矩局判猜形的对角线,唯一确定一个矩形。可以用CClientDC中的Rect函数画矩形,也可以自己用MoveTo、LineTo画出矩形四条边,构成矩形。
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrgin = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.Rectangle(CRect(m_ptOrgin,point));
dc.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
Invalidate();// 引起窗口重画
CScrollView::OnLButtonUp(nFlags, point);
}
在OnLButtonDown中保存起始点,在OnLButtonUp中记录另一个点,两个点称为矩局判猜形的对角线,唯一确定一个矩形。可以用CClientDC中的Rect函数画矩形,也可以自己用MoveTo、LineTo画出矩形四条边,构成矩形。
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrgin = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.Rectangle(CRect(m_ptOrgin,point));
dc.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
Invalidate();// 引起窗口重画
CScrollView::OnLButtonUp(nFlags, point);
}
展开全部
用双缓绘图 ,每次画矩形在新的背景禅腔上画
或者 如果你不嫌麻启坦烦 ,记录上次绘制轨迹 ,取反也是可以的悄袭桐
CClientDC dc(this);
CDC *memDC=new CDC;
memDC->CreateCompatibleDC(&dc);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
mem->Rectangle(CRect(m_ptOrgin,point));
mem->.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
dc.BitBlt()
.......................................................中间的省略了
Invalidate();// 引起窗口重画
或者 如果你不嫌麻启坦烦 ,记录上次绘制轨迹 ,取反也是可以的悄袭桐
CClientDC dc(this);
CDC *memDC=new CDC;
memDC->CreateCompatibleDC(&dc);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
mem->Rectangle(CRect(m_ptOrgin,point));
mem->.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
dc.BitBlt()
.......................................................中间的省略了
Invalidate();// 引起窗口重画
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
OnMouseMove是在鼠标移动时局判猜的响应函数,它用来桐型画曲线比较好。
只是画矩形的话,不要用这个函数实现。
在OnLButtonDown中保存起始点,在OnLButtonUp中记录另一个点,两个点冲如称为矩形的对角线,唯一确定一个矩形。可以用CClientDC中的Rect函数画矩形,也可以自己用MoveTo、LineTo画出矩形四条边,构成矩形。
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrgin = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.Rectangle(CRect(m_ptOrgin,point));
dc.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
Invalidate();// 引起窗口重画
CScrollView::OnLButtonUp(nFlags, point);
}
只是画矩形的话,不要用这个函数实现。
在OnLButtonDown中保存起始点,在OnLButtonUp中记录另一个点,两个点冲如称为矩形的对角线,唯一确定一个矩形。可以用CClientDC中的Rect函数画矩形,也可以自己用MoveTo、LineTo画出矩形四条边,构成矩形。
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrgin = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.Rectangle(CRect(m_ptOrgin,point));
dc.FillSolidRect(CRect(m_ptOrgin,point),RGB(120,20,20));
Invalidate();// 引起窗口重画
CScrollView::OnLButtonUp(nFlags, point);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
新建一个win32工程,答敏粘贴以下代码塌举樱。愿顺利!阿弥陀佛!
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BlokOut2") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow ( szAppName, TEXT ("团丛Mouse Button & Capture Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void DrawBoxOutline (HWND hwnd, POINT ptBeg, POINT ptEnd)
{
HDC hdc ;
hdc = GetDC (hwnd) ;
SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
ReleaseDC (hwnd, hdc) ;
}
LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static BOOL fBlocking, fValidBox ;
static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd ;
HDC hdc ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_LBUTTONDOWN :
ptBeg.x = ptEnd.x = LOWORD (lParam) ;
ptBeg.y = ptEnd.y = HIWORD (lParam) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
SetCapture (hwnd) ;
SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
fBlocking = TRUE ;
return 0 ;
case WM_MOUSEMOVE :
if (fBlocking)
{
SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ptEnd.x = LOWORD (lParam) ;
ptEnd.y = HIWORD (lParam) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
}
return 0 ;
case WM_LBUTTONUP :
if (fBlocking)
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ptBoxBeg = ptBeg ;
ptBoxEnd.x = LOWORD (lParam) ;
ptBoxEnd.y = HIWORD (lParam) ;
ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
fBlocking = FALSE ;
fValidBox = TRUE ;
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case WM_CHAR :
if (fBlocking & wParam == '\x1B') // i.e., Escape
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
fBlocking = FALSE ;
}
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
if (fValidBox)
{
SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
Rectangle (hdc, ptBoxBeg.x, ptBoxBeg.y,
ptBoxEnd.x, ptBoxEnd.y) ;
}
if (fBlocking)
{
SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BlokOut2") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow ( szAppName, TEXT ("团丛Mouse Button & Capture Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void DrawBoxOutline (HWND hwnd, POINT ptBeg, POINT ptEnd)
{
HDC hdc ;
hdc = GetDC (hwnd) ;
SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
ReleaseDC (hwnd, hdc) ;
}
LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static BOOL fBlocking, fValidBox ;
static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd ;
HDC hdc ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_LBUTTONDOWN :
ptBeg.x = ptEnd.x = LOWORD (lParam) ;
ptBeg.y = ptEnd.y = HIWORD (lParam) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
SetCapture (hwnd) ;
SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
fBlocking = TRUE ;
return 0 ;
case WM_MOUSEMOVE :
if (fBlocking)
{
SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ptEnd.x = LOWORD (lParam) ;
ptEnd.y = HIWORD (lParam) ;
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
}
return 0 ;
case WM_LBUTTONUP :
if (fBlocking)
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ptBoxBeg = ptBeg ;
ptBoxEnd.x = LOWORD (lParam) ;
ptBoxEnd.y = HIWORD (lParam) ;
ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
fBlocking = FALSE ;
fValidBox = TRUE ;
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case WM_CHAR :
if (fBlocking & wParam == '\x1B') // i.e., Escape
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
fBlocking = FALSE ;
}
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
if (fValidBox)
{
SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
Rectangle (hdc, ptBoxBeg.x, ptBoxBeg.y,
ptBoxEnd.x, ptBoxEnd.y) ;
}
if (fBlocking)
{
SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最枣液扮槐后加上InvalidateRect(CRect(m_ptBegin,m_ptEnd),TRUE),你试试
得更新一下。。。凳缺物
得更新一下。。。凳缺物
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是MFC吗?
在OnLButtonDown里:
这个函数被调用的时候不液消是传进来一个point参数闹咐知吗,就用这个参数
if((x1<point.x<x2)&&(y1<point.y<y2))....;否则。。。简清
在OnLButtonDown里:
这个函数被调用的时候不液消是传进来一个point参数闹咐知吗,就用这个参数
if((x1<point.x<x2)&&(y1<point.y<y2))....;否则。。。简清
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询