我正在尝试使用 WPF 创建一个戴尔坞站风格的坞站应用程序,到目前为止进展顺利。
我的 WPF 表单在我的屏幕上显示如下:
我的表单的 XAML 如下:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="130" Width="525" Loaded="Window_Loaded" Background="Transparent" WindowStyle="None">
<Grid>
</Grid>
</Window>
我在微软网站上找到了一个添加 Aero Glass 效果的教程。考虑到这一点,我的代码如下:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
namespace WpfApplication7
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MakeTransparent();
Topmost = true;
var screenWidth = SystemInformation.PrimaryMonitorSize.Width;
var startingPoint = (screenWidth / 2) - (Width / 2);
Top = 2;
Left = startingPoint;
}
private void MakeTransparent()
{
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);
Graphics desktop = Graphics.FromHwnd(mainWindowPtr);
float DesktopDpiX = desktop.DpiX;
float DesktopDpiY = desktop.DpiY;
MARGINS margins = new MARGINS()
{
cxLeftWidth = 0,
cxRightWidth = Convert.ToInt32(Width) * Convert.ToInt32(Width),
cyTopHeight = 0,
cyBottomHeight = Convert.ToInt32(Height) * Convert.ToInt32(Height)
};
int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
}
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
}
}
到目前为止,一切都很好。但是,我的表单是可调整大小的,我不想要,所以我将ResizeMode
属性设置为NoResize
,每当我这样做时,我的表单在启动时无处可寻。
为什么会这样?
我试图通过在SizeChanged
事件中重置表单大小来解决这个问题,但这只有在您尝试从表单的右侧或底部调整大小时,如果您尝试从左上角调整大小,它会移动我没有的表单也不想要。
谢谢