Пример #1
0
        protected override void WndProc(ref Message m)
        {
            switch ((ClipBoardHandleOptions)m.Msg)
            {
            case ClipBoardHandleOptions.WM_CREATE:
            {
                this.hWndNextWindow = ClipBoardConHelpers.SetClipboardViewer(this.Handle);
                break;
            }

            case ClipBoardHandleOptions.WM_CHANGECBCHAIN:
            {
                if (m.WParam == hWndNextWindow)
                {
                    hWndNextWindow = m.LParam;
                }
                else if (hWndNextWindow != IntPtr.Zero)
                {
                    ClipBoardConHelpers.SendMessage(hWndNextWindow, m.Msg, m.WParam, m.LParam);
                }
                break;
            }

            case ClipBoardHandleOptions.WM_DRAWCLIPBOARD:
            {
                this.OnClipBoardChangeIO.Invoke(this, new ClipBoardConEventArgs(ClipBoardOPType.Get, this[false]));
                ClipBoardConHelpers.SendMessage(hWndNextWindow, m.Msg, m.WParam, m.LParam);
                break;
            }

            case ClipBoardHandleOptions.WM_DESTROY:
            {
                ClipBoardConHelpers.ChangeClipboardChain(this.Handle, hWndNextWindow);
                break;
            }
            }
            base.WndProc(ref m);
        }
Пример #2
0
 protected void CreateThreads()
 {
     #region Set
     this.SetThread = new Thread(new ParameterizedThreadStart(x =>
     {
         int retryTimes = (int)Thread.GetData(Thread.GetNamedDataSlot("set_retryTimes"))
         , retryDelay   = (int)Thread.GetData(Thread.GetNamedDataSlot("set_retryDelay"));
         bool copy      = (bool)Thread.GetData(Thread.GetNamedDataSlot("set_copy"));
         Contract.Requires <ArgumentNullException>(x != null);
         Contract.Requires <ArgumentOutOfRangeException>(retryTimes > -1);
         Contract.Requires <ArgumentOutOfRangeException>(retryDelay > -1);
         DataObject dataObject = null;
         if (!(x is IComDataObject))
         {
             dataObject = new DataObject(x);
         }
         if (this.restrictFormats)
         {
             if (dataObject == null)
             {
                 dataObject = x as DataObject;
             }
             Contract.Requires <SecurityException>(IsFormatValid(dataObject));
         }
         int hr, retry = retryTimes;
         this.UnmanagedCode.Assert();
         try
         {
             do
             {
                 if (x is IComDataObject)
                 {
                     hr = ClipBoardConHelpers.OleSetClipboard((IComDataObject)x);
                 }
                 else
                 {
                     hr = ClipBoardConHelpers.OleSetClipboard(dataObject);
                 }
                 if (hr != 0)
                 {
                     Contract.Requires <ExternalException>(retry > 0);
                     retry--;
                     System.Threading.Thread.Sleep(retryDelay /*ms*/);
                 }
             }while (hr != 0);
             if (copy)
             {
                 retry = retryTimes;
                 do
                 {
                     hr = ClipBoardConHelpers.OleFlushClipboard();
                     if (hr != 0)
                     {
                         Contract.Requires <ExternalException>(retry > 0);
                         retry--;
                         System.Threading.Thread.Sleep(retryDelay);
                     }
                 }while (hr != 0);
             }
         }
         finally
         {
             CodeAccessPermission.RevertAssert();
         }
     }))
     {
         IsBackground = true
     };
     this.SetThread.SetApartmentState(ApartmentState.STA);
     #endregion
     #region Get
     this.GetThread = new Thread(new ParameterizedThreadStart(x =>
     {
         int retryTimes            = (int)x
         , retryDelay              = (int)Thread.GetData(Thread.GetNamedDataSlot("get_retryDelay"));
         IComDataObject dataObject = null;
         int hr, retry             = retryTimes;
         do
         {
             hr = ClipBoardConHelpers.OleGetClipboard(ref dataObject);
             if (hr != 0)
             {
                 Contract.Requires <ExternalException>(retry != 0);
                 retry--;
                 System.Threading.Thread.Sleep(retryDelay /*ms*/);
             }
         }while (hr != 0);
         if (dataObject != null)
         {
             if ((dataObject is System.Runtime.InteropServices.ComTypes.IDataObject) && !Marshal.IsComObject(dataObject))
             {
                 Thread.SetData(Thread.GetNamedDataSlot("get_Result"), (System.Runtime.InteropServices.ComTypes.IDataObject)dataObject);
             }
             else
             {
                 Thread.SetData(Thread.GetNamedDataSlot("get_Result"), new DataObject(dataObject));
             }
         }
         else
         {
             Thread.SetData(Thread.GetNamedDataSlot("get_Result"), null);
         }
     }))
     {
         IsBackground = true
     };
     this.GetThread.SetApartmentState(ApartmentState.STA);
     #endregion
 }