| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1541 人关注过本帖
标题:窗口消息传递的问题,API高手进。
取消只看楼主 加入收藏
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
窗口消息传递的问题,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
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
收藏
得分:0 
DefWndProc()是重写窗口本身的消息处理函数。
2008-11-08 11:22
快速回复:窗口消息传递的问题,API高手进。
数据加载中...
 
   



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

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