CreatePen的宽度设置为1的时候绘图速度还是可以吧,但设置大于1的时候就感觉很慢.是不是我代码有问题.还是要用其它方法解决?
LRESULT CALLBACK WndPaintProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static POINT LastPoint;
static POINT StartPoint;
static BOOL bDrawing = FALSE;
switch(message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
bDrawing = true; //描画状态开始
StartPoint.x = LOWORd(lParam);//MAKEPOINT(lParam); //当前点保存于NowPoint中
StartPoint.y = HIWORd(lParam);
return 0;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
if (bDrawing == false)//如果是描画状态
{
StartPoint.x = LOWORd(lParam);
StartPoint.y = HIWORd(lParam);
bDrawing = true;
}
HDC hdc;
hdc = GetDC(hwnd);
LastPoint.x = LOWORd(lParam);//MAKEPOINT(lParam); //当前点保存于LastPoint中
LastPoint.y = HIWORd(lParam);
Draw(hdc, StartPoint, LastPoint); //画开始点到当前点
StartPoint = LastPoint; //当前点设置为旧的点
ReleaseDC(hwnd, hdc);
TRACKMOUSEEVENT MouseEvent;//注册鼠标事件,检查是否离开了绘图区
MouseEvent.cbSize = sizeof(MouseEvent);
MouseEvent.dwFlags = TME_LEAVE;
MouseEvent.dwHoverTime = 10;
MouseEvent.hwndTrack = g_hwndPaint;
TrackMouseEvent(&MouseEvent);
//InvalidateRect(hwnd,NULL,FALSE);
}
return 0;
case WM_LBUTTONUP:
bDrawing = false; //描画状态结束
return 0;
case WM_MOUSELEAVE:
//SendMessage(hwnd, WM_LBUTTONUP, NULL, NULL);
bDrawing = false; //描画状态结束
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void Draw(HDC hdc, POINT beg, POINT end)
{
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0) ); //实线画笔
HPEN hOldPen;
//LOGBRUSH lb;
//LOGPEN logpen = {PS_SOLID, Width, RGB(0, 0, 0) };
//HPEN hPen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT,16, &lb, 0, NULL);
//POINT Width = {1, 1 };
//hPen = CreatePenIndirect(&logpen);
//SetROP2(hdc, R2_COPYPEN); //设置前景配色方式为画笔颜色
hOldPen = (HPEN)SelectObject(hdc, hPen);//保存旧画笔
MoveToEx(hdc, beg.x, beg.y, NULL); //移动描画点到按下鼠标键的地方
LineTo(hdc, end.x, end.y); //从描画点到(end.x, end.y)点
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
}