更新时间:2015-12-27 22:26:53浏览次数:1+次
我们要实现的效果如下:
【拖动矩形时矩形本身实时跟着移动】
//实时移动矩形
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
Rectangle(hdc,rectMove.left,rectMove.top,rectMove.right,rectMove.bottom);
CopyRect(&rectLast,&rectMove);
这样就结束了,就实现了实时移动矩形的效果。而如果你要实现带有虚框的移动,则不要使用这段代码了。因为之前已经将矩形都擦除了,自然原始的矩形也看不见了。因为移动的时候会和原始矩形重合,擦除了移动时的矩形也就将原始矩形擦没了。既然如此,就先擦除移动的矩形,然后再画出原始矩形。这样原始矩形就保留下来了。紧接着就是画带黑色边框无填充的矩形了。这个就是虚框了。在不停的移动中,这个虚框的位置就在不停地改变,就实现了虚框移动的效果。代码如下://使用虚框代替矩形实时移动效果
//画初始的矩形
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
Rectangle(hdc,rectOld.left,rectOld.top,rectOld.right,rectOld.bottom);
//画本次移动的矩形轨迹
SelectObject(hdc,GetStockObject(BLACK_PEN));
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Rectangle(hdc,rectMove.left,rectMove.top,rectMove.right,rectMove.bottom);
CopyRect(&rectLast,&rectMove);
注意,实时移动矩形和使用虚框移动矩形的代码二选一,不要同时生效哦。#include "windows.h"
#include "Windowsx.h"
#include <tchar.h>
// - 项目是Unicode字符集
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static POINT ptOld,ptNew;
static RECT rectOld,rectMove,rectNew,rectLast;
static bool bIsMove=false;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
SelectObject(hdc,GetStockObject(WHITE_PEN));
SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
Rectangle(hdc,rectOld.left,rectOld.top,rectOld.right,rectOld.bottom);
EndPaint(hwnd,&ps);
return 0;
case WM_RBUTTONDOWN:
//绘制第一个矩形
hdc = GetDC(hwnd);
SetRect(&rectOld,10,10,110,70);//保存上次矩形的位置
SelectObject(hdc,GetStockObject(WHITE_PEN));
SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
Rectangle(hdc,rectOld.left,rectOld.top,rectOld.right,rectOld.bottom);
ReleaseDC(hwnd,hdc);
return 0;
case WM_LBUTTONDOWN:
//确定鼠标起始位置
ptOld.x = GET_X_LPARAM(lParam);
ptOld.y = GET_Y_LPARAM(lParam);
if (PtInRect(&rectOld,ptOld))
{
bIsMove=true;
}
else
{
bIsMove=false;
}
return 0;
case WM_LBUTTONUP:
//确定鼠标释放位置
if (bIsMove)
{
hdc = GetDC(hwnd);
bIsMove=false;
ptNew.x = GET_X_LPARAM(lParam);
ptNew.y = GET_Y_LPARAM(lParam);
rectNew.left = rectOld.left+ (ptNew.x -ptOld.x);
rectNew.top = rectOld.top+ (ptNew.y -ptOld.y);
rectNew.right = rectNew.left+100;
rectNew.bottom = rectNew.top+60;
CopyRect(&rectOld,&rectNew);
InvalidateRect(hwnd,NULL,TRUE);
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_MOUSEMOVE:
if(bIsMove)
{
//鼠标移动画边框
POINT ptMove;
ptMove.x = GET_X_LPARAM(lParam);
ptMove.y = GET_Y_LPARAM(lParam);
rectMove.left=rectOld.left+(ptMove.x-ptOld.x);
rectMove.top=rectOld.top+(ptMove.y-ptOld.y);
rectMove.right = rectMove.left+100;
rectMove.bottom = rectMove.top +60;
hdc = GetDC(hwnd);
//-擦掉上一次的拖动的矩形轨迹
SelectObject(hdc,GetStockObject(WHITE_PEN));
SelectObject(hdc,GetStockObject(WHITE_BRUSH));
Rectangle(hdc,rectLast.left,rectLast.top,rectLast.right,rectLast.bottom);
////实时移动矩形
//SelectObject(hdc,GetStockObject(NULL_PEN));
//SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
//Rectangle(hdc,rectMove.left,rectMove.top,rectMove.right,rectMove.bottom);
//CopyRect(&rectLast,&rectMove);
//使用虚框代替矩形实时移动效果
//画初始的矩形
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
Rectangle(hdc,rectOld.left,rectOld.top,rectOld.right,rectOld.bottom);
//画本次移动的矩形轨迹
SelectObject(hdc,GetStockObject(BLACK_PEN));
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Rectangle(hdc,rectMove.left,rectMove.top,rectMove.right,rectMove.bottom);
CopyRect(&rectLast,&rectMove);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int iShow)
{
TCHAR ClassName[] = _T("MyClass");
TCHAR title1[] = _T("C++技术网http://www.cjjjs.com");
WNDCLASS wndClass;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName = ClassName;
wndClass.lpszMenuName=NULL;
wndClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndClass)) return 0;
HWND hwnd = CreateWindow(ClassName,title1,WS_OVERLAPPEDWINDOW,0,0,440,440,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
相关资讯