【问题标题】:Hide minimize, maximize, close buttons from a window and show the icon隐藏窗口中的最小化、最大化、关闭按钮并显示图标
【发布时间】:2014-03-30 06:54:53
【问题描述】:

我试图隐藏窗口顶部的最小化、最大化和关闭按钮,但仍然显示我的图标。

我尝试了几种不同的方法,但无法让图标保持不变。这是我正在使用的代码:

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x00080000;

[DllImport("user32.dll")]
private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hwnd, int index);

public Window()
{
    SourceInitialized += MainWindow_SourceInitialized;
    InitializeComponent();

    Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute);
    this.Icon = BitmapFrame.Create(iconUri);
}

void MainWindow_SourceInitialized(object sender, EventArgs e)
{
    WindowInteropHelper wih = new WindowInteropHelper(this);
    int style = GetWindowLong(wih.Handle, GWL_STYLE);
    SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);
}

任何帮助将不胜感激! 谢谢!

【问题讨论】:

  • 您是要完全禁用调整大小,还是只删除按钮?
  • 我正在尝试隐藏/删除窗口右上角的所有三个按钮(最小化、最大化和关闭)。

标签: c# wpf interop interopservices


【解决方案1】:

请注意,某些窗口样式在创建窗口后无法更改,但我不知道这是否适用于这些标志...据我所知,如果您的标题栏是由系统绘制的,您要么同时拥有图标和关闭按钮,或者都没有,因为它们都由WS_SYSMENU 窗口样式控制。

【讨论】:

    【解决方案2】:

    在表单属性中,例如在 WPF 应用程序中,您只能隐藏最小化和最大化按钮。

    有一个属性叫ResizeMode,如果你设置为NoResize,这两个按钮就会被隐藏。 ;)

    【讨论】:

      【解决方案3】:

      您可以将XAMLWPF windowWindowStyle 属性设置为None。 即

      WindowStyle="None"
      

      使用代码你可以做同样的事情如下:-

      WindowName.WindowStyle = WindowStyle.None;
      

      它必须隐藏所有三个按钮。

      【讨论】:

        【解决方案4】:

        这是我用来在 winforms 中启用和禁用关闭按钮的代码。我意识到这与你想要的有 3 种不同 1)它只处理关闭按钮(虽然,如果奥斯卡是正确的,这是你唯一需要担心的) 2)它不会隐藏它,它只是将其禁用/变灰(尽管您可以更改参数以完全隐藏它) 3)它是用于winforms,而不是wpf

        尽管存在这些差异,但也许查看代码可以帮助您找出缺少的内容。如果您确实想通了,我会对您发布您的解决方案感兴趣:)

        #region Enable / Disable Close Button
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        
        private const int SC_CLOSE      = 0xF060;
        private const int MF_BYCOMMAND  = 0x0000;
        
        private const int MF_ENABLED    = 0x0000;
        private const int MF_GRAYED     = 0x0001;
        
        protected void DisableCloseButton()
        {
            try
            {
                EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
                this.CloseButtonIsDisabled = true;
            }
            catch{}
        }
        protected void EnableCloseButton()
        {
            try
            {
                EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
                this.CloseButtonIsDisabled = false;
            }
            catch{}
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.CloseButtonIsDisabled)
                this.DisableCloseButton();
            base.OnSizeChanged(e);
        }
        
        #endregion
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-26
          • 2011-03-13
          • 2011-09-25
          • 1970-01-01
          • 2012-02-24
          • 2012-12-16
          • 2017-01-07
          相关资源
          最近更新 更多