1

ウィンドウの上部にある最小化、最大化、閉じるボタンを非表示にして、アイコンを表示しようとしています。

私はいくつかの異なることを試しましたが、アイコンを保持できません。これは私が取り組んでいるコードです:

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);
}

どんな助けでも大歓迎です!ありがとう!

4

4 に答える 4

0

これは、winforms の閉じるボタンを有効または無効にするために使用したコードです。私はそれがあなたが望むものとは3つの点で異なることを認識しています1)閉じるボタンのみを処理します(ただし、オスカーが正しければ、心配する必要があるのはそれだけです)2)非表示にするのではなく、無効にするだけです/グレーアウトします(ただし、パラメーターを変更して完全に非表示にすることもできます)3)wpfではなくwinform用です

これらの違いはありますが、おそらくコードを見ると、不足しているものを理解するのに役立つでしょう。あなたがそれを理解しているなら、私はあなたの解決策を投稿することに興味があります:)

#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
于 2014-02-27T20:51:48.027 に答える
0

フォーム プロパティでは、たとえば WPF アプリケーションでは、最小化ボタンと最大化ボタンのみを非表示にできます。

ResizeModeというプロパティがあり、NoResizeにするとこの2つのボタンが非表示になります。;)

于 2014-02-27T06:06:12.610 に答える