How can I do that?
The following seems to work:
CreateWindowEx(WS_EX_TOOLWINDOW, szToolWndClass, "Title",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 240, 320,
hWnd, NULL, hInstance, NULL)
But this doesn't (the window is not displayed):
CreateWindowEx(WS_EX_TOOLWINDOW, szToolWndClass, "Title",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 240, 320,
hWnd, (HMENU) IDD_TOOL, hInstance, NULL)
However, it works when I add the WS_CHILD style, but that doesn't create a floating window.
Top-level windows use the ID slot as the HMENU (or rather, child windows use the HMENU slot as the control ID); so child windows can't have HMENUs, and top-level HWNDs can't have IDs.
What's likely happening is that when you omit WS_CHILD, windows treats the ID as a HMENU, and since it's not a valid HMENU, fails the CreateWindow call.
Generally speaking, an ID only makes sense in the context of a known container. So in the context of a dialog, IDs make sense, because the dialog owns the controls in it, and the author can ensure there's no duplicates - and GetDlgItem will do something sensible.
But on the desktop, every window is from a different source, so there would be no way to ensure unique IDs, so the concept wouldn't really make sense there anyhow.
Your best bet is perhaps to save away the HWND itself, and use it directly when needed.
One thing to be aware of is that when you don't use WS_CHILD, your new window is actually a child of the desktop window, but is owned by the HWND you pass in, it's not a child of that window. Enumerating the child windows of that owner window will not return your new window.
Raymond Chen (who's blog should be considered required reading for all Win32 devs) has a good explanation of the parent vs owner issue here.
Related
I have created a window with WS_CHILD specified inside CreateWindowEx function, as follows:
HWND newEvent = CreateWindowEx(WS_EX_CONTROLPARENT, NewEventClassName, NewEventTitle,
WS_BORDER | WS_OVERLAPPEDWINDOW | WS_CHILD,
CW_USEDEFAULT, CW_USEDEFAULT,
400, 230, hwnd, NULL, NULL, NULL);
Then, I created an Edit Control inside it:
HWND editCtrlEventName;
editCtrlEventName = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"EVENT_NAME",
WS_GROUP | WS_CHILD | WS_VISIBLE |
ES_LEFT | ES_UPPERCASE, 110, 10, 130, 20,
newEvent, (HMENU)2001, NULL, NULL);
When the window is created, the edit control is created, but disabled inside it and I can't enable the control by any means. I already tried to use EnableWindow() function and similars in my Edit Control, but with no success. However, when I delete WS_CHILD style from newEvent, it works perfectly.
I'm a beginner at winapi programming and this behavior is not clear for me. Is there any problem in creating edit controls inside child windows?
HWND newEvent = CreateWindowEx(WS_EX_CONTROLPARENT, NewEventClassName, NewEventTitle,
WS_BORDER | WS_OVERLAPPEDWINDOW | WS_CHILD,
CW_USEDEFAULT, CW_USEDEFAULT,
400, 230, hwnd, NULL, NULL, NULL);
The mistake in the above code is to combine two styles which are mutually exclusive: WS_OVERLAPPEDWINDOW | WS_CHILD.
WS_OVERLAPPEDWINDOW is a style only for top-level windows, and WS_CHILD of course only for child windows.
Also, CW_USEDEFAULT is not valid for a child window.
Other than that, there shouldn't be any problems creating an edit control as the child window of another child window. Windows itself uses this technique in many places, for instance a combobox has an edit control as child window.
How do I make an Edit Control have its width and height as 100% of the window.
A good example of this is the Edit Control in Notepad.
Up till now, I've been setting the size of controls in the CreateWindow() function as static integers.
e.g.
CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE,
0, 0, 640, 480, hWnd, NULL, NULL, NULL);
I am using C and Visual Studio 2015.
Thanks in advance.
You have to handle WM_SIZE messages in your main window procedure and use SetWindowPos API call to resize an edit control.
When a StatusBar is first created, for example:
HWND hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, "", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hWnd, 0, GetModuleHandle(NULL), NULL);
Is it created in simple mode or in multiple-part mode? or maybe this is not guaranteed and it is better to set the mode I want after creating it?
Status bars are created, as you have done, with a call to CreateWindowEx. When a status bar is first created it has no parts. Parts are added to status bars by sending a SB_SETPARTS message to the status bar.
Is it created in simple mode or in multiple-part mode?
Simple mode without parts.
Is it better to set the mode I want after creating it?
Parts can only by specified after the window has been created.
Can anyone give me an example code for a tool window (with pixel dimensions) for the Win32 API?
Thanks
Tool windows are just windows with the WS_EX_TOOLWINDOW extended style:
hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 100, 100, 500, 400, NULL, NULL, hInstance, NULL);
Note you need to use CreateWindow*Ex* to use extended styles. The above is a 500x400 window at 100,100 on the screen.
With C#, I was easily able to get the effect I wanted:
However, I'm having trouble doing the same thing using the Win32 API in C. I don't know how to create a window that has no icon (at all), but still has a caption, a minimize button, and a close button.
I registered my class properly, but I can't figure out what to put for the window styles/extended window styles.
static const TCHAR lpctszTitle[] = TEXT("Stuff"), lpctszClass[] =
TEXT("StuffClass");
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST, lpctszClass,
lpctszTitle, WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, 250, 55, NULL, NULL, hThisInstance, NULL);
The code above produced:
which still has an icon in the title bar and is not what I wanted.
A standard window requires an icon because it needs some form of representation in the taskbar at the bottom of the screen. What should be displayed when you press Alt+Tab in the window switcher if one of the main windows doesn't have an icon?
You need to specify the WS_EX_DLGMODALFRAME extended style. This is the same effect that WinForms sets when you turn off the icon in the title bar.
You also need to make sure that you do not specify an icon when you register the window class. You need to set the hIcon and hIconSm fields of the WNDCLASSEX structure to 0.
Change your code to the following:
static const TCHAR lpctszTitle[] = TEXT("Stuff"), lpctszClass[] =
TEXT("StuffClass");
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST, lpctszClass,
lpctszTitle, WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, 250, 55, NULL, NULL, hThisInstance, NULL);
On a side note, use Spy++ or other similar tool to see the styles that any given HWND actually uses. Point it at your C# window, then duplicate the reported styles in your C code.