首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 怎么得到窗口中控件的句柄

2013-01-11 
c# 如何得到窗口中控件的句柄[imghttp://][/img]我本人是搞C++的,C#的有些不太熟,下面是一个简单的对话框

c# 如何得到窗口中控件的句柄
[img=http://][/img]我本人是搞C++的,C#的有些不太熟,
下面是一个简单的对话框的XAML,里面有个canvas和button1变量,我想得到这两个的句柄(HWND)


<Window x:Class="Traffic.AP.FocusSet"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="调焦设置" FontSize="16" Height="500" Width="500" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" Closing="Window_Closing" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="370*" />
            <RowDefinition Height="91*" />
        </Grid.RowDefinitions>
        <Canvas Name="canvas" Grid.Row="0">
        </Canvas>
        <Button Content="抓拍" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="147,22,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>



类的代码如下,

namespace Traffic.AP
{
    /// <summary>
    /// Interaction logic for FocusSet.xaml
    /// </summary>
    public partial class FocusSet : Window
    {
        public FocusSet(delegateRecordFocus recordFocus,delegateGrabImg grabImg)
        {
            InitializeComponent();
            this.m_recordFocus = recordFocus;
            this.m_grabImg = grabImg;
        }

        public delegate void delegateRecordFocus(IntPtr hWndScreen, string tempDir, bool bStop);
        private delegateRecordFocus m_recordFocus;

        public delegate void delegateGrabImg();
        private delegateGrabImg m_grabImg;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            m_grabImg();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //var helper = new System.Windows.Interop.WindowInteropHelper(this);
            //IntPtr hWnd = helper.Handle;

            // 这里想获取canvs和button1的变量,不知道怎么获取,求指点


            IntPtr hWnd;
            hWnd = .....;// canvs 、button1怎么转
            m_recordFocus(hWnd, "D:\\1\\”" false);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }
    }
}



谢谢
[解决办法]
WPF的控件木有句柄,只有整个窗体才有句柄。
可以试试在WPF窗体上套一个windowsFormHost控件,再在其里面套一个winform的控件,使用winform下的控件的句柄。
[解决办法]
如果spy++获取不到句柄,lz就不用操心了。

热点排行