A detailed introduction to the code for implementing Windows Clipboard Monitor in c#

黄舟
Release: 2017-03-11 13:38:55
Original
1645 people have browsed it

Windows Clipboard

The Clipboard (ClipBoard) is an area in the memory. It is a very useful tool built into Windows. Through the small clipboard, A colorful bridge makes it possible to transfer and share information between various applications. However, the fly in the ointment is that the clipboard can only retain one copy of data, and whenever new data is passed in, the old ones will be overwritten.

Related Windows API

The most important one is SetClipboardViewer. Whenever the contents of the clipboard change, this function adds the window to be notified through the WM_DRAWCLIPBOARD message. window chain.
Since the handle to the next window in the clipboard viewer chain has not yet been returned, the application should not pass the WM_DRAWCLIPBOARD message it received during the SetClipboardViewer
call.
If you want to remove the window chain from the clipboard observer chain, the application must call the ChangeClipboard member function.


        #region Definitions
        //Constants for API Calls...
        private const int WM_DRAWCLIPBOARD = 0x308;
        private const int WM_CHANGECBCHAIN = 0x30D;

        //Handle for next clipboard viewer...
        private IntPtr mNextClipBoardViewerHWnd;

        //API declarations...
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern bool ChangeClipboardChain(IntPtr HWnd, IntPtr HWndNext);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        #endregion
Copy after login

wndproc function

The operating system sends a series of messages to the application, such as left button down and left button up, and the application will pass GetMessage The other methods finally submit the message to the window process (WndProc [full name in English: windows process]), which points to a pointer to an application-defined window process.

We need to rewrite this function to handle the clipboard content change event:

        #region Message Process
        //Override WndProc to get messages...
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_DRAWCLIPBOARD:
                    {
                        //The clipboard has changed...
                        //##########################################################################
                        // Process Clipboard Here :)........................
                        //##########################################################################
                        SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());

                        //显示剪贴板中的文本信息
                        if (Clipboard.ContainsText())
                        {
                            label1.Text = Clipboard.GetText();
                        }
                        //显示剪贴板中的图片信息
                        if (Clipboard.ContainsImage())
                        {
                            pictureBox1.Image = Clipboard.GetImage();
                            pictureBox1.Update();
                        }
                        break;
                    }
                case WM_CHANGECBCHAIN:
                    {
                        //Another clipboard viewer has removed itself...
                        if (m.WParam == (IntPtr)mNextClipBoardViewerHWnd)
                        {
                            mNextClipBoardViewerHWnd = m.LParam;
                        }
                        else
                        {
                            SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
                        }
                        break;
                    }
            }
            base.WndProc(ref m);
        }
        #endregion
Copy after login

Effect:



The above is the detailed content of A detailed introduction to the code for implementing Windows Clipboard Monitor in c#. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!