| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1541 人关注过本帖
标题:窗口消息传递的问题,API高手进。
只看楼主 加入收藏
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
结帖率:66.67%
收藏
 问题点数:0 回复次数:5 
窗口消息传递的问题,API高手进。
#region 窗口消息处理相关引用
        const Int32 WM_COPYDATA = 0x004A;
        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct COPYDATASTRUCT
        {
            /// <summary>
            /// 固定为 (IntPtr) 100?
            /// </summary>
            public IntPtr dwData;
            /// <summary>
            /// 消息长度+1,通过System.Text.Encoding.Default.GetByteCount函数取得字符串实际长度
            /// </summary>
            public int cbData;
            /// <summary>
            /// 字节数组表示的消息内容
            /// </summary>
            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
            public string lpData;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="hWnd">handle to destination window </param>
        /// <param name="Msg">message identifier </param>
        /// <param name="wParam">first message parameter </param>
        /// <param name="lParam">second message parameter </param>
        /// <returns> </returns>
        [System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(
        Int32 hWnd,
        Int32 Msg,
        Int32 wParam,
        ref COPYDATASTRUCT lParam
        );

        /// <summary>
        /// 该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回。
        /// </summary>
        /// <param name="hWnd">目标窗口句柄 </param>
        /// <param name="Msg">message identifier </param>
        /// <param name="wParam">第一个信息参数 </param>
        /// <param name="lParam">第一个信息参数 </param>
        /// <returns> </returns>
        [System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "PostMessage")]
        private static extern int PostMessage(
            Int32 hWnd,
            Int32 Msg,
            Int32 wParam,
            ref COPYDATASTRUCT lParam
            );

        //[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "GetWindowText")]
        //private static extern int GetWindowText(
        //    Int32 hWnd, // {a handle to a window}
        //    [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
        //    string lpString, // {a pointer to a buffer to receive the string}
        //  Int32 nMaxCount // {the maximum number of characters to copy}
        //    ); // {returns the length of the copied string}

        //[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "FindWindow")]
        //private static extern int FindWindow(string lpClassName, string
        //lpWindowName);
        #endregion

        //窗口消息处理
        protected override void DefWndProc(ref Message m)//没接收到相应的窗口信息
        {
            switch (m.Msg)
            {
                case WM_COPYDATA:

                    COPYDATASTRUCT cdsT = new COPYDATASTRUCT();
                    Type mytype = cdsT.GetType();
                    cdsT = (COPYDATASTRUCT)m.GetLParam(mytype);
                    //调试开始
                    tbContent.AppendText(cdsT.lpData); //在外部已经定义
                    MessageBox.Show("调试开始");
                    //调试结束
                    //cdsT.lpData
                    if (cdsT.lpData == ConstantVars.WindowMessageConst.sOptionChanged) LoadOption();
                    if (cdsT.lpData.StartsWith(ConstantVars.WindowMessageConst.sOpenFile))
                    {
                        Int32 I = cdsT.lpData.Length - ConstantVars.WindowMessageConst.sOpenFile.Length;
                        OpenFile(sflStoryFileList.DecreaseStoryFilesFrequecyBut(new StringBuilder(cdsT.lpData, ConstantVars.WindowMessageConst.sOpenFile.Length, I, I).ToString())); //在外部已经定义
                    }
                    else
                    {
                        if (cdsT.lpData == ConstantVars.WindowMessageConst.sOptionChanged)
                        {
                            LoadOption(); //在外部已经定义
                        }
                    }
                    MessageBox.Show(cdsT.lpData);
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        /// <summary>
        /// //找出以前的实例并发送信息
        /// </summary>
        /// <param name="sMessage"> </param>
        private void SendMessageToBefore(string sMessage)//这个函数有调用,但没有结果
        {
            COPYDATASTRUCT csT = new COPYDATASTRUCT();
            csT.dwData = (IntPtr)100;
            csT.lpData = sMessage;
            csT.cbData = System.Text.Encoding.Default.GetByteCount(sMessage) + 1;
            System.Diagnostics.Process prcCurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
            string sFileName = prcCurrentProcess.MainModule.FileName;
            Int32 iID = prcCurrentProcess.Id;
            System.Diagnostics.Process[] prcs = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process prcT in prcs)
            {
                try
                {
                    Int32 I;
                    if (prcT.MainModule.FileName == sFileName)
                        if (prcT.Id != iID)
                            I = PostMessage(prcT.MainWindowHandle.ToInt32(), WM_COPYDATA, 0, ref csT);
                }
                catch
                {
                }
            }
        }
搜索更多相关主题的帖子: API 窗口 
2008-11-07 15:40
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
收藏
得分:0 
有没有高手帮帮忙?
2008-11-08 10:27
heihei1314
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-9-16
收藏
得分:0 
我不是高手,来占位学习~~~~
也想学学API~~
2008-11-08 10:29
ghl2312
Rank: 4
等 级:业余侠客
威 望:2
帖 子:208
专家分:226
注 册:2008-7-10
收藏
得分:0 
DefWndProc()在什么地方调的呢?

[[it] 本帖最后由 ghl2312 于 2008-11-8 11:03 编辑 [/it]]

向灰太狼学习
2008-11-08 11:00
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
收藏
得分:0 
DefWndProc()是重写窗口本身的消息处理函数。
2008-11-08 11:22
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
收藏
得分:0 

API使用一直不纯熟.......

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2008-11-21 11:36
快速回复:窗口消息传递的问题,API高手进。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018598 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved