使用wpf制作了一个虚拟键盘,应用到最新的项目中,美工配合修改了一下样式,觉得还不错。上图:

简述过程:

建立wpf工程,新建文件夹Keyboard,在该文件夹下建立自定义控件(WPF),命名TouchScreenKeyboard,同时自动生成自定义控件样式Themes\Generic.Xaml.

TouchScreenKeyboard.cs代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TouchKeyboard.Keyboard
{
public class TouchScreenKeyboard : Window
{
\#region Property & Variable & Constructor
private static double _WidthTouchKeyboard = 605;
public static double WidthTouchKeyboard
{
get { return _WidthTouchKeyboard; }
set { _WidthTouchKeyboard = value; }
}
private static bool _ShiftFlag;
protected static bool ShiftFlag
{
get { return _ShiftFlag; }
set { _ShiftFlag = value; }
}
private static bool _CapsLockFlag;
protected static bool CapsLockFlag
{
get { return TouchScreenKeyboard._CapsLockFlag; }
set { TouchScreenKeyboard._CapsLockFlag = value; }
}
private static Window _InstanceObject;
private static Brush _PreviousTextBoxBackgroundBrush = null;
private static Brush _PreviousTextBoxBorderBrush = null;
private static Thickness _PreviousTextBoxBorderThickness;
private static Control _CurrentControl;
public static string TouchScreenText
{
get
{
if (_CurrentControl is TextBox)
{
return ((TextBox)_CurrentControl).Text;
}
else if (_CurrentControl is PasswordBox)
{
return ((PasswordBox)_CurrentControl).Password;
}
else return "";
}
set
{
if (_CurrentControl is TextBox)
{
((TextBox)_CurrentControl).Text = value;
}
else if (_CurrentControl is PasswordBox)
{
((PasswordBox)_CurrentControl).Password = value;
}
}
}
public static RoutedUICommand CmdTlide = new RoutedUICommand();
public static RoutedUICommand Cmd1 = new RoutedUICommand();
public static RoutedUICommand Cmd2 = new RoutedUICommand();
public static RoutedUICommand Cmd3 = new RoutedUICommand();
public static RoutedUICommand Cmd4 = new RoutedUICommand();
public static RoutedUICommand Cmd5 = new RoutedUICommand();
public static RoutedUICommand Cmd6 = new RoutedUICommand();
public static RoutedUICommand Cmd7 = new RoutedUICommand();
public static RoutedUICommand Cmd8 = new RoutedUICommand();
public static RoutedUICommand Cmd9 = new RoutedUICommand();
public static RoutedUICommand Cmd0 = new RoutedUICommand();
public static RoutedUICommand CmdMinus = new RoutedUICommand();
public static RoutedUICommand CmdPlus = new RoutedUICommand();
public static RoutedUICommand CmdBackspace = new RoutedUICommand();
public static RoutedUICommand CmdTab = new RoutedUICommand();
public static RoutedUICommand CmdQ = new RoutedUICommand();
public static RoutedUICommand Cmdw = new RoutedUICommand();
public static RoutedUICommand CmdE = new RoutedUICommand();
public static RoutedUICommand CmdR = new RoutedUICommand();
public static RoutedUICommand CmdT = new RoutedUICommand();
public static RoutedUICommand CmdY = new RoutedUICommand();
public static RoutedUICommand CmdU = new RoutedUICommand();
public static RoutedUICommand CmdI = new RoutedUICommand();
public static RoutedUICommand CmdO = new RoutedUICommand();
public static RoutedUICommand CmdP = new RoutedUICommand();
public static RoutedUICommand CmdOpenCrulyBrace = new RoutedUICommand();
public static RoutedUICommand CmdEndCrultBrace = new RoutedUICommand();
public static RoutedUICommand CmdOR = new RoutedUICommand();
public static RoutedUICommand CmdCapsLock = new RoutedUICommand();
public static RoutedUICommand CmdA = new RoutedUICommand();
public static RoutedUICommand CmdS = new RoutedUICommand();
public static RoutedUICommand CmdD = new RoutedUICommand();
public static RoutedUICommand CmdF = new RoutedUICommand();
public static RoutedUICommand CmdG = new RoutedUICommand();
public static RoutedUICommand CmdH = new RoutedUICommand();
public static RoutedUICommand CmdJ = new RoutedUICommand();
public static RoutedUICommand CmdK = new RoutedUICommand();
public static RoutedUICommand CmdL = new RoutedUICommand();
public static RoutedUICommand CmdColon = new RoutedUICommand();
public static RoutedUICommand CmdDoubleInvertedComma = new RoutedUICommand();
public static RoutedUICommand CmdEnter = new RoutedUICommand();
public static RoutedUICommand CmdShift = new RoutedUICommand();
public static RoutedUICommand CmdZ = new RoutedUICommand();
public static RoutedUICommand CmdX = new RoutedUICommand();
public static RoutedUICommand CmdC = new RoutedUICommand();
public static RoutedUICommand CmdV = new RoutedUICommand();
public static RoutedUICommand CmdB = new RoutedUICommand();
public static RoutedUICommand CmdN = new RoutedUICommand();
public static RoutedUICommand CmdM = new RoutedUICommand();
public static RoutedUICommand CmdGreaterThan = new RoutedUICommand();
public static RoutedUICommand CmdLessThan = new RoutedUICommand();
public static RoutedUICommand CmdQuestion = new RoutedUICommand();
public static RoutedUICommand CmdSpaceBar = new RoutedUICommand();
public static RoutedUICommand CmdClear = new RoutedUICommand();
public static RoutedUICommand CmdClose = new RoutedUICommand();
public TouchScreenKeyboard()
{
this.Width = WidthTouchKeyboard;
this.DefaultStyleKey = typeof(TouchScreenKeyboard);
}
static TouchScreenKeyboard()
{
// DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchScreenKeyboard), new FrameworkPropertyMetadata(typeof(TouchScreenKeyboard)));
SetCommandBinding();
}
#endregion
#region CommandRelatedCode
private static void SetCommandBinding()
{
CommandBinding CbTlide = new CommandBinding(CmdTlide, RunCommand);
CommandBinding Cb1 = new CommandBinding(Cmd1, RunCommand);
CommandBinding Cb2 = new CommandBinding(Cmd2, RunCommand);
CommandBinding Cb3 = new CommandBinding(Cmd3, RunCommand);
CommandBinding Cb4 = new CommandBinding(Cmd4, RunCommand);
CommandBinding Cb5 = new CommandBinding(Cmd5, RunCommand);
CommandBinding Cb6 = new CommandBinding(Cmd6, RunCommand);
CommandBinding Cb7 = new CommandBinding(Cmd7, RunCommand);
CommandBinding Cb8 = new CommandBinding(Cmd8, RunCommand);
CommandBinding Cb9 = new CommandBinding(Cmd9, RunCommand);
CommandBinding Cb0 = new CommandBinding(Cmd0, RunCommand);
CommandBinding CbMinus = new CommandBinding(CmdMinus, RunCommand);
CommandBinding CbPlus = new CommandBinding(CmdPlus, RunCommand);
CommandBinding CbBackspace = new CommandBinding(CmdBackspace, RunCommand);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbTlide);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb1);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb2);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb3);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb4);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb5);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb6);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb7);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb8);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb9);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cb0);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbMinus);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbPlus);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbBackspace);
CommandBinding CbTab = new CommandBinding(CmdTab, RunCommand);
CommandBinding CbQ = new CommandBinding(CmdQ, RunCommand);
CommandBinding Cbw = new CommandBinding(Cmdw, RunCommand);
CommandBinding CbE = new CommandBinding(CmdE, RunCommand);
CommandBinding CbR = new CommandBinding(CmdR, RunCommand);
CommandBinding CbT = new CommandBinding(CmdT, RunCommand);
CommandBinding CbY = new CommandBinding(CmdY, RunCommand);
CommandBinding CbU = new CommandBinding(CmdU, RunCommand);
CommandBinding CbI = new CommandBinding(CmdI, RunCommand);
CommandBinding Cbo = new CommandBinding(CmdO, RunCommand);
CommandBinding CbP = new CommandBinding(CmdP, RunCommand);
CommandBinding CbOpenCrulyBrace = new CommandBinding(CmdOpenCrulyBrace, RunCommand);
CommandBinding CbEndCrultBrace = new CommandBinding(CmdEndCrultBrace, RunCommand);
CommandBinding CbOR = new CommandBinding(CmdOR, RunCommand);
CommandBinding CbCapsLock = new CommandBinding(CmdCapsLock, RunCommand);
CommandBinding CbA = new CommandBinding(CmdA, RunCommand);
CommandBinding CbS = new CommandBinding(CmdS, RunCommand);
CommandBinding CbD = new CommandBinding(CmdD, RunCommand);
CommandBinding CbF = new CommandBinding(CmdF, RunCommand);
CommandBinding CbG = new CommandBinding(CmdG, RunCommand);
CommandBinding CbH = new CommandBinding(CmdH, RunCommand);
CommandBinding CbJ = new CommandBinding(CmdJ, RunCommand);
CommandBinding CbK = new CommandBinding(CmdK, RunCommand);
CommandBinding CbL = new CommandBinding(CmdL, RunCommand);
CommandBinding CbColon = new CommandBinding(CmdColon, RunCommand);
CommandBinding CbDoubleInvertedComma = new CommandBinding(CmdDoubleInvertedComma, RunCommand);
CommandBinding CbEnter = new CommandBinding(CmdEnter, RunCommand);
CommandBinding CbShift = new CommandBinding(CmdShift, RunCommand);
CommandBinding CbZ = new CommandBinding(CmdZ, RunCommand);
CommandBinding CbX = new CommandBinding(CmdX, RunCommand);
CommandBinding CbC = new CommandBinding(CmdC, RunCommand);
CommandBinding CbV = new CommandBinding(CmdV, RunCommand);
CommandBinding CbB = new CommandBinding(CmdB, RunCommand);
CommandBinding CbN = new CommandBinding(CmdN, RunCommand);
CommandBinding CbM = new CommandBinding(CmdM, RunCommand);
CommandBinding CbGreaterThan = new CommandBinding(CmdGreaterThan, RunCommand);
CommandBinding CbLessThan = new CommandBinding(CmdLessThan, RunCommand);
CommandBinding CbQuestion = new CommandBinding(CmdQuestion, RunCommand);
CommandBinding CbSpaceBar = new CommandBinding(CmdSpaceBar, RunCommand);
CommandBinding CbClear = new CommandBinding(CmdClear, RunCommand);
CommandBinding CbClose = new CommandBinding(CmdClose, RunCommand);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbTab);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbQ);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cbw);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbE);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbR);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbT);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbY);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbU);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbI);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), Cbo);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbP);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbOpenCrulyBrace);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbEndCrultBrace);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbOR);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbCapsLock);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbA);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbS);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbD);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbF);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbG);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbH);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbJ);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbK);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbL);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbColon);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbDoubleInvertedComma);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbEnter);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbShift);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbZ);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbX);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbC);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbV);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbB);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbN);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbM);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbGreaterThan);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbLessThan);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbQuestion);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbSpaceBar);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbClear);
CommandManager.RegisterClassCommandBinding(typeof(TouchScreenKeyboard), CbClose);
}
static void RunCommand(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == CmdTlide) //First Row
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "`";
}
else
{
TouchScreenKeyboard.TouchScreenText += "~";
ShiftFlag = false;
}
}
else if (e.Command == Cmd1)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "1";
}
else
{
TouchScreenKeyboard.TouchScreenText += "!";
ShiftFlag = false;
}
}
else if (e.Command == Cmd2)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "2";
}
else
{
TouchScreenKeyboard.TouchScreenText += "@";
ShiftFlag = false;
}
}
else if (e.Command == Cmd3)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "3";
}
else
{
TouchScreenKeyboard.TouchScreenText += "#";
ShiftFlag = false;
}
}
else if (e.Command == Cmd4)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "4";
}
else
{
TouchScreenKeyboard.TouchScreenText += "$";
ShiftFlag = false;
}
}
else if (e.Command == Cmd5)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "5";
}
else
{
TouchScreenKeyboard.TouchScreenText += "%";
ShiftFlag = false;
}
}
else if (e.Command == Cmd6)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "6";
}
else
{
TouchScreenKeyboard.TouchScreenText += "^";
ShiftFlag = false;
}
}
else if (e.Command == Cmd7)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "7";
}
else
{
TouchScreenKeyboard.TouchScreenText += "&";
ShiftFlag = false;
}
}
else if (e.Command == Cmd8)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "8";
}
else
{
TouchScreenKeyboard.TouchScreenText += "*";
ShiftFlag = false;
}
}
else if (e.Command == Cmd9)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "9";
}
else
{
TouchScreenKeyboard.TouchScreenText += "(";
ShiftFlag = false;
}
}
else if (e.Command == Cmd0)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "0";
}
else
{
TouchScreenKeyboard.TouchScreenText += ")";
ShiftFlag = false;
}
}
else if (e.Command == CmdMinus)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "-";
}
else
{
TouchScreenKeyboard.TouchScreenText += "_";
ShiftFlag = false;
}
}
else if (e.Command == CmdPlus)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "=";
}
else
{
TouchScreenKeyboard.TouchScreenText += "+";
ShiftFlag = false;
}
}
else if (e.Command == CmdBackspace)
{
if (!string.IsNullOrEmpty(TouchScreenKeyboard.TouchScreenText))
{
TouchScreenKeyboard.TouchScreenText = TouchScreenKeyboard.TouchScreenText.Substring(0, TouchScreenKeyboard.TouchScreenText.Length - 1);
}
}
else if (e.Command == CmdTab) //Second Row
{
TouchScreenKeyboard.TouchScreenText += " ";
}
else if (e.Command == CmdQ)
{
AddKeyBoardINput('Q');
}
else if (e.Command == Cmdw)
{
AddKeyBoardINput('w');
}
else if (e.Command == CmdE)
{
AddKeyBoardINput('E');
}
else if (e.Command == CmdR)
{
AddKeyBoardINput('R');
}
else if (e.Command == CmdT)
{
AddKeyBoardINput('T');
}
else if (e.Command == CmdY)
{
AddKeyBoardINput('Y');
}
else if (e.Command == CmdU)
{
AddKeyBoardINput('U');
}
else if (e.Command == CmdI)
{
AddKeyBoardINput('I');
}
else if (e.Command == CmdO)
{
AddKeyBoardINput('O');
}
else if (e.Command == CmdP)
{
AddKeyBoardINput('P');
}
else if (e.Command == CmdOpenCrulyBrace)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "[";
}
else
{
TouchScreenKeyboard.TouchScreenText += "{";
ShiftFlag = false;
}
}
else if (e.Command == CmdEndCrultBrace)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "]";
}
else
{
TouchScreenKeyboard.TouchScreenText += "}";
ShiftFlag = false;
}
}
else if (e.Command == CmdOR)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += @"\";
}
else
{
TouchScreenKeyboard.TouchScreenText += "|";
ShiftFlag = false;
}
}
else if (e.Command == CmdCapsLock) ///Third ROw
{
if (!CapsLockFlag)
{
CapsLockFlag = true;
}
else
{
CapsLockFlag = false;
}
}
else if (e.Command == CmdA)
{
AddKeyBoardINput('A');
}
else if (e.Command == CmdS)
{
AddKeyBoardINput('S');
}
else if (e.Command == CmdD)
{
AddKeyBoardINput('D');
}
else if (e.Command == CmdF)
{
AddKeyBoardINput('F');
}
else if (e.Command == CmdG)
{
AddKeyBoardINput('G');
}
else if (e.Command == CmdH)
{
AddKeyBoardINput('H');
}
else if (e.Command == CmdJ)
{
AddKeyBoardINput('J');
}
else if (e.Command == CmdK)
{
AddKeyBoardINput('K');
}
else if (e.Command == CmdL)
{
AddKeyBoardINput('L');
}
else if (e.Command == CmdColon)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += ";";
}
else
{
TouchScreenKeyboard.TouchScreenText += ":";
ShiftFlag = false;
}
}
else if (e.Command == CmdDoubleInvertedComma)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "'";
}
else
{
TouchScreenKeyboard.TouchScreenText += Char.ConvertFromUtf32(34);
ShiftFlag = false;
}
}
else if (e.Command == CmdEnter)
{
if (_InstanceObject != null)
{
_InstanceObject.Close();
_InstanceObject = null;
}
_CurrentControl.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
else if (e.Command == CmdShift) //Fourth Row
{
ShiftFlag = true; ;
}
else if (e.Command == CmdZ)
{
AddKeyBoardINput('Z');
}
else if (e.Command == CmdX)
{
AddKeyBoardINput('X');
}
else if (e.Command == CmdC)
{
AddKeyBoardINput('C');
}
else if (e.Command == CmdV)
{
AddKeyBoardINput('V');
}
else if (e.Command == CmdB)
{
AddKeyBoardINput('B');
}
else if (e.Command == CmdN)
{
AddKeyBoardINput('N');
}
else if (e.Command == CmdM)
{
AddKeyBoardINput('M');
}
else if (e.Command == CmdLessThan)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += ",";
}
else
{
TouchScreenKeyboard.TouchScreenText += "<";
ShiftFlag = false;
}
}
else if (e.Command == CmdGreaterThan)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += ".";
}
else
{
TouchScreenKeyboard.TouchScreenText += ">";
ShiftFlag = false;
}
}
else if (e.Command == CmdQuestion)
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += "/";
}
else
{
TouchScreenKeyboard.TouchScreenText += "?";
ShiftFlag = false;
}
}
else if (e.Command == CmdSpaceBar)//Last row
{
TouchScreenKeyboard.TouchScreenText += " ";
}
else if (e.Command == CmdClear)//Last row
{
TouchScreenKeyboard.TouchScreenText = "";
}
else if (e.Command == CmdClose)
{
if (_InstanceObject != null)
{
_InstanceObject.Close();
_InstanceObject = null;
}
}
}
#endregion
#region Main Functionality
private static void AddKeyBoardINput(char input)
{
if (CapsLockFlag)
{
if (ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += char.ToLower(input).ToString();
ShiftFlag = false;
}
else
{
TouchScreenKeyboard.TouchScreenText += char.ToUpper(input).ToString();
}
}
else
{
if (!ShiftFlag)
{
TouchScreenKeyboard.TouchScreenText += char.ToLower(input).ToString();
}
else
{
TouchScreenKeyboard.TouchScreenText += char.ToUpper(input).ToString();
ShiftFlag = false;
}
}
}
private static void syncchild()
{
if (_CurrentControl != null && _InstanceObject != null)
{
Point virtualpoint = new Point(0, _CurrentControl.ActualHeight + 3);
Point Actualpoint = _CurrentControl.PointToScreen(virtualpoint);
if (WidthTouchKeyboard + Actualpoint.X > SystemParameters.VirtualScreenWidth)
{
double difference = WidthTouchKeyboard + Actualpoint.X - SystemParameters.VirtualScreenWidth;
_InstanceObject.Left = Actualpoint.X - difference;
}
else if (!(Actualpoint.X > 1))
{
_InstanceObject.Left = 1;
}
else
_InstanceObject.Left = Actualpoint.X;
_InstanceObject.Top = Actualpoint.Y;
_InstanceObject.Show();
}
}
public static bool GetTouchScreenKeyboard(DependencyObject obj)
{
return (bool)obj.GetValue(TouchScreenKeyboardProperty);
}
public static void SetTouchScreenKeyboard(DependencyObject obj, bool value)
{
obj.SetValue(TouchScreenKeyboardProperty, value);
}
public static readonly DependencyProperty TouchScreenKeyboardProperty =
DependencyProperty.RegisterAttached("TouchScreenKeyboard", typeof(bool), typeof(TouchScreenKeyboard), new UIPropertyMetadata(default(bool), TouchScreenKeyboardPropertyChanged));
static void TouchScreenKeyboardPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (bool.Parse(e.NewValue.ToString()))
{
FrameworkElement host = sender as FrameworkElement;
if (host != null)
{
host.GotFocus += new RoutedEventHandler(OnGotFocus);
host.LostFocus += new RoutedEventHandler(OnLostFocus);
}
}
else
{
FrameworkElement host = sender as FrameworkElement;
if (host != null)
{
host.GotFocus -= new RoutedEventHandler(OnGotFocus);
host.LostFocus -= new RoutedEventHandler(OnLostFocus);
}
}
}
static void OnGotFocus(object sender, RoutedEventArgs e)
{
Control host = sender as Control;
_PreviousTextBoxBackgroundBrush = host.Background;
_PreviousTextBoxBorderBrush = host.BorderBrush;
_PreviousTextBoxBorderThickness = host.BorderThickness;
host.Background = Brushes.Yellow;
host.BorderBrush = Brushes.Red;
host.BorderThickness = new Thickness(1);
_CurrentControl = host;
if (_InstanceObject == null)
{
_InstanceObject = new TouchScreenKeyboard();
_InstanceObject.AllowsTransparency = true;
_InstanceObject.WindowStyle = WindowStyle.None;
_InstanceObject.ShowInTaskbar = false;
_InstanceObject.ShowInTaskbar = false;
_InstanceObject.ResizeMode = ResizeMode.NoResize;
_InstanceObject.MouseLeftButtonDown += new MouseButtonEventHandler(NonRectangularWindow_MouseLeftButtonDown);
_InstanceObject.Topmost = true;
syncchild();
}
}
static void NonRectangularWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_InstanceObject.DragMove();
}
static void tb_LayoutUpdated(object sender, EventArgs e)
{
syncchild();
}
static void OnLostFocus(object sender, RoutedEventArgs e)
{
Control host = sender as Control;
host.Background = _PreviousTextBoxBackgroundBrush;
host.BorderBrush = _PreviousTextBoxBorderBrush;
host.BorderThickness = _PreviousTextBoxBorderThickness;
if (_InstanceObject != null)
{
_InstanceObject.Close();
_InstanceObject = null;
}
}
#endregion
}
}

