I would like to draw my program's icon on an "owner-drawn" button. This could be either an icon in the resource file, or the generic Windows icon. But, even after endless searching, I have not been able to find the code for this. I have come across bits and pieces of answers. But, no complete explanations.
Sorry that I have no code to post. I am totally lost on this one. Either standard Win API or GDI+ will work for me.
When you create the button, add BS_ICON style, then get the handle to your icon, you can use LoadImage.
Finally send BM_SETIMAGE message with the handle to your icon.
Here is the code sample:
HWND button1 = CreateWindow(TEXT("Button"), TEXT("OK"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_ICON,
50, 50, 50, 50, hwnd, (HMENU)1001, hInstance, NULL);
HICON hIcon = (HICON)LoadImage( // returns a HANDLE so we have to cast to HICON
NULL, // hInstance must be NULL when loading from a file
L"iconname.ico", // the icon file name
IMAGE_ICON, // specifies that the file is an icon
0, // width of the image (we'll specify default later on)
0, // height of the image
LR_LOADFROMFILE | // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE | // default metrics based on the type (IMAGE_ICON, 32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
SendMessage(button1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
Related
I have a problem setting an image to static window.
Here is a part of my code:
HBITMAP bitmap = (HBITMAP)LoadImage(NULL, "Tetris/Tetrominoes/T One.bmp", IMAGE_BITMAP, 20, 20, LR_LOADFROMFILE);
one = CreateWindow("Static", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP, (x-10), ((y-y)+50), 20, 20, hMain, NULL,NULL,NULL);
SendMessage(one, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bitmap);
BringWindowToTop(one);
The thing I don't understand is why it isn't loading since I used this type of code before for different purpose. According to all sources this part of code should work fine but my image is not showing. if I remove the SendMessage() function my window is visible but without image and when I put SendMessage() the window just disappears. I tried chaging the image size window size and still nothing.
Thanks in advance!
how do you change the font size of a static text box in a Windows GUI application written in C++?
HWND hText = CreateWindowW(L"EDIT", L"enter some text", WS_VISIBLE | WS_CHILD | ES_RIGHT, 100, 100, 100, 50, hWnd, NULL, NULL, NULL);
do i have to make another Window message
As #RbMm said that, use CreateFont and WM_SETFONT can achieve this. And the official documents also have corresponding introduction.
Changing the Font Used by an Edit Control.
An application can change the font that an edit control uses by
sending the WM_SETFONT message. Most applications do this while
processing the WM_INITDIALOG message. Changing the font does not
change the size of the edit control; applications that send the
WM_SETFONT message may have to retrieve the font metrics for the text and recalculate the size of the edit control. For more
information about fonts and font metrics, see Fonts and Text.
The least code:
LOGFONT logfont;
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.lfHeight = -20;
HFONT hFont = CreateFontIndirect(&logfont);
SendMessage(hText, WM_SETFONT, (WPARAM)hFont, TRUE);
I want to change a Static bitmap image when hovering it. I've tried using a button instead, but the picture never fill the button. I've tried too TrackMouseEvent but I want it to be always active so i cannot use this. Is there a way to get notified when mouse hover a static control or to make a control that behaves like that?
This is my control:
HWND Button = CreateWindow("Static", NULL,
WS_VISIBLE | WS_CHILD | SS_BITMAP | SS_NOTIFY,
20, 240, 120, 20,
hwnd, (HMENU)101, NULL, NULL);
SendMessage(hwnd, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)view.vBitMap);
I use SS_NOTIFY nto get notified when user clicks it or doubleclicks it, but unlike BS_NOTIFY, it don't tell when hovering on WM_NOTIFY
I created an edit control by the following code:
hWnd = CreateWindowExW( 0, // extended styles
L"EDIT",
L"Text in edit.",
ES_LEFT | WS_VISIBLE | WS_CHILD,
10, // x
50, // y
30, // width
100, // height
hWndMainWindow,
NULL,
hInstance,
NULL);
My edit control looks like this:
But most edit controls I see in Windows have a 3D look like in the image below:
I tried several extended and normal window styles, but the didn't do any good. How do I make my edit control look like in the second image?
Some quick links that may help you to take reference:
Window Styles
Extended Window Styles
Edit Styles
You need to include the WS_EX_CLIENTEDGE extended window style.
You say that you have already tried including WS_EX_CLIENTEDGE without success. I'm going to take a wild guess that you have been trying to include it in the window style rather than the extended window style. Your code should look like this:
hWnd = CreateWindowExW(
WS_EX_CLIENTEDGE, // extended styles
L"EDIT",
...
);
I have problems with creating a simple Group-Box-Control via CreateWindowEx. The font-size/-style of its caption just doesn’t look right.
I have created a simple Windows Dialog (containing group-boxes, buttons…) with the Visual Studio - Resource Manager. When I load that dialog with DialogBox(…) everything looks normal but when I create another group-box-control on that same dialog via CreateWindowEx(…) the caption of the new control has a different font-size/-style.
With Microsoft Spy++ I was able to see the dwExStyle and dwStyle values of the other groub-boxes, but even when I use the same values in CreateWindowEx I still get a different look.
Here is the code I use to create the new group-box:
HWND hGroup1 = GetDlgItem(_hWnd, IDC_GROUPBOX1);
HWND hGroup2 = CreateWindowEx(
WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_NOPARENTNOTIFY,
L"Button",
L"Hallo",
WS_CHILDWINDOW | WS_VISIBLE | BS_GROUPBOX,
20, 20, 250, 250,
hGroup1,
nullptr,
_hInstance,
nullptr);
Here is a screen capture of the dialog:
http://imageshack.us/photo/my-images/856/groupboxfontissue.png/
Please let me know where I went wrong and what I can do to fix it.
[EDIT-1]
In regards to Jonathan Potter and Superman, as you suggested I set the font-handle of the new group-box to the same as for the other controls.
HFONT hFont1 = (HFONT)SendMessage(hGroup1, WM_GETFONT, 0, 0);
HFONT hFont2 = (HFONT)SendMessage(hGroup2, WM_GETFONT, 0, 0);
HFONT hFont3 = (HFONT)SendMessage(_hWnd, WM_GETFONT, 0, 0);
SendMessage(hGroup2, WM_SETFONT, (WPARAM)hFont1, TRUE);
hFont2 = (HFONT)SendMessage(hGroup2, WM_GETFONT, 0, 0);
At the end of this code, I can see that all controls and the dialog window have the same font-handle but only the controls which were created with the Resource Manager have the correct font (which is the system font).
Is there anything else I can do???
[EDIT-2]
I cannot believe it… it works now! Thank you all very much for your help!
I just had to set the hWndParent value in CreateWindowEx(…) to the dialog handle and then use WM_GETFONT and WM_SETFONT to copy the right font.
I wish you all a nice weekend.
Controls you create manually (via CreateWindowEx) do not get their font set automatically, and will default to the "system font" (which is what you see in your screenshot). Instead, you need to set the control's font once it has been created. For example,
SendMessage(hGroup2, WM_SETFONT, (WPARAM)SendMessage(hGroup1, WM_GETFONT, 0, 0), TRUE);
When you place a control in a dialog using the resource editor, the font set to the dialog, which is the parent of the control will be used for it by default.
If you're creating a control dynamically, the system font will be used instead of the font of the dialog.
To get the same font of the dialog for a control that you create dynamically, set the font of the dialog to the control in the WM_INITDIALOG handler.
In the code snippet below, replace m_hWnd with the handle of the parent dialog.
HFONT font = (HFONT)::SendMessage(m_hWnd, WM_GETFONT, 0, 0);
::SendMessage(hGroup2, WM_SETFONT, (WPARAM)font, TRUE);