剪贴板值返回null

时间:2014-08-12 07:10:57

标签: c# .net

我的应用程序使用剪贴板操作来执行任务。我正在尝试使用Microsoft Excel和记事本中的值。当我选择这些值并按下从我的应用程序注册的热键说 Ctrl + 2 ,当我尝试调试并检查Clipboard.GetText()时返回null当我按下热键时,它什么都不做。

private void ReceiveHotKey(string hotKeyID)
{
    if (options == null || options.Visible == false)
    {
        clipBoardText = null;
        prevStringObject = null;
        prevDataObject = null;
        hotKeyTrialNumber = 0;
        this.myHotKey.HotKeyPressed -= new HotKey.HotKeyPressedEventHandler(ReceiveHotKey);
        try
        {
            //get the previous value from the clipboard
            if (Clipboard.ContainsText())
            {
                prevStringObject = Clipboard.GetText();
            }
            else
            {
                prevDataObject = Clipboard.GetDataObject();
            }
            //copy the data and store it in the clipboard
            SendKeys.SendWait("^c");
            System.Threading.Thread.Sleep(500);

            if (Clipboard.ContainsText())
            {
                clipBoardText = Clipboard.GetText(); // returns null
                Clipboard.Clear();
                ShortcutKeyVisualize(hotKeyID);
                //restore the previous value to the clipboard                    
                RestoreClipboard();
            }
            else
            {
                Log.ErrorFormat("Error!!!", language.CLIPBOARD_BUSY);
                //MyMessageBox.Log("Error!!!", m_objLanguage.CLIPBOARD_BUSY);
                RetryClipboardOperation(hotKeyID);                    
            }
        }
        catch (Exception e)
        {
            if (string.IsNullOrEmpty(prevStringObject) && null != prevDataObject)
            {
                Log.Error(language.CLIPBOARD_PROCESS_USER);
                //MyMessageBox.Log(m_objLanguage.CLIPBOARD_PROCESS_USER);
                return;
            }
            else if (string.IsNullOrEmpty(clipBoardText))
            {
                MessageBox.Show(language.CLIPBOARD_PROCESS_AUTO);
                //MyMessageBox.Log(e.Message, m_objLanguage.CLIPBOARD_PROCESS_AUTO);
                Log.ErrorFormat(e.Message, language.CLIPBOARD_PROCESS_AUTO);
            }
            else
            {
                MessageBox.Show(language.CLIPBOARD_PROCESS_AUTO);
                //MyMessageBox.Log(e.Message, m_objLanguage.CLIPBOARD_PROCESS_AUTO);
                Log.ErrorFormat(e.Message, language.CLIPBOARD_PROCESS_AUTO);
            }

            if (hotKeyTrialNumber < 5)
            {
                hotKeyTrialNumber = hotKeyTrialNumber + 1;
                RetryClipboardOperation(hotKeyID);
            }
            else
            {
                MessageBox.Show(language.CLIPBOARD_BUSY);
                ApplicationRestart();
            }
            //MyMessageBox.Log(e.Message, e.StackTrace);
            Log.ErrorFormat(e.Message, e.StackTrace);
        }
        finally
        {
            this.myHotKey.HotKeyPressed += new HotKey.HotKeyPressedEventHandler(ReceiveHotKey);
        }
    }    
}

// Restores the clipboard data
// after the hot key press event
// is completed.
private void RestoreClipboard()
{
    //do not restore the old data in the
    //clipboard if a new data is already 
    //present in the clipboard
    if (!(Clipboard.ContainsAudio() || Clipboard.ContainsImage() || Clipboard.ContainsText()))
    {
         //check the data object and assign if not null
         if (prevDataObject != null)
         {
              Clipboard.SetDataObject(prevDataObject);
         }
         //now check the string object and assign it 
         //to the clipboard if not null
         if (prevStringObject != null)
         {
              Clipboard.SetText(prevStringObject);
         }
    }            
}

我错过了什么吗?

0 个答案:

没有答案