Mic*_*ell 29 wpf xaml dialog titlebar application-icon
众所周知,如果未定义wpf窗口的图标,则会显示默认图标.我想在标题栏中显示一个没有任何图标的窗口.我意识到我可以使用空白图像,但这会导致标题栏中的文本偏移到右侧.
有谁知道完全删除图标的方法?
(我试图寻找类似的问题,但找不到任何东西.)
Nir*_*Nir 27
很简单,将此代码添加到您的窗口:
[DllImport("user32.dll")]
static extern uint GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
private const int GWL_STYLE = -16;
private const uint WS_SYSMENU = 0x80000;
protected override void OnSourceInitialized(EventArgs e)
{
    IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
    SetWindowLong(hwnd, GWL_STYLE,
        GetWindowLong(hwnd, GWL_STYLE) & (0xFFFFFFFF ^ WS_SYSMENU));
    base.OnSourceInitialized(e);
}
And*_*rej 17
虽然不是一个恰当的解决方案,但您可以尝试以下方法之一:
将WindowStyle-Property设置为ToolWindow会使Icon消失,但标题栏(显然)会更小.
为整个Window编写一个ControlTemplate.根据Window是否必须看起来像"真实"窗口,尝试在模板中重新创建默认样式需要付出很多努力.
Bre*_*yan 10
我知道这已得到解答,但是Dan Rigsby的博客上有一篇文章,展示了如何在不隐藏最小化/最大化框的情况下执行此操作.
我发现这让我感到很沮丧,因为我正在使用这些文章(这里和这里,但它隐藏了系统菜单时隐藏了所有按钮,以帮助我创建这个帮助器,如上所示调用OnSourceInitialized.
public static class WpfWindowHelper {
    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr hwnd, int index);
    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_DLGMODALFRAME = 0x0001;
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOMOVE = 0x0002;
    public const int SWP_NOZORDER = 0x0004;
    public const int SWP_FRAMECHANGED = 0x0020;
    public const int GWL_STYLE = -16;
    public const int WS_MAXIMIZEBOX = 0x00010000;
    public const int WS_MINIMIZEBOX = 0x00020000;
    public const int WS_SYSMENU = 0x00080000;
    public static void HideSysMenu(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
        SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    }
    public static void HideMinimizeBox(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MINIMIZEBOX));
    }
    public static void HideMaximizeBox(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX));
    }
    public static void HideMinimizeAndMaximizeBoxes(this Window w) {
        IntPtr hwnd = new WindowInteropHelper(w).Handle;
        SetWindowLong(hwnd, GWL_STYLE,
            GetWindowLong(hwnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX));
    }
}
| 归档时间: | 
 | 
| 查看次数: | 20199 次 | 
| 最近记录: |