How to set image to the button using Win32APi c++? - c++

I need to set image to the button. The main idea is to make image clickable, and when click on it some event happens.
case WM_CREATE:
HANDLE hBmp = (HBITMAP)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDB_BITMAP1),
IMAGE_BITMAP,
NULL,
NULL,
LR_DEFAULTCOLOR);
HWND hButton = CreateWindow(L"button", L"Label",
BS_BITMAP | WS_VISIBLE |WS_CHILD,
10, 10,
1366, 699,
hWnd, (HMENU)MAP_BUTTON1,
hInst, NULL);
SendMessage(
(HWND)hWnd,
(UINT)BM_SETIMAGE,
(WPARAM)IMAGE_BITMAP,
(LPARAM)hBmp);
break;
After executing this I have something starnge like this

I see the following problems:
You don't do any error checking. Perhaps LoadImage fails. Perhaps CreateWindow fails.
You get the type wrong for hBmp. It should be HBITMAP.
When sending BM_SETIMAGE, you are passing the handle of the button's parent rather than the handle of the button.

to not overwrite #David Heffernan answer, here is a hint;
also, you don't need hinst for the button, just set it to NULL;

Related

How can i put icon on button in c++?

I have a window and there is a button on it. I want to put a icon of question mark on that button,how can i do this??
To create button:-
HWND button = CreateWindow(TEXT("button"), NULL,
WS_VISIBLE | WS_CHILD | BS_ICON, 20, 50, 200, 25,
hWnd, NULL, NULL, NULL);
To load question mark icon:-
HICON hIcon = LoadIcon(NULL, IDI_QUESTION);
To put the icon on button:-
SendMessage(button, WM_SETICON, IMAGE_ICON, (LPARAM)hIcon);
But icon is not appearing on button.
You are using the wrong message to set the icon to the button. You need to use BM_SETIMAGE.

Child window disappears on scrolling

HWND hwndDlg=GetDesktopWindow();
HWND hImage=CreateWindow(_T("STATIC"), _T(""), SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP | WS_CHILD | WS_VISIBLE,
550, 480, 10, 10, hwndDlg, NULL,
(HINSTANCE)GetWindowLong(hwndDlg, GWLP_HINSTANCE),
NULL);
LPWSTR imgPath = getImagePath();
HBITMAP bitmap = (HBITMAP)LoadImageW(NULL,imgPath, IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
SendMessage(hImage, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)bitmap);
With this piece of code i'm able to create a child window to the current window and make a picture appear on that window. It works as expected. But my problem is when i scroll the child window and the picture disappear. What am I doing wrong? I'm totally new with windows programming. Please help me out.
http://i.stack.imgur.com/VO0uD.png
You may see the screenschot in the above link

Drawing image over static control

I'm trying to paint a Picture Box over Static Control, yet the image is not appearing.
The image do exist at the location.
Here is my code :
HWND hwn = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 5, 10, 470, 100, hwnd, NULL, NULL, NULL);
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "D:\\Pic.bmp", IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
SendMessage(hwn, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
First I'm creating a static control (just as the guides in google said), and then I'm painting the image over it... Why isnt it working ? It only draws the static control, but the image isnt being loaded at all...
Thanks!
You need SS_BITMAP style when creating the static control.

Presence of image control in VC++2008

Hello Everyone,
I want to know if there is an image control in VC++ like there is one in VB. Actually using the picture box i face the problem of not being able to re size the image at design time for my dialog. But in image control this is possible. I there is no image control is there a way to check the height and width of a dialog from the dialog editor at design time ???
If you're writing an unmanaged C or C++ project it's a little bit more difficult than using the PictureBox control that would be available when designing managed Windows Forms application, but still doable.
If you are using a DialogBox resource for the window (note: I wrote this part using Visual Studio 2015 as a reference, not 2008, but the general process should be the same):
Insert the image as a resource in your project. Let's say we named the resource for the bitmap IDB_BITMAP1 for simplicity.
Create a new Static child window in the dialog box.
Right click on the new Static window and select Properties.
Under the Misc subheading in Properties, change Type to Bitmap.
Under the Misc subheading in Properties, change Image to IDB_BITMAP1.
If you are hand-coding the window (i.e. manually writing calls to CreateWindow and CreateWindowEx to create the window):
Insert the image as a resource in your project. Make sure to add the line #include "resource.h" to your code.
Get a handle to the bitmap using the LoadBitmap function.
Create the static window as a child window of the main window, and specify the SS_BITMAP window style.
Send the STM_SETIMAGE message to the window using the previously identified handle to the bitmap resource.
Example code, assuming your image is IDB_BITMAP1:
#include <Windows.h>
#include <tchar.h>
#include "resource.h"
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
HWND hWnd, hStcImage;
MSG Msg;
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
// ... register the window class etc
hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, _T("ExampleClassName"), _T("Simple Window"), WS_VISIBLE | WS_SYSMENU, 100, 100, 350, 370, NULL, NULL, hInstance, NULL);
hStcImage = CreateWindow(_T("Static"), NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP, 10, 10, 0, 0, hWnd, NULL, hInstance, NULL);
SendMessage(hStcImage, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

Make a window Static as well as allow adding text using CreateWindowEx()

I'm using CreateWindowEx() function to create an "EDIT" window, i.e. where a user can type.
g_hwndMain = CreateWindowEx(0,
WC_TEXT,
NULL,
WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
0, 0, 400, 200,
phwnd, NULL,
g_hInstance, NULL);
But I would also like the window to be static. Is there a way to do it during the creation of the window? Or any other function that may be used after the creation of the window? I tried using SetWindowPos function after creating the window using SWP_NOSENDCHANGING and SWP_NOREPOSITION, but that didn't o the trick. ANy ideas?
No, I mean Immovable Window. Basically, the window I create should be able to accept text and be immovable at the same time.
You need to handle the WM_WINDOWPOSCHANGING message for that window and then set the SWP_NOMOVE flag of the flags member of the WINDOWPOS structure before you forward it along.
This blog post has an example (though he's preventing size changes, the technique is the same).
Thanks for your help. Ok So far I've done this to handle WM_WINDOWPOSCHANGING message
BOOL OnWindowPosChanging(HWND hwnd, WINDOWPOS *pwp)
{
return 0;
}
LRESULT CALLBACK
WndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch (uiMsg) {
HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, OnWindowPosChanging);
}
return DefWindowProc(hwnd, uiMsg, wParam, lParam);
}
and when I create my window, I do this:
g_hwndMain = CreateWindowEx(0,
TEXT("EDIT"),
NULL,
WS_BORDER,
0, 0, 400, 200,
phwnd, NULL,
g_hInstance, NULL);
if (!g_hwndMain) {
RemoveImages(spHTMLDoc);//Just so I know that the window has been created properly
}
else{
SetWindowPos(g_hwndMain, HWND_TOP, 500, 500, 300, 300, SWP_NOSENDCHANGING | SWP_SHOWWINDOW );
}
The SWP_NOMOVE flag does not let the code change the position of the window, but the user is still able to change the window's position by moving it using a mouse. But this is exactly what I want to prevent. The window should be static. Any thing missing in my code, or any more suggestions?