Creating a window using CreateWindowEx without an icon
Asked Answered
P

2

5

With C#, I was easily able to get the effect I wanted:

standard window without icon in title bar

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:

standard window WITH an icon in the title bar

which still has an icon in the title bar and is not what I wanted.

Percheron answered 5/2, 2011 at 6:19 Comment(0)
P
7

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);
Persuasive answered 5/2, 2011 at 6:30 Comment(4)
I tried that, but then the window had no close button. localhostr.com/files/k3WzHR6/Stuff.pngPercheron
@myeviltacos: I suppose I missed the point of the question. Removing the system menu does in fact remove the close box; that's by design. You're actually looking for a dialog window to emulate the similar style in WinForms. A standard main window won't do what you're hoping. I've updated my answer.Persuasive
@myeviltacos: Sorry, the confusion on my part was whether or not you've specified an icon when you registered the window class. That changes the behavior, obviously. You didn't show that code in your question. I think I've addressed that sufficiently now.Persuasive
WS_EX_DLGMODALFRAME is what I was looking for.Kuth
N
2

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.

Needlework answered 5/2, 2011 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.