简体   繁体   English

将位图加载到窗口

[英]Loading a bitmap to a window

I want to load a bitmap to a window in my application, I will use GDI to draw the bitmap to the window. 我想将位图加载到应用程序的窗口中,我将使用GDI将位图绘制到窗口中。 Should I also draw the bitmap when handling the WM_PAINT message so that the bitmap persists on the window? 处理WM_PAINT消息时是否还应该绘制位图,以使位图保留在窗口上?

Yes, you should draw your bitmap in WM_PAINT or WM_ERASEBKGND handler just like this: 是的,您应该在WM_PAINTWM_ERASEBKGND处理程序中绘制位图,如下所示:

        switch(Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            break;
        case WM_PAINT:
            hDC = BeginPaint(hWnd, &Ps);

            // Load the bitmap from the resource
            bmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_MY_COOL_PIC));
            // Create a memory device compatible with the above DC variable
            MemDC = CreateCompatibleDC(hDC);
            // Select the new bitmap
            HBITMAP hOldBmp = SelectObject(MemDC, bmp);

            // Copy the bits from the memory DC into the current dc
            BitBlt(hDC, 10, 10, 450, 400, MemDC, 0, 0, SRCCOPY);

            // Restore the old bitmap
            SelectObject(MemDC, hOldBmp);
            DeleteObject(bmp);

            DeleteDC(MemDC);
            EndPaint(hWnd, &Ps);
            break;
        default:
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM