5
5
using System . Security . Cryptography ;
6
6
using System . Text ;
7
7
using System . Text . Json ;
8
- using System . Threading ;
9
8
using System . Web ;
10
9
using System . Windows ;
11
10
using System . Windows . Input ;
@@ -23,114 +22,105 @@ public partial class MainWindow : Window
23
22
#pragma warning disable CA2211 // Non-constant fields should not be visible
24
23
public static bool changeStatus = false ;
25
24
#pragma warning restore CA2211 // Non-constant fields should not be visible
26
- bool switch1Check = true ;
27
- bool switch2Check = false ;
28
- bool switch3Check = false ;
25
+ private bool switch1Check = true ;
26
+ private bool switch2Check = false ;
27
+ private bool switch3Check = false ;
28
+
29
+ public SharpClipboard clipboard ;
29
30
30
- public SharpClipboard clipboard = new SharpClipboard ( ) ;
31
31
public MainWindow ( )
32
32
{
33
33
InitializeComponent ( ) ;
34
34
35
-
36
- // Attach your code to the ClipboardChanged event to listen to cuts/copies.
37
- clipboard . ClipboardChanged += ClipboardChanged ;
38
- //disable issueing ClipboardChanged event when start
39
- clipboard . ObserveLastEntry = false ;
40
- clipboard . ObservableFormats . Files = false ;
41
- clipboard . ObservableFormats . Images = false ;
35
+ InitializeClipboardMonitor ( ) ;
42
36
}
43
37
44
- public void InitializeClipboard ( )
38
+
39
+ //Initializes a new instance of SharpClipboard
40
+ public void InitializeClipboardMonitor ( )
45
41
{
46
42
clipboard = new SharpClipboard ( ) ;
43
+ //Attach your code to the ClipboardChanged event to listen to cuts/copies
47
44
clipboard . ClipboardChanged += ClipboardChanged ;
48
45
//disable issueing ClipboardChanged event when start
49
46
clipboard . ObserveLastEntry = false ;
47
+ //disable monitoring files
50
48
clipboard . ObservableFormats . Files = false ;
49
+ //disable monitoring images
51
50
clipboard . ObservableFormats . Images = false ;
52
51
}
53
52
54
53
private void ClipboardChanged ( Object sender , SharpClipboard . ClipboardChangedEventArgs e )
55
54
{
56
- //if (e.SourceApplication.Title.Contains("CopyPlusPlus"))
57
- //{
58
- // return;
59
- //}
60
- if ( true )
55
+ // Is the content copied of text type?
56
+ if ( e . ContentType == SharpClipboard . ContentTypes . Text )
61
57
{
62
- // Is the content copied of text type?
63
- if ( e . ContentType == SharpClipboard . ContentTypes . Text )
64
- {
65
- // Get the cut/copied text.
66
- string text = e . Content . ToString ( ) ;
58
+ // Get the cut/copied text.
59
+ string text = e . Content . ToString ( ) ;
67
60
68
- if ( switch1Check == true || switch2Check == true )
61
+ if ( switch1Check == true || switch2Check == true )
62
+ {
63
+ for ( int counter = 0 ; counter < text . Length - 1 ; counter ++ )
69
64
{
70
- for ( int counter = 0 ; counter < text . Length - 1 ; counter ++ )
65
+ if ( switch1Check == true )
71
66
{
72
- if ( switch1Check == true )
67
+ if ( text [ counter + 1 ] . ToString ( ) == " \r " )
73
68
{
74
- if ( text [ counter + 1 ] . ToString ( ) == "\r " )
69
+ if ( text [ counter ] . ToString ( ) == ". " )
75
70
{
76
- if ( text [ counter ] . ToString ( ) == "." )
77
- {
78
- continue ;
79
- }
80
- if ( text [ counter ] . ToString ( ) == "。" )
81
- {
82
- continue ;
83
- }
84
- text = text . Remove ( counter + 1 , 2 ) ;
71
+ continue ;
85
72
}
86
- }
87
-
88
- if ( switch2Check == true )
89
- {
90
- if ( text [ counter ] . ToString ( ) == " " )
73
+ if ( text [ counter ] . ToString ( ) == "。" )
91
74
{
92
- text = text . Remove ( counter , 1 ) ;
75
+ continue ;
93
76
}
77
+ text = text . Remove ( counter + 1 , 2 ) ;
94
78
}
95
79
}
96
- }
97
80
98
- if ( switch3Check == true )
99
- {
100
- if ( changeStatus == false )
81
+ if ( switch2Check == true )
101
82
{
102
- string appId = Properties . Settings . Default . AppID ;
103
- string secretKey = Properties . Settings . Default . SecretKey ;
104
- if ( appId == "none" || secretKey == "none" )
105
- {
106
- MessageBox . Show ( "请先设置翻译接口" ) ;
107
-
108
- KeyInput keyinput = new KeyInput ( ) ;
109
- keyinput . Show ( ) ;
110
- changeStatus = true ;
111
- }
112
- else
83
+ if ( text [ counter ] . ToString ( ) == " " )
113
84
{
114
- text = BaiduTrans ( appId , secretKey , text ) ;
85
+ text = text . Remove ( counter , 1 ) ;
115
86
}
116
87
}
117
88
}
118
-
119
- clipboard . StopMonitoring ( ) ;
120
- Clipboard . SetText ( text ) ;
121
- //Clipboard.Flush();
122
- InitializeClipboard ( ) ;
123
-
124
-
125
89
}
126
90
127
- // If the cut/copied content is complex, use 'Other'.
128
- else if ( e . ContentType == SharpClipboard . ContentTypes . Other )
91
+ if ( switch3Check == true )
129
92
{
130
- //do nothing
93
+ if ( changeStatus == false )
94
+ {
95
+ string appId = Properties . Settings . Default . AppID ;
96
+ string secretKey = Properties . Settings . Default . SecretKey ;
97
+ if ( appId == "none" || secretKey == "none" )
98
+ {
99
+ MessageBox . Show ( "请先设置翻译接口" ) ;
131
100
132
- // Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
101
+ KeyInput keyinput = new KeyInput ( ) ;
102
+ keyinput . Show ( ) ;
103
+ changeStatus = true ;
104
+ }
105
+ else
106
+ {
107
+ text = BaiduTrans ( appId , secretKey , text ) ;
108
+ }
109
+ }
133
110
}
111
+
112
+ clipboard . StopMonitoring ( ) ;
113
+ Clipboard . SetText ( text ) ;
114
+ Clipboard . Flush ( ) ;
115
+ InitializeClipboardMonitor ( ) ;
116
+ }
117
+
118
+ // If the cut/copied content is complex, use 'Other'.
119
+ else if ( e . ContentType == SharpClipboard . ContentTypes . Other )
120
+ {
121
+ //do nothing
122
+
123
+ // Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
134
124
}
135
125
136
126
}
@@ -229,8 +219,6 @@ private void ShowInputWindowText(object sender, MouseButtonEventArgs e)
229
219
changeStatus = true ;
230
220
}
231
221
232
-
233
-
234
222
private void SwitchUncheck ( object sender , RoutedEventArgs e )
235
223
{
236
224
HorizontalToggleSwitch switchButton = sender as HorizontalToggleSwitch ;
0 commit comments