如何在Windows窗口中插入图片

然后显示图片需要写在WM_PAINT消息里:

首选使用函数LoadImage载入位图或图标。 

hb_icon = (HBITMAP)::LoadImage(hInst, _TEXT("icon.bmp"), IMAGE_BITMAP, 36, 36, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);

    HDC hMemDC = CreateCompatibleDC(hdc); //创建内存DC

    ::SelectObject(hMemDC, hb_icon); //把位图选进内存DC

    BitBlt(hdc, 0, 0, 35, 35, hMemDC, 0, 0, SRCCOPY); //内存DC映射到屏幕DC


列子

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    switch (message)

    {

    case WM_COMMAND:

        {

            HWND wmId = (HWND)LOWORD(lParam);

            switch(LOWORD(wParam)){

            case IDM_ABOUT:

                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

                break;

            case IDM_EXIT:

                DestroyWindow(hWnd);

                break;

            default:

                return DefWindowProc(hWnd, message, wParam, lParam);

            }

break;

        }

    case WM_PAINT:

        {

            PAINTSTRUCT ps;

            HDC hdc = BeginPaint(hWnd, &ps);

        su_icon = (HBITMAP)::LoadImage(hInst, _TEXT("icon.bmp"), IMAGE_BITMAP, 36, 36, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);

            //绘制图标

            HDC hMemDC = CreateCompatibleDC(hdc); //创建内存DC

            ::SelectObject(hMemDC, su_icon); //把位图选进内存DC

            BitBlt(hdc, 10, 75, 35, 35, hMemDC, 0, 0, SRCCOPY); //内存DC映射到屏幕DC

            EndPaint(hWnd, &ps);

            break;

        }

    case WM_DESTROY:

        PostQuitMessage(0);

        break;

    default:

        return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}

你可能感兴趣的:(如何在Windows窗口中插入图片)