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.
Related
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.
At the moment I am using the following code:
hwndB11 = CreateWindow ("BUTTON", "",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP,
B11x, B11y, B11l, B11h, hwnd, (HMENU) BUTTON11,
NULL, //((LPCREATESTRUCT) lParam)->hInstance,
NULL);
SendMessage(hwndB11,
BM_SETIMAGE,
IMAGE_BITMAP,
(LPARAM)LoadBitmap(g_hInst, MAKEINTRESOURCE(SWITCH_OFF)));
How do I modify the code so that the bitmap is stretched to fit onto a Windows button? Any help would be gratefully received.
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;
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
When I tried to set my static control position at 0, 0 and size the same as window's size everything worked just fine. Control was placed the same as window, but when i tried to make control smaller and place it so that there's equal amount of space on every side of control it didn't work. At the top there was more space than at the bottom and on the left side ther was more space than on the right side. Could you please tell me how to do what i wanted to do? Code i was using:
1: ghStatic = CreateWindowExW(WS_EX_CLIENTEDGE, L"STATIC", L"Foo", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 0, 300, 300, hwnd, nullptr, hInstance, nullptr);
2: ghStatic = CreateWindowExW(WS_EX_CLIENTEDGE, L"STATIC", L"Foo", WS_CHILD | WS_VISIBLE | SS_CENTER, 75, 75, 150, 150, hwnd, nullptr, hInstance, nullptr);
Are do you using the CreateWindowExW function to change the size of your hwnd? You cannot use this function to change the hwnd size, it is used to create controls, because it will be trying to recreate what was already created.
Try use MoveWindow function to change the position and size of hwnd:
MoveWindow(YourHWND, x, y, w, h, TRUE);
MoveWindow(ghStatic, 75, 75, 150, 150, TRUE);