c++ - 加载和绘制 bmp win32api c++

标签 c++ winapi

我正在尝试使用这段代码在屏幕上显示一个 bmp:(这段代码来自一本书,但是当我编译时我无法显示窗口,只出现白色窗口)

#include "resource2.h"
#include <vector>
#include <windows.h>

using namespace std;

HWND window1;
HWND window2;
HINSTANCE happ;
HDC handledevice;
PAINTSTRUCT ps;
HBITMAP hbitmap;
BITMAP bitmap;
HDC bmhdc;
HBITMAP oldbm;

LRESULT CALLBACK WindTyp1(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
            if (wparam==VK_ESCAPE)
            {
                DestroyWindow(window1);
            }
            return 0;
        case WM_PAINT:
            handledevice=BeginPaint(hwnd,&ps);
            BitBlt(handledevice,0,0,bitmap.bmWidth,bitmap.bmHeight,bmhdc,0,0,SRCCOPY);
            EndPaint(hwnd,&ps);
            return 0;
    }
    return DefWindowProc(hwnd,msg,wparam,lparam);
}

LRESULT CALLBACK WindTyp2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
            if (wparam==VK_F1)
            {
                DestroyWindow(window1);
            }
            if (wparam==VK_F2)
            {
                DestroyWindow(window2);
            }
            if (wparam==VK_ESCAPE)
            {
                DestroyWindow(window1);
                DestroyWindow(window2);
            }

    }
    return DefWindowProc(hwnd,msg,wparam,lparam);
}

int WINAPI WinMain(HINSTANCE histance,HINSTANCE hprevinstance,PSTR cmdline,int showcmd)
{
    happ=histance;
    MSG msg;
    hbitmap=LoadBitmap(happ,MAKEINTRESOURCE(happ,IDB_BITMAP1));
    GetObject(hbitmap,sizeof(BITMAP),&bitmap);
    bmhdc=CreateCompatibleDC(handledevice);
    SelectObject(bmhdc,&bitmap);

    //clase 1
    WNDCLASS windowstyle1,windowstyle2;
    windowstyle1.cbClsExtra=0;
    windowstyle1.cbWndExtra=0;
    windowstyle1.hbrBackground=(HBRUSH) ::GetStockObject(WHITE_BRUSH);
    windowstyle1.hCursor=::LoadCursor(0,IDC_ARROW);
    windowstyle1.hIcon=::LoadIcon(0,IDI_APPLICATION);
    windowstyle1.hInstance=histance;
    windowstyle1.lpfnWndProc=WindTyp1;
    windowstyle1.lpszClassName="Class 1";
    windowstyle1.lpszMenuName=0;
    windowstyle1.style= CS_HREDRAW | CS_VREDRAW;

    //clase 2
    windowstyle2.cbClsExtra=0;
    windowstyle2.cbWndExtra=0;
    windowstyle2.hbrBackground=(HBRUSH) ::GetStockObject(BLACK_BRUSH);
    windowstyle2.hCursor=::LoadCursor(0,IDC_ARROW);
    windowstyle2.hIcon=::LoadIcon(0,IDI_APPLICATION);
    windowstyle2.hInstance=histance;
    windowstyle2.lpfnWndProc=WindTyp2;
    windowstyle2.lpszClassName="Class 2";
    windowstyle2.lpszMenuName=0;
    windowstyle2.style= CS_HREDRAW | CS_VREDRAW;

    //registrar ambas clases
    RegisterClass(&windowstyle1);
    RegisterClass(&windowstyle2);

    //crear ventanas
    window1=::CreateWindow("Class 1","Ventana Blanca",WS_OVERLAPPEDWINDOW,0,0,1400,1000,0,0,happ,0);
    if (window1==0)
        ::MessageBox(0,"error failed to create window","error",0);

    //Show & Update
    ShowWindow(window1,true);
    UpdateWindow(window1);

    //Message Loop
    ZeroMemory(&msg,sizeof(MSG));
    while (GetMessage(&msg,0,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

最佳答案

看看这个例子 http://www.functionx.com/win32/Lesson13.htm

bmhdc=CreateCompatibleDC(handledevice);

在 WinMain 中,您正在使用尚未初始化的 handledevice,因此选择位图会导致问题,但它可能为 0,因此它会使用桌面 DC。通常我不会在 WinMain 中这样做,而是像 url 中的示例那样在绘画中这样做。

第一个

关于c++ - 加载和绘制 bmp win32api c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882787/

相关文章:

c# - .NET Framework (v 4.0) 从 Win32 C++ 应用程序免费调用 COM 注册

c++ - g++:错误:没有依赖模板参数的 'pow' 参数,因此 'pow' 的声明必须可用 [-fpermissive]

c++ - 返回值 1.#INF000

数组中的 C++ 相等运算符

获取公共(public)库文件夹的 CSIDL 的 C++ Shell 函数

c++ - 常量值运行时评估

multithreading - 有关Win32的多进程窗口所有权的MSDN文档在哪里? (Chrome使用此功能)

c++ - Buit Rectangles(我自己的类型),不会随窗口调整大小?

c++ - 打开转义文件时出现意外行为 :///URL in IE

c++ - FreeLibrary 后堆损坏?