win32 桌面图像获取问题
代码如下,在执行之后还是没有办法获取桌面的图像,求大神帮忙看看
HDC mem_dc;
SIZE screen_size;
void
screen_caption(){
HDC desk_dc = CreateDC(L"DISPLAY", 0, 0, 0);
mem_dc = CreateCompatibleDC(desk_dc);
screen_size.cx = GetDeviceCaps(desk_dc, HORZRES);
screen_size.cy = GetDeviceCaps(desk_dc, VERTRES);
HBITMAP hBmp = CreateCompatibleBitmap(desk_dc, screen_size.cx, screen_size.cy);
SelectObject(mem_dc, hBmp);
BitBlt(mem_dc, 0, 0, screen_size.cx, screen_size.cy, desk_dc, 0, 0, SRCCOPY);
}
LRESULT CALLBACK
ScreenProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message) {
case WM_CREATE:
screen_caption();
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
BitBlt(hdc, 0, 0, screen_size.cx, screen_size.cy, mem_dc, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
case WM_KEYDOWN:
switch (wparam) {
case VK_ESCAPE:
ShowWindow(hwnd, SW_HIDE);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
再补充一下,我是想在子窗口上显示桌面图像,下面是我建立子窗口的函数
void
registe_sreenshoot_window(HINSTANCE hInstance) {
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = ScreenProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = sizeof(HANDLE);
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 = szScreenClassName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, "注册失败", "错误", MB_ICONERROR);
return;
}
}
void
create_screenshoot_window(HWND *hwnd, HWND phwnd) {
*hwnd = CreateWindow(szScreenClassName, TEXT("Child Win"),
WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
phwnd, NULL, hInst, NULL);
}
void
show_screenshoot_window(HWND hwnd) {
ShowWindow(hwnd, SW_MAXIMIZE);
UpdateWindow(hwnd);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到问题所在了,在创建memDc和hBmp时应该与当前窗口dc相兼容,而不是创建与桌面dc相兼容的dc、bitmap