-2

I am able to add image to the button as background but later I want to add text to the button as "Weclome", I tried all possible ways using Settext, SendmessageA. please help

#include <Windows.h>

int main()
{
    MSG msg;
    HWND hWnd = CreateWindow(TEXT("button"), TEXT("START"), WS_VISIBLE | WS_POPUP | WS_CHILD | WS_TABSTOP | BS_BITMAP,
        250, 250, 500, 500, NULL, NULL, NULL, NULL);
    HANDLE hImg = LoadImageW(NULL, L"Untitled.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
    SendMessageW(hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImg);

    SendMessageA(hWnd, WM_SETTEXT, 0, (LPARAM)"Welcome");
    //SendMessageW(hWnd, WM_SETTEXT, (WPARAM) 256,NULL);


    while (GetMessage(&msg, NULL, 0, 0))
    {
        ShowWindow(hWnd, SW_SHOW);
        UpdateWindow(hWnd);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • I am not sure if you have xaml in c++. But in Wpf C# I would put a StackPanel inside my Button using the xaml. Then inside the StackPanel I would put both Text control and Image control andthen play with their visibility On/Off from code behin. – Khalil Khalaf Jul 27 '16 at 16:41
  • Probably need to remove the `BS_BITMAP` style from the window. – Captain Obvlious Jul 27 '16 at 16:43
  • @firststep this is not wpf – David Heffernan Jul 27 '16 at 17:52
  • @FirstStep: The Windows API is not the Windows Runtime is not .NET. But sure, you can access Direct2D from C++. Not that it's going to be helpful in solving this issue, but it's possible. And yes, you can use XAML in a C++ WinRT application. But that's a different XAML from WPF or Silverlight. Anyway, all of this is completely unrelated. – IInspectable Jul 27 '16 at 18:18
  • Well I thought to give an idea of possibly doing this my way (To put both of them at the same time, and control their visibility). And I mentioned xaml because (I think) it is easier to see/understand the idea using xaml (wpf). Well thank you for informing me then I just wanted to help and now I learned something new :) @IInspectable – Khalil Khalaf Jul 27 '16 at 18:21
  • 1
    @DavidHeffernan Thank you for pointing this out. I did not say it is a wpf. – Khalil Khalaf Jul 27 '16 at 18:21
  • 2
    That code is so wrong, it boggles the mind. You cannot create a `WS_CHILD` window, and then pass `NULL` as the parent. And its totally unclear, why you call `ShowWindow` and `UpdateWindow` every time you handle a message. Since you don't seem to know what you are doing, consider getting a book. Petzold's [Programming Windows®](https://www.amazon.com/dp/157231995X) is fine. – IInspectable Jul 27 '16 at 18:21
  • @firststep so why are you talking about WPF and XAML then? – David Heffernan Jul 27 '16 at 19:02

1 Answers1

0

You do realize you have to create a window and then put the button inside the window?

Chances are the program is looking for a file in the wrong directory. Use full path names and do error checking to make sure the bitmap is loaded. Example:

HANDLE hImg = LoadImageW(NULL, L"c:\\fullpath\\Untitled.bmp", IMAGE_BITMAP, 0, 0, 
LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);

if (!hImg)
    report error...

Don't put ShowWindow and UpdateWindow in the message loop. Just show the window and then call the message loop. Example:

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • I can add the bitmap image to the button but I also want to see the text on the image button – test test Jul 28 '16 at 02:02
  • Remove `BS_BITMAP` from button style. Only use `SendMessage(... BM_SETIMAGE ...)` to assign the bitmap. Your code has more serious problems which I didn't notice before. See comments earlier about reading a tutorial on Windows programming. – Barmak Shemirani Jul 28 '16 at 03:42
  • I am so sorry, I removed BS_BITMAP after that It is removing the image from the button and adding text. but I need both image and text. However I just started learning the code from given books – test test Jul 28 '16 at 04:54
  • Call `SendMessage(... BM_SETIMAGE ...)` to assign the bitmap. The button will show both bitmap and text (text will be to the right of bitmap). See the "Remarks" section in official [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761822(v=vs.85).aspx) But first you have to create a window, then create a button inside the window. – Barmak Shemirani Jul 28 '16 at 05:01
  • Seems that I cant make it work, Need to learn from the scratch just for this basic GUI code. I am afraid that I wont be submit my secure source code analyzer to the management. I am basically security tester and i develop background automation security scripts – test test Jul 28 '16 at 05:39