Search code examples
c++winapibitmapbmpwin32com

Win 32 GUI c++ .bmp The image is not showing


I was actually following a tutorial. I really want to get an answer because I will need to add icons to the window down the line. Getting images to show in the window would be the first step.

Sorry for some reason the update I added did not go through before. My solution is geared towards Unicode.

The corrected updated file is below :

#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);
}

Solution

  • case WM_COMMAND:
        switch(wp)
        {
        }
    break;
    parentControls(hWnd); <--- never gets here
    loadPictures();    /// Calling the Images function in Create
    break;
    

    parentControls and loadPictures are never reached in this switch statement.

    loadPictures should be called first.

    Remove the two lines, put them in WM_CREATE as follows:

    case WM_CREATE:
        loadPictures();    /// Calling the Images function in Create
        parentControls(hWnd);
        break;