WPF教程

向WPF窗体发送windows messagehtml教程

时间:2013-6-20 1:21:10  作者:WPF之家  来源:http://www.wpf123.com  查看:171  评论:0
内容摘要:Buzz中采用 WM_COPYDATA的方式向已经启动的程序发送数据,以用以启动程序打开指定视频文件。  首先定义所需的win32函数和结构:    public class Win32Functions &n...

Buzz中采用 WM_COPYDATA的方式向已经启动的程序发送数据,以用以启动程序打开指定视频文件。

  首先定义所需的win32函数和结构:

    public class Win32Functions
    {
        [DllImport("user32.dll")]
        public static extern Int32 SendMessage(IntPtr hwnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct COPYDATASTRUCT
    {
        public UInt32 dwData;
        public UInt32 cbData;
        public string lpData;
    }
    public class Win32Consts
    {
        public const int WM_COPYDATA = 0x004A;
    }
 
    然后实现接收message的功能:
    ... ...
    //hook winproc, 须在Windows.Loaded事件响应中调用
    HwndSource hs = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
    hs.AddHook(new HwndSourceHook(TestHwndSourceHook));
    ... ...
 
    private IntPtr TestHwndSourceHook (
                                        IntPtr hwnd,
                                        int msg,
                                        IntPtr wParam,
                                        IntPtr lParam,
                                        ref bool handled
                                        )
        {
            switch (msg)
            {
                case Win32Consts.WM_COPYDATA:
                    COPYDATASTRUCT copydata = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
                    this.BindMedia.Stop();
                    this.BindMedia.Source = new Uri(copydata.lpData);
                    this.BindMedia.Play();
                    handled = true;
                    break;
                default:
                    break;
            }
            return IntPtr.Zero;
        }
    发送消息的代码:
    //construct COPYSTRUCT
            COPYDATASTRUCT copydata = new COPYDATASTRUCT();
            copydata.dwData = 0;
            copydata.lpData = @"E:\我的文档\My Videos\11.wmv";
            copydata.cbData = (UInt32)Encoding.Default.GetByteCount(copydata.lpData) + 1;
            IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(copydata));
            Marshal.StructureToPtr(copydata, buffer, false);
           
            //Send the message
            Win32Functions.SendMessage(new WindowInteropHelper(this).Handle,
                                        Win32Consts.WM_COPYDATA,
                                        IntPtr.Zero, buffer);

  附Xaml代码:

<Grid>
      <StackPanel x:Name="Container" Orientation="Vertical">
        <MediaElement x:Name="BindMedia" Height="200" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"/>
        <Button x:Name="SendMessageBtn" Width="95" Height="25" Content="SendMessage" Margin="0,10,0,0" Click="SendMessageBtn_Click" />
      </StackPanel>
    </Grid>

本文来自Huson的博客,原文地址:http://blog.sina.com.cn/s/blog_4b1558c4010006r4.html


标签:向WPF窗体发送windows message 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号