c# winform 应用编程代码总结 13

47、监视剪贴板

        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr SetClipboardViewer(IntPtr hwnd);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr ChangeClipboardChain(IntPtr hwnd,IntPtr hWndNext);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);

        const int WM_DRAWCLIPBOARD = 0x308;
        const int WM_CHANGECBCHAIN = 0x30D;
        IntPtr NextClipHwnd;
        //观察链中下一个窗口句柄   

        private void Form1_Load(object sender, System.EventArgs e)
        {
            NextClipHwnd=SetClipboardViewer(this.Handle);
            //获得观察链中下一个窗口句柄       
            //DateTime.Now
        }

        private void Form1_Closed(object sender, System.EventArgs e)
        {
            ChangeClipboardChain(this.Handle,NextClipHwnd);
            //从观察链中删除本观察窗口
            SendMessage(NextClipHwnd,WM_CHANGECBCHAIN,this.Handle,NextClipHwnd);
            //将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口       
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            switch(m.Msg)
            {
                case WM_DRAWCLIPBOARD:
                    SendMessage(NextClipHwnd,m.Msg,m.WParam,m.LParam);
                    //将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
                    IDataObject iData = Clipboard.GetDataObject();
                    if(iData.GetDataPresent(DataFormats.Text)|iData.GetDataPresent(DataFormats.OemText)) 
                    {
                        this.richTextBox1.Text=(String)iData.GetData(DataFormats.Text); 
                        
                    }
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }        
        }

48、模拟鼠标

        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags,int dx,int dy, int cButtons, int dwExtraInfo);
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        private void Form1_DoubleClick(object sender, System.EventArgs e)
        {
            MessageBox.Show("Double Click");
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            mouse_event(MOUSEEVENTF_MOVE,-10,-10,0,0);        
            mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);        
            mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
            mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);                
        }

49、模拟键盘操作

            SendKeys.Send("^a");
            SendKeys.Send("{DEL}");
            SendKeys.Send("+(ec)");
            SendKeys.Send("{ENTER}");
            SendKeys.Send("+ec");
            SendKeys.Send("{ENTER}");
            SendKeys.Send("{h 10}");
            SendKeys.Send("{ENTER}");

作者:syxChina
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。

posted @ 2010-12-14 15:28  BuildNewApp  阅读(238)  评论(0编辑  收藏  举报