我是这样写的,可是用了缓冲区后,就什么也不能画了
各位帮忙看看LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static HINSTANCE hInst;
static int cxClient, cyClient, cxScreen, cyScreen;
HDC hdc;
static POINT pt,lp;
PAINTSTRUCT ps ;
HPEN hpen;
static int bDown=0;
static HBITMAP hBitmap;
// static SIZE size;
RECT rect;
switch (message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect);
hdc = GetDC(hwnd);
hdcMem1 = CreateCompatibleDC(hdc);
hBitMap1 = CreateCompatibleBitmap(hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem1,hBitMap1);
hdcMem2 = CreateCompatibleDC(hdc);
hBitMap2 = CreateCompatibleBitmap(hdc,hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem2,hBitMap2);
hBitMap = CreateCompatibleBitmap(hdc,hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdc,hBitMap);
BitBlt(hdcMem1,0,0,0,0,hdc,0,0,SRCCOPY);
BitBlt(hdcMem2,0,0,0,0,hdc,0,0,SRCCOPY);
ReleaseDC(hwnd,hdc);
return 0; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
BitBlt(hdc,0,0,0,0,hdcMem1,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
case WM_LBUTTONDOWN:
bDown = 1;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
return 0;
/* case WM_SIZE:
size.cx=LOWORD(lParam);
size.cy=HIWORD(lParam);
return 0;*/ case WM_LBUTTONUP:
bDown = 0; return 0;
case WM_MOUSEMOVE: // 鼠标移动
if(bDown==1)
{
BitBlt(hdcMem1,0,0,0,0,hdcMem2,0,0,SRCCOPY);
lp.x = LOWORD(lParam);
lp.y = HIWORD(lParam);// hdc = GetDC(hwnd);
hpen=CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
SelectObject(hdcMem1,hpen);
MoveToEx(hdcMem1,pt.x,pt.y,NULL);
LineTo(hdcMem1,lp.x,lp.y);
// BitBlt(hdcMem2,0,0,size.cx,size.cy,hdcMem1,0,0,SRCCOPY); InvalidateRect(hwnd, NULL, FALSE);
}
// InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
各位帮忙看看LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static HINSTANCE hInst;
static int cxClient, cyClient, cxScreen, cyScreen;
HDC hdc;
static POINT pt,lp;
PAINTSTRUCT ps ;
HPEN hpen;
static int bDown=0;
static HBITMAP hBitmap;
// static SIZE size;
RECT rect;
switch (message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect);
hdc = GetDC(hwnd);
hdcMem1 = CreateCompatibleDC(hdc);
hBitMap1 = CreateCompatibleBitmap(hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem1,hBitMap1);
hdcMem2 = CreateCompatibleDC(hdc);
hBitMap2 = CreateCompatibleBitmap(hdc,hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem2,hBitMap2);
hBitMap = CreateCompatibleBitmap(hdc,hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdc,hBitMap);
BitBlt(hdcMem1,0,0,0,0,hdc,0,0,SRCCOPY);
BitBlt(hdcMem2,0,0,0,0,hdc,0,0,SRCCOPY);
ReleaseDC(hwnd,hdc);
return 0; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
BitBlt(hdc,0,0,0,0,hdcMem1,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
case WM_LBUTTONDOWN:
bDown = 1;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
return 0;
/* case WM_SIZE:
size.cx=LOWORD(lParam);
size.cy=HIWORD(lParam);
return 0;*/ case WM_LBUTTONUP:
bDown = 0; return 0;
case WM_MOUSEMOVE: // 鼠标移动
if(bDown==1)
{
BitBlt(hdcMem1,0,0,0,0,hdcMem2,0,0,SRCCOPY);
lp.x = LOWORD(lParam);
lp.y = HIWORD(lParam);// hdc = GetDC(hwnd);
hpen=CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
SelectObject(hdcMem1,hpen);
MoveToEx(hdcMem1,pt.x,pt.y,NULL);
LineTo(hdcMem1,lp.x,lp.y);
// BitBlt(hdcMem2,0,0,size.cx,size.cy,hdcMem1,0,0,SRCCOPY); InvalidateRect(hwnd, NULL, FALSE);
}
// InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static HDC hdcMem1,hdcMem2;
static HBITMAP hBitMap,hBitMap1,hBitMap2;
static SIZE size;
// 上面的变量是我添加的,是不是原来的都是全局变量?
static HINSTANCE hInst;
static int cxClient, cyClient, cxScreen, cyScreen;
HDC hdc;
static POINT pt,lp;
PAINTSTRUCT ps ;
HPEN hpen;
static int bDown=0; static HBITMAP hBitmap;
// static SIZE size;
RECT rect; switch (message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect);
hdc = GetDC(hwnd);
hdcMem1 = CreateCompatibleDC(hdc);
hBitMap1 = CreateCompatibleBitmap(hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem1,hBitMap1);
hdcMem2 = CreateCompatibleDC(hdc);
hBitMap2 = CreateCompatibleBitmap(hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdcMem2,hBitMap2);
hBitMap = CreateCompatibleBitmap(hdc,rect.right-rect.left,rect.bottom-rect.top);
SelectObject(hdc,hBitMap);
BitBlt(hdcMem1,0,0,0,0,hdc,0,0,SRCCOPY);
BitBlt(hdcMem2,0,0,0,0,hdc,0,0,SRCCOPY);
ReleaseDC(hwnd,hdc);
return 0; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
// 这一句,参数中宽和高指定的都是0,我想错误的关键原因在这一句
// 现在cx,xy中存储的是客户区的宽和高
BitBlt(hdc,0,0,size.cx,size.cy,hdcMem1,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0; case WM_LBUTTONDOWN:
bDown = 1;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
return 0; case WM_SIZE:
// 这里的注释去掉了。
size.cx=LOWORD(lParam);
size.cy=HIWORD(lParam);
return 0; case WM_LBUTTONUP:
bDown = 0;
return 0; case WM_MOUSEMOVE: // 鼠标移动
if(bDown==1)
{
BitBlt(hdcMem1,0,0,0,0,hdcMem2,0,0,SRCCOPY);
lp.x = LOWORD(lParam);
lp.y = HIWORD(lParam); // hdc = GetDC(hwnd); hpen=CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
SelectObject(hdcMem1,hpen); MoveToEx(hdcMem1,pt.x,pt.y,NULL);
LineTo(hdcMem1,lp.x,lp.y);
BitBlt(hdcMem2,0,0,size.cx,size.cy,hdcMem1,0,0,SRCCOPY);
// 这里的注释也去掉了。
InvalidateRect(hwnd, NULL, FALSE); }
// InvalidateRect(hwnd, NULL, TRUE);
return 0; case WM_DESTROY: PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}