Esempio n. 1
0
        private async void ClipboardNotification_ClipboardUpdate(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                // Delay if clipboard still open by other app
                await Task.Delay(10);

                bool done = false;
                for (int i = 0; i < 5 && !done; i++)
                {
                    try
                    {
                        var clipboradData = Clipboard.GetDataObject();

                        StringObject newObject = null;
                        if (clipboradData?.GetDataPresent(DataFormats.Text) ?? false)
                        {
                            newObject = new StringObject()
                            {
                                Value = clipboradData.GetData(DataFormats.Text).ToString()
                            };
                        }

                        if (newObject != null)
                        {
                            if (newObject.Value.Trim().Length == 0)
                            {
                                return;
                            }

                            // Clean search to get the real clipboard entrys and not just a view or results which we cant modify
                            // window is closed after this code anyway and search is discarded
                            ViewModel.CurrentSearch = "";

                            ViewModel.ClipboardEntrys.Remove(ViewModel.ClipboardEntrys.FirstOrDefault(x => x.Value == newObject.Value));
                            ViewModel.ClipboardEntrys.Insert(0, newObject);

                            if (ViewModel.ClipboardEntrys.Count > ViewModel.MaxHistoryCount)
                            {
                                ViewModel.ClipboardEntrys.Remove(ViewModel.ClipboardEntrys.Last());
                            }
                        }

                        done = true;
                    }
                    catch (COMException)
                    {
                        // Clipboard already opened
                        // Delay if clipboard still open by other app
                        await Task.Delay(10);
                    }
                }

                // Put text into clipboard from text popup
                if (InputTextPopup.IsOpen && done)
                {
                    PutInClipboard(true, false, ViewModel.ClipboardEntrys.First().Value);
                }
            }
        }
Esempio n. 2
0
        public static string GetText()
        {
            IDataObject dataObj = WPFClipboard.GetDataObject();

            if (dataObj == null || !dataObj.GetDataPresent(typeof(string)))
            {
                return(null);
            }

            return((string)dataObj.GetData(DataFormats.UnicodeText)
                   ?? (string)dataObj.GetData(DataFormats.Text));
        }