How to display image on button - c++

I created a button and I want to display an image on it. After some research I found the following code:
HBITMAP buttonImage;
buttonImage = (HBITMAP)LoadImageW(NULL,L"Button.bmp",IMAGE_BITMAP,95,35,LR_LOADFROMFILE);
HWND playBTN = CreateWindow(TEXT("button"),NULL,WS_VISIBLE | WS_CHILD | BS_BITMAP,330, 400, 95, 35, hwnd, (HMENU)1, NULL, NULL);
SendMessageW(playBTN,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM) buttonImage);
The code runs succesfuly but there isnt any change at the button
I also tried using LR_DEFAULTCOLOR,LR_VGACOLORand LR_LOADMAP3DCOLORS but nothing changed.
Note: The image I want to display is a box with green color

Related

Text box in menu win32 C++

I have a windows app on which when I do a right click a lot of options are shown in the pop up context menu. How can one place an edit text box, in the context menu so that I can type something (and on the change of text box disable few menu items ) . I tried something like this . This doesn't work.
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"),
WS_CHILD | WS_VISIBLE, 100, 20, 140,
20, NULL, NULL, NULL, NULL);
AppendMenu(MyLocalRightClick, MF_STRING, (UINT)hWndEdit,NULL);

How to get text from a textbox in the Windows API

I've been scratching my head over this for a week now. I'm using the Windows API and I made a textbox with
editBox = CreateWindowEx(WS_EX_PALETTEWINDOW, TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | ES_MULTILINE | ES_LEFT | ES_AUTOVSCROLL, 175, 110, 140, 150, hwnd, (HMENU) ID_TEXT, NULL, NULL);
and a button
button = CreateWindowEx(WS_EX_PALETTEWINDOW, "BUTTON", "Ok", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 175, 260, 140, 20, hwnd, (HMENU) ID_BUTTON, NULL, NULL);
and I want to be able to click the button and it store what was typed into editBox in a primitive string. Examples are preffered but links and documentation are also very much appreciated! :)
You can use the GetWindowText API function to get the text of an edit control.
For a window with title this function retrieves the title. For an edit control it retrieves the edit control's text. Just ignore any documentation saying that you're limited to 64K or so, if you encounter it (it was once that way).
To detect the button click, process the WM_COMMAND window message in your window procedure; it's sent to the parent window of the button. There is a more sophisticated approach based on reflecting the message back to button, which can then process it itself, and that approach is used in most higher level frameworks. But at the API level, just check WM_COMMAND in the parent window's window procedure.

C++ Button with transparent background

I'm creating a basic application in C++ (Win32 API). I tried to make a button with the CreateWindow() function as seen below:
button1 = CreateWindow("button", TEXT("Click Me!"), WS_TABSTOP | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 200, 70, 25, hwnd, (HMENU)NULL, NULL, NULL);
The problem is that there is a white border around the button as you can see here:
Link
I tried to make it transparent with this code but is doesn't work.
case WM_CTLCOLORBTN:{
HBRUSH hBrushbtn;
hBrushbtn = (HBRUSH)GetStockObject(NULL_BRUSH);
SetBkMode((HDC) wParam, TRANSPARENT);
return ((LRESULT)hBrushbtn);
break;
}
How can I do this?
Thanks
SetBkMode() affects whether or not text rendering is transparent or not, which is not the issue here.
You're going to have to WM_PRINTCLIENT up to the parent control into the button's DC, as I demonstrate here. (Note that my code still calls SetBkMode() for transparent text in checkboxes, groupboxes, labels, etc.; for pushbuttons it won't matter if you're using visual styles).

custom cursor not showing on custom button

I registered a custom button in WM_CREATE but my custom cursor resource is not recognized and I am getting a double arrow cursor when hovering over the custom buttons that I create at run time. Any solutions? I have included resource.h and triplechecked that the custom resource with IC_CURSOR2 is there in the resources.
WNDCLASSEX buttonx; //subclass our custom buttons
buttonx.cbSize = sizeof(WNDCLASSEX);
GetClassInfoEx(NULL,TEXT("BUTTON"), &buttonx);
buttonx.lpszClassName = "CustomButton";
buttonx.hInstance = hInst;
buttonx.hCursor = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR2));
RegisterClassEx(&buttonx);
hButton1 = CreateWindowEx(NULL, "CustomButton", "Close", WS_CHILD
| WS_VISIBLE | BS_OWNERDRAW, 410, 570, 100, 30, hWnd,
(HMENU)ID_BUTTON1, g_hInstance, NULL);
I am getting a double arrow cursor
That sounds like one of the stock cursors, like IDC_SIZENS. Which indicates that you hInst variable is NULL. Maybe you should have used g_hInstance, it isn't clear from the question.

How to make an icon button in C++

I know how to draw a button in C++ but how would i make an icon on it can someone post source or give reference please? by SendMessage() or if not that way just please paste
Please need easier anwsers without so many files im new a bit
Since you're new, you may also wish to consult the MSDN Library. You can find information on Button Styles (see, specifically, the BS ICON and BS BITMAP styles) and the BM_SETIMAGE message .
If you use MFC then I would recommend you to use the following CButton method SetIcon:
CButton myButton;
// Create an icon button.
myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_ICON,
CRect(10,10,60,50), pParentWnd, 1);
// Set the icon of the button to be the system question mark icon.
myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );
This works very well.
send BM_SETIMAGE message, and pass loaded image handle to lParam.
button1 = CreateWindowW(L"BUTTON", L"&Button", WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_BITMAP, 20, 50, 80, 25, hwnd, (HMENU) 600, NULL, NULL);
hImg = LoadImageW(NULL, L"test123.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessageW(button1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImg);
P.S: you need to use BS_BITMAP flag when CreateWindow()