由于wp中把相关api给阉割掉了,所以本文以wpf为例,说明控件与窗口的关系,便于大家的理解。
1. 在xaml中定义两个控件
第二步后台获取他们对应的窗口句柄Code:<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1"> <Grid Name="grid1"> <Button Content="hello" Name="button1"/> </Grid> </Window>
3. 调试可知,这几个控件对应的窗口句柄是相同的,也就是说Code: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.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded_1(object sender, RoutedEventArgs e) { HwndSource gridwin = (HwndSource)HwndSource.FromVisual(grid1); IntPtr handle1 = gridwin.Handle; HwndSource buttonwin = (HwndSource)HwndSource.FromVisual(button1); IntPtr handle2 = buttonwin.Handle; WindowInteropHelper windowHwnd = new WindowInteropHelper(this); IntPtr handle3 = windowHwnd.Handle; } } }
wpf中控件和窗口没有一一对应关系,而是各个控件最后绘制到一个窗口中去。。。
Regards
Vincent
http://weibo.com/xueyw

Reply With Quote

