How can i put icon on button in c++? - 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.

Related

Detect mouse click outside child window

I want to destroy a child window when user click somewhere outside its window. I tried to use SetCapture() to detect mouse click. Here is the code:
HWND textbox;
//IN case WM_CREATE:
CreateWindowEx(NULL, L"BUTTON", L"button!",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 150, 40, hwnd,
(HMENU)IDC_TEXTBOX, NULL, NULL);
textbox = CreateWindowEx(
NULL, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER,
100, 100, 300, 200, hwnd, (HMENU)ID_TEXTBOX, NULL, NULL
);
SetWindowText(textbox, L"the initial text");
//IN CASE WM_ONLBUTTONDOWN:
if (textbox != NULL) {
if (SetCapture(textbox) == NULL) {
ReleaseCapture();
DestroyWindow(textbox);
textbox = NULL;
}
}
The window did get destroyed when i clicked outside its region. HOWEVER, when i click the button it didn't get destroyed as i expected. I want the child window to get destroyed when user clicks WHEREVER outside its window. Why did my code fail? And how do i do this right?
EDIT: Here is a screenshot to make things clear. The window only have an edit window with initial text and a button. As mentioned above, the child window disappears when i click outside its window but when i click the button (which is also outside window), the edit window remains.
According to this thread, I find the EN_KILLFOCUS notification which will work for you in the Edit Control Document.

Can't change the background color of windows which are inside another window

I want to change the background color of a STATIC window, both - on load and change it on runtime. So far I have been able to change the color the following way:
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(200, 200, 20));
SetBkColor(hdcStatic, RGB(10, 10, 10));
SetBkMode(hdcStatic, TRANSPARENT);
return (INT_PTR)CreateSolidBrush(RGB(30, 30, 30));
}
Everything works fine and the background color gets changed, except for any STATIC windows, which are inside another static window:
HWND mainContainer = CreateWindowEx
(
0,
_TEXT("STATIC"),
"",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_SOLID,
10, 10, 500, 500,
hwnd,
NULL,
(HINSTANCE)GetWindowLong(hwnd, GWLP_HINSTANCE),
NULL
);
HWND subItem = CreateWindowEx
(
0,
_TEXT("STATIC"),
"SubItem",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_SOLID,
10, 10, 100, 100,
mainContainer,
NULL,
(HINSTANCE)GetWindowLong(mainContainer, GWLP_HINSTANCE),
NULL
);
In this case the mainContainer color gets changed, but not the background color for subItem. Any ideas why this is happening? Thank you!
The message WM_CTLCOLORSTATIC will be sent only to the parent window, but not parent's parent window.
According to About Static Controls :
The window procedure for the predefined static control window class
performs default processing for all messages that the static control
procedure does not process.
The WM_CTLCOLORSTATIC is not in the list that it process. So The predefined window procedure will passes the message to DefWindowProc for default processing.
(We really don't often put a static window inside another static window. This is not a common operation. So you should reset the parent window of subItem to hwnd.)

How do I resize a bitmap to fit into a Windows button, in Dev C++?

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.

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

Up-Down control doesn't show its position in its buddy window

I created an up-down control by the following code.
HWND hEdit, hUpDown;
hEdit = CreateWindowExW(WS_EX_CLIENTEDGE,
L"EDIT",
Content.c_str(),
ES_LEFT | WS_VISIBLE | WS_CHILD,
600,
260,
100,
25,
hWndParent,
NULL,
hInstance,
NULL);
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_UPDOWN_CLASS;
InitCommonControlsEx(&iccx);
hUpDown = CreateWindowExW( 0,
UPDOWN_CLASSW,
L"",
UDS_ARROWKEYS | UDS_ALIGNRIGHT | WS_VISIBLE | WS_CHILD,
0,
0,
0,
0,
hWndParent,
NULL,
hInstance,
NULL);
SendMessageW(hUpDown, UDM_SETBUDDY, (WPARAM) hEdit, (LPARAM) NULL);
SendMessageW(hUpDown, UDM_SETRANGE32, (WPARAM) 0, (LPARAM) 100);
Sleep(5000);
SendMessageW(hUpDown, UDM_SETPOS32, (WPARAM) NULL, (LPARAM) 20);
Sleep(5000);
SendMessageW(hUpDown, UDM_SETPOS32, (WPARAM) NULL, (LPARAM) 60);
I checked the return values of the SendMessageW() functions. They terminate successfully by returning the previous position value as documented.
The created up-down control looks normal:
The problem is, sending the UDM_SETPOS32 message, clicking the up and down arrows and pressing the up and down keys on the keyboard have no effect. I can't change the contents of the edit control (the buddy window of the up-down control) without directly typing something into it. It just stays empty.
I am able to type anything in it manually by using keyboard:
How do I change the position/value of this up-down control by pressing keyboard arrow keys, by clicking the arrows in the GUI and by sending UDM_SETPOS32 in the code? What am I missing in my code?
Use the style UDS_SETBUDDYINT to the up-down control while creating it.
From MSDN documentation:
UDS_SETBUDDYINT
Causes the up-down control to set the text of the buddy window (using the WM_SETTEXT message) when the position changes. The text consists of the position formatted as a decimal or hexadecimal string.
Change the creation code of the up-down control like this by adding the UDS_SETBUDDYINT style:
hUpDown = CreateWindowExW( 0,
UPDOWN_CLASSW,
L"",
UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_ALIGNRIGHT | WS_VISIBLE | WS_CHILD,
0,
0,
0,
0,
hWndParent,
NULL,
hInstance,
NULL);