C# 当鼠标在Wpf窗口中的控件上移动时,为什么会更改位置
我需要wpf控件上的一些wpf控件,该控件由hwndHost托管,所以我使用子窗口代替在父窗口中包含这些控件。 但当我将控件悬停在子窗口中时,子窗口的位置将更改,并且不会调用activity:LocationChanged。 我很困惑,谁能帮我~ 谢谢 Test1.xamlC# 当鼠标在Wpf窗口中的控件上移动时,为什么会更改位置,c#,.net,wpf,windows,xaml,C#,.net,Wpf,Windows,Xaml,我需要wpf控件上的一些wpf控件,该控件由hwndHost托管,所以我使用子窗口代替在父窗口中包含这些控件。 但当我将控件悬停在子窗口中时,子窗口的位置将更改,并且不会调用activity:LocationChanged。 我很困惑,谁能帮我~ 谢谢 Test1.xaml <Window x:Class="WpfApplication1.Test1" xmlns="http://schemas.microsoft.com/winfx/2006
<Window x:Class="WpfApplication1.Test1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Test1" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="52*"/>
<ColumnDefinition Width="153*"/>
<ColumnDefinition Width="54*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Name="leftListContain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Border Grid.Column="2" Name="rightListContain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Window>
Test1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
namespace WpfApplication1
{
/// <summary>
/// </summary>
public partial class Test1 : Window
{
ControlList leftControlList = null;
ControlList rightControlList = null;
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]
public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr);
public Test1()
{
InitializeComponent();
this.Loaded += ControlListLoadedHandler;
this.SizeChanged += ControlListResizeHandler;
}
private void ControlListLoadedHandler(object sender, EventArgs e)
{
if (leftControlList != null && rightControlList != null)
{
return;
}
Thread t = new Thread(() =>
{
while (!unityControl.isUnityLoaded)
{
Thread.Sleep(10);
}
this.Dispatcher.Invoke(() => {
ControlListLoaded();
});
});
t.Start();
this.WindowState = System.Windows.WindowState.Maximized;
}
private void ControlListLoaded()
{
leftControlList = new ControlList();
leftControlList.Show();
WindowInteropHelper leftParentHelper = new WindowInteropHelper(this);
WindowInteropHelper leftChildHelper = new WindowInteropHelper(leftControlList);
SetParent(leftChildHelper.Handle, leftParentHelper.Handle);
rightControlList = new ControlList();
rightControlList.Show();
WindowInteropHelper rightParentHelper = new WindowInteropHelper(this);
WindowInteropHelper rightChildHelper = new WindowInteropHelper(rightControlList);
SetParent(rightChildHelper.Handle, rightParentHelper.Handle);
ControlListResize();
}
private void ControlListResizeHandler(object sender, EventArgs e)
{
ControlListResize();
}
private void ControlListResize()
{
if (leftControlList == null || rightControlList == null)
{
return;
}
leftControlList.SetSize(leftListContain.ActualWidth, leftListContain.ActualHeight);
Point point = leftListContain.TransformToAncestor(this).Transform(new Point(0, 0));
leftControlList.SetPosition(point.X, point.Y);
rightControlList.SetSize(rightListContain.ActualWidth, rightListContain.ActualHeight);
point = rightListContain.TransformToAncestor(this).Transform(new Point(0, 0));
rightControlList.SetPosition(point.X, point.Y);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用System.Runtime.InteropServices;
使用System.Windows.Interop;
使用系统线程;
命名空间WpfApplication1
{
///
///
公共部分类Test1:Window
{
ControlList leftControlList=null;
控制器右控制器=空;
[System.Runtime.InteropServices.DllImport(“user32.dll”,EntryPoint=“SetParent”)]
公共外部静态IntPtr SetParent(IntPtr childPtr,IntPtr parentPtr);
公共测试1()
{
初始化组件();
this.Loaded+=ControlListLoadedHandler;
this.SizeChanged+=ControlListResizeHandler;
}
私有void ControlListLoadedHandler(对象发送方,事件参数e)
{
if(leftControlList!=null&&rightControlList!=null)
{
返回;
}
线程t=新线程(()=>
{
而(!unityControl.isUnityLoaded)
{
睡眠(10);
}
this.Dispatcher.Invoke(()=>{
ControlListLoaded();
});
});
t、 Start();
this.WindowState=System.Windows.WindowState.Maximized;
}
私有void controllistloadded()
{
leftControlList=新的ControlList();
leftControlList.Show();
WindowInteropHelper leftParentHelper=新的WindowInteropHelper(此);
WindowInteropHelper leftChildHelper=新的WindowInteropHelper(leftControlList);
SetParent(leftChildHelper.Handle,leftParentHelper.Handle);
rightControlList=新的控制器();
右控制论者Show();
WindowInteropHelper rightParentHelper=新的WindowInteropHelper(此);
WindowInteropHelper rightChildHelper=新的WindowInteropHelper(rightControlList);
SetParent(rightChildHelper.Handle,rightParentHelper.Handle);
ControlListResize();
}
私有void controlleristresizehandler(对象发送方,事件参数e)
{
ControlListResize();
}
私有void ControlListResize()
{
if(leftControlList==null | | rightControlList==null)
{
返回;
}
leftControlList.SetSize(leftListContain.ActualWidth,leftListContain.ActualHeight);
Point Point=leftListContain.TransformToAncestor(this.Transform)(新点(0,0));
设置位置(点X,点Y);
rightControlList.SetSize(rightListContain.ActualWidth,rightListContain.ActualHeight);
point=rightListContain.TransformToAncestor(this.Transform)(新点(0,0));
右控制器设置位置(点X,点Y);
}
}
}
ControlList.xaml
<Window x:Class="WpfApplication1.ControlList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" WindowStyle="None" Opacity="0.5" AllowsTransparency="True">
<Grid Name="grid">
<ListBox Name="listBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
</ListBox>
</Grid>
</Window>
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
11111111111111111111111111
Control.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
namespace WpfApplication1
{
/// <summary>
/// </summary>
public partial class ControlList : Window
{
public ControlList()
{
InitializeComponent();
}
public void SetSize(double width, double height)
{
this.Width = width;
this.Height = height;
}
public void SetPosition(double left, double top)
{
this.Left = left;
this.Top = top;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用系统线程;
命名空间WpfApplication1
{
///
///
公共部分类控制器:窗口
{
公共控制者()
{
初始化组件();
}
公共空隙设置尺寸(双倍宽度,双倍高度)
{
这个。宽度=宽度;
这个。高度=高度;
}
公共无效设置位置(双左、双顶)
{
这个。左=左;
这个.Top=Top;
}
}
}
我认为您可以简单地将父窗口设置为子窗口的window.Owner
属性,而不是调用SetParent
函数。