此代码不起作用。它必须将屏幕复制到程序窗口。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static wchar_t szAppName[]=TEXT("App");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(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;
wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
SetTimer(hwnd,0,100,NULL);
while(BOOL lol=GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HDC cdc;
HBITMAP hBmp;
HDC scr=GetDC(0);
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(iMsg)
{
case WM_CREATE:
cdc=CreateCompatibleDC(scr);
hBmp=CreateCompatibleBitmap(scr,500,500);
SelectObject(cdc,hBmp);
return 0;
case WM_PAINT:
{
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
}
case WM_TIMER:
{
hdc=GetDC(hwnd);
BitBlt(cdc,0,0,500,500,scr,0,0,SRCCOPY);
HDC compatibleDC=CreateCompatibleDC(scr);
SelectObject(compatibleDC,hBmp);
BitBlt(hdc,0,0,500,500,compatibleDC,0,0,SRCCOPY);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
内部处理WM_TIMER有行BitBlt(hdc,0,0,500,500,compatibleDC,0,0,SRCCOPY);
,我使用compatibleDC将位图从它复制到窗口的DC。但是,复制不会发生。为什么会这样。我同时在两个DC中选择HBITMAP。可能吗?或是导致这种行为的错误?