H5W3
当前位置:H5W3 > 其他技术问题 > 正文

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);

}

回答:

找到问题所在了,在创建memDc和hBmp时应该与当前窗口dc相兼容,而不是创建与桌面dc相兼容的dc、bitmap

本文地址:H5W3 » win32 桌面图像获取问题

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址