然后样式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TouchKeyboard.Keyboard" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Style x:Key="MyFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Grid Margin="4 2">
<Rectangle x:Name="r1" StrokeThickness="1" Stroke="Black" StrokeDashArray="2 2"/>
<Border x:Name="border" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" CornerRadius="2" BorderThickness="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ShadowStyle">
<Setter Property="Control.Foreground" Value="LightGray" />
</Style>
<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="10px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FF0EFF1D" Offset="0.059"/>
<GradientStop Color="#FFF7FFF7" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="1"
Padding="4,2"
CornerRadius="3" Height="Auto" Width="Auto" SnapsToDevicePixels="False" BorderBrush="#FF858585">
<Border.Effect>
<DropShadowEffect BlurRadius="0" Direction="270" ShadowDepth="2" Opacity="0.425" RenderingBias="Quality"/>
</Border.Effect>
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFCECECE" Offset="1"/>
<GradientStop Color="#FFF7FFF7"/>
</LinearGradientBrush>
</Border.Background>
<Grid >
<ContentPresenter HorizontalAlignment="Left"
VerticalAlignment="Center" x:Name="contentShadow"
Style="{StaticResource ShadowStyle}"/>
<ContentPresenter HorizontalAlignment="Left"
VerticalAlignment="Center" x:Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />
<Setter Property="Foreground" Value="#FF4788c8" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.35"/>
<GradientStop Color="Orange" Offset="0.95"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="1.0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:TouchScreenKeyboard}">
<Setter Property="Background" Value="White"/>
<Setter Property="Width" Value="600"/>
<Setter Property="Height" Value="220"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TouchScreenKeyboard}">
<Border
CornerRadius="5"
Focusable="False" BorderBrush="{x:Null}" BorderThickness="1">
<Border BorderBrush="{x:Null}" BorderThickness="1" CornerRadius="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFDADADA" Offset="0.442"/>
<GradientStop Color="#FFEFEFEF"/>
<GradientStop Color="#FFEDEDED" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Border BorderThickness="1,1,1,5" CornerRadius="5">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0.809">
<GradientStop Color="#FF4E4E4E" Offset="0.638"/>
<GradientStop Color="#FFCCCCCC" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Border BorderBrush="{x:Null}" BorderThickness="2,4,2,0" CornerRadius="5" Padding="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" >
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdTlide" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="~"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="`"/></TextBlock>
</StackPanel>
</Button>
<Button Command="local:TouchScreenKeyboard.Cmd1" Style="{StaticResource InformButton}" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="!"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="1"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd2" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="@"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="2"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd3" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="#"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="3"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd4" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="$"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="4"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd5" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="%"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="5"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd6" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="^"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="6"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd7" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="&amp;"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="7"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd8" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="*"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="8"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd9" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="("/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="9"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmd0" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text=")"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="0"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdMinus" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="--"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="-"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdPlus" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="+"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="="/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdBackspace" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="73.5" Height="35" Content="Backspace"/>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdTab" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="54.25" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Tab"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdQ" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Q"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.Cmdw" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="W"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdE" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="Auto">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="E"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdR" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="R"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdT" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="T"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdY" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Y"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdU" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="U"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdI" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="I"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdO" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="O"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdP" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="P"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdOpenCrulyBrace" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="{}{"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="["/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdEndCrultBrace" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="}"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="]"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdOR" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="54.25" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="|"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="\"/></TextBlock>
</StackPanel>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdCapsLock" HorizontalContentAlignment="Left" Width="71.75" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Caps Lock"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdA" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="A"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdS" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="S"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdD" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="D"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdF" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="F"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdG" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="G"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdH" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="H"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdJ" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="J"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdK" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="K"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdL" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="L"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdColon" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text=":"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text=";"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdDoubleInvertedComma" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="&quot;"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="'"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" VerticalContentAlignment="Top" Command="local:TouchScreenKeyboard.CmdEnter" HorizontalContentAlignment="Left" Width="74.55" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Enter"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdShift" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="92.05" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Shift"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdZ" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Z"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdX" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="X"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdC" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="C"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdV" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="V"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdB" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="B"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdN" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="N"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdM" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="M"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdLessThan" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="&lt;"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text=","/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdGreaterThan" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="&gt;"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="."/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdQuestion" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="35" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="?"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" ><Run Text="/"/></TextBlock>
</StackPanel>
</Button>
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdSpaceBar" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="92.05" Height="35">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" ><Run Text="Space"/></TextBlock>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" />
</StackPanel>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdClear" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="145" Height="Auto" Content="Clear"/>
<Grid Width="285" Height="35" />
<Button Style="{StaticResource InformButton}" Command="local:TouchScreenKeyboard.CmdClose" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Width="145" Height="Auto" Content="Close"/>
</StackPanel>
</StackPanel>
</Border>
</Border>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Opacity" Value="0.98"/>
</Style>
</ResourceDictionary>

调用方法:可以直接在该项目下操作,也可以把上面代码生成dll,引用到其他项目。使用时可以通过xaml关联或者动态设置。

一、XAML关联方式:

1.在Xaml页面设置k

1
xmlns:k="clr-namespace:TouchKeyboard.Keyboard;

2.TextBox控件或者Password中加入代码 k:TouchScreenKeyboard.TouchScreenKeyboard=”True”

例如:

1
<TextBox k:TouchScreenKeyboard.TouchScreenKeyboard="True" HorizontalAlignment="Left" Height="31" Margin="66,22,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="373" FontSize="16"/>

3.运行程序,当文本框获得焦点时弹出虚拟键盘。(仅支持TextBox和PassWord控件)

二、动态关联

通过SetValue直接操作DependencyProperty设置value为true即可

例如

1
txtUser.SetValue(TouchKeyboard.Keyboard.TouchScreenKeyboard.TouchScreenKeyboardProperty, true);

反之,如果要取消关联,就设置为false.

代码下载