wpf this.Loaded 作用
时间: 2023-09-27 15:11:56 浏览: 62
`this.Loaded` 是 WPF 中一个事件,当一个控件被加载并准备好被使用时,就会触发该事件。`this.Loaded` 事件的作用是在控件加载完成后执行一些初始化工作,例如为控件的属性赋值、为控件添加事件等。可以通过在 XAML 中添加 `Loaded` 属性或在代码中重写 `OnLoaded` 方法来处理该事件。
相关问题
wpf在winform显示位置不对
当在Windows窗体中使用ElementHost控件显示WPF窗口时,有可能出现WPF窗口位置不正确的问题。这可能是由于Windows窗体和WPF窗口之间的坐标系不同所致。
为了解决这个问题,你可以在WPF窗口的Loaded事件中设置窗口的位置,使其与ElementHost控件对齐。以下是一个示例代码:
```
private void MyWpfWindow_Loaded(object sender, RoutedEventArgs e)
{
PresentationSource source = PresentationSource.FromVisual(this);
if (source != null)
{
Matrix m = source.CompositionTarget.TransformToDevice;
Point topLeft = m.Transform(new Point(0, 0));
HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(this);
if (hwndSource != null)
{
double dpiX = hwndSource.CompositionTarget.TransformToDevice.M11;
double dpiY = hwndSource.CompositionTarget.TransformToDevice.M22;
Point location = new Point(this.Left * dpiX + topLeft.X, this.Top * dpiY + topLeft.Y);
Point targetLocation = PointToScreen(new Point(0, 0));
double offsetX = targetLocation.X - location.X;
double offsetY = targetLocation.Y - location.Y;
hwndSource.Handle.SetWindowPos(IntPtr.Zero, (int)(Left * dpiX + offsetX), (int)(Top * dpiY + offsetY), 0, 0, SWP.NOSIZE | SWP.NOZORDER);
}
}
}
```
在这个示例中,MyWpfWindow是你自己定义的WPF窗口类。在WPF窗口的Loaded事件中,使用PresentationSource和HwndSource获取WPF窗口的设备坐标和窗口句柄,并计算出WPF窗口与ElementHost控件之间的偏移量。然后,使用Windows API函数SetWindowPos将WPF窗口移动到正确的位置。
注意,由于WPF窗口和Windows窗体之间的坐标系不同,需要进行一些计算才能正确地将WPF窗口移动到ElementHost控件的位置。此外,使用Windows API函数SetWindowPos可能会影响WPF窗口的性能,因此需要谨慎使用。
wpf 最小化托盘 demo
WPF最小化托盘(System Tray)的实现可以通过以下步骤来完成:
1. 首先,在XAML文件中创建一个托盘图标。可以使用Rectangle、Ellipse或Image等控件来作为图标,然后设置图标的宽度、高度和颜色等属性。
```xml
<Rectangle Width="16" Height="16" Fill="Red" />
```
2. 在MainWindow.xaml.cs文件中,首先添加以下命名空间:
```csharp
using System.Windows.Forms;
```
3. 在MainWindow.xaml.cs文件中,创建一个NotifyIcon对象,并在窗口加载事件中进行初始化,包括设置托盘图标和右键菜单等属性。
```csharp
private NotifyIcon notifyIcon;
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new System.Drawing.Icon("Icon.ico");
notifyIcon.Visible = true;
notifyIcon.Text = "My WPF Application";
ContextMenu menu = new ContextMenu(); // 创建右键菜单
MenuItem showMenuItem = new MenuItem();
showMenuItem.Text = "显示";
showMenuItem.Click += new EventHandler(ShowMenuItem_Click);
menu.MenuItems.Add(showMenuItem);
MenuItem exitMenuItem = new MenuItem();
exitMenuItem.Text = "退出";
exitMenuItem.Click += new EventHandler(ExitMenuItem_Click);
menu.MenuItems.Add(exitMenuItem);
notifyIcon.ContextMenu = menu;
}
```
4. 创建点击事件处理方法来处理托盘图标的左键双击事件,以及右键菜单中的"显示"和"退出"事件。
```csharp
private void TrayIcon_DoubleClick(object sender, EventArgs e)
{
this.Show(); // 双击托盘图标时显示主窗口
this.WindowState = WindowState.Normal;
}
private void ShowMenuItem_Click(object sender, EventArgs e)
{
this.Show(); // 右键菜单中点击"显示"时显示主窗口
this.WindowState = WindowState.Normal;
}
private void ExitMenuItem_Click(object sender, EventArgs e)
{
this.Close(); // 右键菜单中点击"退出"时关闭应用程序
}
```
5. 最后,在MainWindow.xaml文件中的Window标签中添加Loaded事件调用MainWindow_Loaded方法。
```xml
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF最小化托盘Demo" Height="450" Width="800" Loaded="MainWindow_Loaded">
<!-- 主窗口内容 -->
</Window>
```
通过以上步骤,我们可以实现一个简单的WPF最小化托盘Demo,点击托盘图标可以显示主窗口,右键菜单中可以选择显示或退出应用程序。