Я могу удалить значок окна из окна WPF с помощью WinApi, но снова получаю значок в окне приложения, когда запускаю только исполняемый файл проекта WPF.
Как удалить значок?
Я могу удалить значок окна из окна WPF с помощью WinApi, но снова получаю значок в окне приложения, когда запускаю только исполняемый файл проекта WPF.
Как удалить значок?
Я изменил образец из "LnDCobra", поэтому его можно использовать как присоединенное свойство (как предложил "Томас":
<Window
...
xmlns:i="clr-namespace:namespace-to-WindowEx"
i:WindowEx.ShowIcon = "false"
...
>
Реализация WindowEx:
public class WindowEx
{
private const int GwlExstyle = -20;
private const int SwpFramechanged = 0x0020;
private const int SwpNomove = 0x0002;
private const int SwpNosize = 0x0001;
private const int SwpNozorder = 0x0004;
private const int WsExDlgmodalframe = 0x0001;
public static readonly DependencyProperty ShowIconProperty =
DependencyProperty.RegisterAttached(
"ShowIcon",
typeof (bool),
typeof (WindowEx),
new FrameworkPropertyMetadata(true, new PropertyChangedCallback((d, e) => RemoveIcon((Window) d))));
public static Boolean GetShowIcon(UIElement element)
{
return (Boolean) element.GetValue(ShowIconProperty);
}
public static void RemoveIcon(Window window)
{
window.SourceInitialized += delegate {
// Get this window handle
var hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GwlExstyle);
SetWindowLong(hwnd, GwlExstyle, extendedStyle | WsExDlgmodalframe);
// Update the window non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SwpNomove |
SwpNosize | SwpNozorder | SwpFramechanged);
};
}
public static void SetShowIcon(UIElement element, Boolean value)
{
element.SetValue(ShowIconProperty, value);
}
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
}
}
От WPFTutorial:
Как удалить значок окна WPF
К сожалению, WPF не предоставляет никакой функции для удаления значка окна. Одним из решений может быть установка значка на прозрачный значок. Но таким образом дополнительное пространство между границей окна и заголовком остается.
Лучшим подходом является использование функции, предоставляемой Win32 API для удаления значка.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
IconHelper.RemoveIcon(this);
}
}
Вспомогательный класс, используемый для удаления значка.
public static class IconHelper
{
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOZORDER = 0x0004;
const int SWP_FRAMECHANGED = 0x0020;
const uint WM_SETICON = 0x0080;
public static void RemoveIcon(Window window)
{
// Get this window handle
IntPtr hwnd = new WindowInteropHelper(window).Handle;
// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
// Update the window non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE |
SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
Я использую очень маленькое прозрачное изображение как значок (1x1 px) для окна WPF.
Просто добавьте этот WindowStyle="ToolWindow"
в свойства вашего окна.