Question

I'm developing an MFC application and I've recently been searching for a good method to remove the icon from a Windows title bar, but retain the close button. The two obvious candidate solutions are turning off the system menu style or using the tool window style, but I would prefer not to disable the system menu or use the shrunken tool window title bar. Many MFC applications have this functionality, so I wonder: am I missing some standard way of doing this?

Was it helpful?

Solution

You can use WM_NCRBUTTONDOWN to detect if the user has right-clicked on your caption and then bring up the system menu.

OTHER TIPS

Set WS_EX_DLGMODALFRAME extended style.

You could use a fully transparent icon.

what about getting rid of the system menu and then putting it back in another place yourseld (say next to the close button etc.)?

Without the icon, the only method I could imagine for users to access the system menu is via right-click of the titlebar.

If that's what you had in mind, you could handle WM_RBUTTONDOWN on your main frame and then calculate if the right-click was on the titlebar.

int clickX = GET_X_LPARAM(lParam);
int clickY = GET_Y_LPARAM(lParam);

CRect frameRect;
mainFrame.GetWindowRect(&frameRect);

int titleBarHeight = GetSystemMetrics(SM_CYCAPTION);

if (clickX >= frameRect.left &&
    clickX <= frameRect.right &&
    clickY >= frameRect.top &&
    clickY <= frameRect.top + titleBarHeight)
{
   TrackPopupMenu(m_systemMenu);
}

A sample code in Delphi which removes icon:

const
  WM_ResetIcon = WM_APP - 1;

type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  protected
    procedure WMResetIcon(var Message: TMessage); message WM_ResetIcon;
  end;

implementation

procedure TForm1.FormShow(Sender: TObject);
begin
  PostMessage(Handle, WM_ResetIcon, 0, 0);
end;

procedure TForm1.WMResetIcon(var Message: TMessage);
const
  ICON_SMALL = 0;
  ICON_BIG   = 1;
begin
  DestroyIcon(SendMessage(Handle, WM_SETICON, ICON_BIG, 0));
  DestroyIcon(SendMessage(Handle, WM_SETICON, ICON_SMALL, 0));
end;

A similar code should work for MFC. Basically, you just need to WM_SETICON to NULL in the right place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top