如何在窗口上绘制图像? [英] How to draw image on a window?

查看:145
本文介绍了如何在窗口上绘制图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows Vista上使用C ++在VS2005中使用createwindow()api创建了一个窗口

I have created a window with createwindow() api using VS2005 in C++ on Windows Vista

我的要求是在该窗口上绘制任何格式的图像.我在此应用程序中未使用任何MFC.

My requirement is to draw an image (of any format) on that window. I am not using any MFC in this application.

推荐答案

不确定是否是您的问题:在表单上绘制位图,或者您想知道如何使用各种图像格式,或者同时使用这两种格式.无论如何,下面是一个如何加载位图并将其绘制在表单上的示例:

not exactly sure what is your problem: draw a bitmap on the form, or you would like know how to work with various image formats, or both. Anyways below is an example of how you could load a bitmap and draw it on the form:

HBITMAP hBitmap = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;

    switch (message)
    {
<...>

    case WM_CREATE:
        hBitmap = (HBITMAP)LoadImage(hInst, L"c:\\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        break;
    case WM_PAINT:
        PAINTSTRUCT     ps;
        HDC             hdc;
        BITMAP          bitmap;
        HDC             hdcMem;
        HGDIOBJ         oldBitmap;

        hdc = BeginPaint(hWnd, &ps);

        hdcMem = CreateCompatibleDC(hdc);
        oldBitmap = SelectObject(hdcMem, hBitmap);

        GetObject(hBitmap, sizeof(bitmap), &bitmap);
        BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

        SelectObject(hdcMem, oldBitmap);
        DeleteDC(hdcMem);

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

LoadImage加载图标,光标,动画光标或位图.详细信息此处

LoadImage loads an icon, cursor, animated cursor, or bitmap. Details here

要使用各种图像格式,可以使用Windows Imaging Component(请参阅加载JPEG和GIF图片或第三方工具,例如 FreeImage LeadTools

For working with various images formats you can use Windows Imaging Component (see IWICBitmapDecoder) or code from here Loading JPEG and GIF pictures or 3rd party tools like FreeImage or LeadTools

希望这会有所帮助

这篇关于如何在窗口上绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