Win 32 GUI c ++ .bmp图像未显示

时间:2018-01-06 16:03:21

标签: c++ winapi bitmap bmp win32com

我实际上是在按照教程进行操作。我真的想得到答案,因为我需要在窗口上添加图标。在窗口中显示图像将是第一步。

很抱歉由于某些原因我之前添加的更新没有通过。我的解决方案面向Unicode。

更正的更新文件如下:

#include <windows.h>
#include <commctrl.h>

using namespace std;

LPCWSTR szClassName = L"myWindowClass";

HWND hLogo;
HBITMAP hLogoImage;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void loadPictures();
void parentControls(HWND);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int icmdshow)
{

    HWND hWnd;

    WNDCLASSW wc = { 0 };

    wc.style = 0;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szClassName;
    wc.lpfnWndProc = WndProc;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hInstance = hInstance;
    wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;


    if (!RegisterClassW(&wc))
    {
        const wchar_t Error01[] = L"Register Issue To Check On : ";   /// Notice this 
        const wchar_t Error01_Caption[] = L"Error 01";

        MessageBoxW(NULL, Error01, Error01_Caption, MB_OK | MB_ICONERROR);

        return 0;
    }

    LPCWSTR parentWinTitle = L"My Window";

    hWnd = CreateWindowW(szClassName, parentWinTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 200, NULL, NULL, NULL, NULL);

    if (hWnd == NULL)
    {

        const wchar_t Error02[] = L"Window Creation Issue To Check On : ";
        const wchar_t Error02_Caption[] = L"Window Creation Issue To Check On : ";
        MessageBoxW(NULL, Error02, Error02_Caption, MB_OK | MB_ICONERROR);

    }
    ShowWindow(hWnd, icmdshow);
    UpdateWindow(hWnd);


    MSG msg = { 0 };

    while (GetMessageW(&msg, NULL, 0, 0))
    {

        TranslateMessage(&msg);
        DispatchMessageW(&msg);

    }

    return 0;
}

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

    switch (message)
    {
    case WM_CREATE:
        loadPictures();    /// Must be called first, Calling the Images function in Create
        parentControls(hWnd);
        break;      
/*  case WM_COMMAND:
        switch (wParam)
        {

        }
        break;
*/
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }
    return 0;
}


void parentControls(HWND hWnd)
{
    hLogo = CreateWindowW(WC_STATICW, NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP, 70, 25, 100, 100, hWnd, NULL, NULL, NULL);
    SendMessageW(hLogo, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hLogoImage);
}

void loadPictures()
{   /// bmp image save in file with main.cpp
    LPCWSTR myBmp = L"bitmap1.bmp";
    hLogoImage = (HBITMAP)LoadImageW(NULL, myBmp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}

2 个答案:

答案 0 :(得分:1)

public frmSettings()
{
    InitializeComponent();

    //Subscribe to MouseUp events for key binds
    SubscribeMouseClicks(this, new MouseUpDelegate(frmSettings_MouseUp));
}

private delegate void MouseUpDelegate(object sender, MouseEventArgs e);
private void SubscribeMouseClicks(Control parentControl, MouseUpDelegate mouseUpDelegate)
{
    foreach (Control ctrl in parentControl.Controls)
    {
        if (ctrl.GetType() != typeof(Button))
        {
            ctrl.MouseUp += mouseUpDelegate; //This line causes the error. If I change mouseUpDelegate to frmSettings_MouseUp it works
            if (ctrl.HasChildren)
            {
                SubscribeMouseClicks(ctrl, mouseUpDelegate);
            }
        }
    }
}

case WM_COMMAND: switch(wp) { } break; parentControls(hWnd); <--- never gets here loadPictures(); /// Calling the Images function in Create break; parentControls在此switch语句中永远不会到达。

首先应该调用

loadPictures

删除这两行,将它们放在loadPictures中,如下所示:

WM_CREATE

答案 1 :(得分:0)

有人告诉我答案应该在这里,而不是在上面更新。我相信如果我在这里添加它,我将会意识到这是错误的。我认为这就是为什么在我之前在上面的原帖中尝试更新时没有采用的更新。无论哪种方式,更新/答案都在下面。

#include <windows.h>
#include <commctrl.h>

using namespace std;

LPCWSTR szClassName = L"myWindowClass";

HWND hLogo;
HBITMAP hLogoImage;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void loadPictures();
void parentControls(HWND);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int icmdshow)
{

    HWND hWnd;

    WNDCLASSW wc = { 0 };

    wc.style = 0;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szClassName;
    wc.lpfnWndProc = WndProc;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hInstance = hInstance;
    wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;


    if (!RegisterClassW(&wc))
    {
        const wchar_t Error01[] = L"Register Issue To Check On : ";   /// Notice this 
        const wchar_t Error01_Caption[] = L"Error 01";

        MessageBoxW(NULL, Error01, Error01_Caption, MB_OK | MB_ICONERROR);

        return 0;
    }

    LPCWSTR parentWinTitle = L"My Window";

    hWnd = CreateWindowW(szClassName, parentWinTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 200, NULL, NULL, NULL, NULL);

    if (hWnd == NULL)
    {

        const wchar_t Error02[] = L"Window Creation Issue To Check On : ";
        const wchar_t Error02_Caption[] = L"Window Creation Issue To Check On : ";
        MessageBoxW(0, Error02, Error02_Caption, MB_OK | MB_ICONERROR);

    }
    ShowWindow(hWnd, icmdshow);
    UpdateWindow(hWnd);


    MSG msg = { 0 };

    while (GetMessageW(&msg, NULL, 0, 0))
    {

        TranslateMessage(&msg);
        DispatchMessageW(&msg);

    }

    return 0;
}

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

    switch (message)
    {
    case WM_CREATE:
        loadPictures();    /// Must be called first, Calling the Images function in Create
        parentControls(hWnd);
        break;      
/*  case WM_COMMAND:
        switch (wParam)
        {

        }
        break;
*/
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }
    return 0;
}


void parentControls(HWND hWnd)
{
    hLogo = CreateWindowW(WC_STATICW, NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP, 70, 25, 100, 100, hWnd, NULL, NULL, NULL);
    SendMessageW(hLogo, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hLogoImage);
}

void loadPictures()
{   /// bmp image save in file with main.cpp
    LPCWSTR myBmp = L"bitmap1.bmp";
    hLogoImage = (HBITMAP)LoadImageW(NULL, myBmp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}