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.
Related
I'm noticing that standard edit controls are not showing the last line of text if it's not fully visible. Is there a way to override this behavior? If not, is there an equivalent solution to an edit box that does? Preferably in "pure" Win32 API fashion, as I am not using MFC or ATL. This is how I define the edit control at the moment:
CreateWindow(L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, 0, 0, 0, 0, hWnd, (HMENU)ID_EDIT_CONTROL, hInst, NULL);
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 using the following code to create a Window:
CreateWindowEx(0, "WinClass", "My Title", WS_OVERLAPPED | WS_SYSMENU, 261, 172, 594, 384, NULL, NULL, hInstance, NULL);
I get different Window sizes under Windows XP and Windows 7:
However, when I add the WS_THICKFRAME style, the problem is solved:
CreateWindowEx(0, "WinClass", "My Title", WS_OVERLAPPED | WS_SYSMENU | WS_THICKFRAME, 261, 172, 594, 384, NULL, NULL, hInstance, NULL);
But unfortunately WS_THICKFRAME makes the Window resizable which I don't want.
Edit:
What I want it to remain the same size across different versions of Windows is the client area.
I think you are looking for AdjustWindowRect function:
Calculates the required size of the window rectangle, based on the
desired client-rectangle size. The window rectangle can then be passed
to the CreateWindow function to create a window whose client area is
the desired size.
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.
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.