728x90
반응형
728x170
▶ VirtualKeyboardHelper.cs
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
namespace TestProject
{
/// <summary>
/// 가상 키보드 헬퍼
/// </summary>
public static class VirtualKeyboardHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 가상 키보드 지원 여부 속성
/// </summary>
public static readonly DependencyProperty IsSurpportVirtualKeyboardProperty;
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 파일명
/// </summary>
private static string _fileName = "osk.exe";
/// <summary>
/// 프로세스
/// </summary>
private static Process _process = null;
/// <summary>
/// 라우티드 이벤트 핸들러
/// </summary>
private static RoutedEventHandler _routedEventHandler = new RoutedEventHandler(targetFrameworkElement_Focus);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Static
#region 생성자 - VirtualKeyboardHelper()
/// <summary>
/// 생성자
/// </summary>
static VirtualKeyboardHelper()
{
IsSurpportVirtualKeyboardProperty = DependencyProperty.RegisterAttached
(
"IsSurpportVirtualKeyboard",
typeof(bool?),
typeof(VirtualKeyboardHelper),
new FrameworkPropertyMetadata
(
null,
new PropertyChangedCallback(IsSurpportScreenKeyboard_PropertyChanged)
)
);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 가상 키보드 지원 여부 구하기 - GetIsSurpportVirtualKeyboard(targetFrameworkElement)
/// <summary>
/// 가상 키보드 지원 여부 구하기
/// </summary>
/// <param name="targetFrameworkElement">타겟 프레임워크 엘리먼트</param>
/// <returns>가상 키보드 지원 여부</returns>
public static bool GetIsSurpportVirtualKeyboard(FrameworkElement targetFrameworkElement)
{
return (bool)targetFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty);
}
#endregion
#region 가상 키보드 지원 여부 설정하기 - SetIsSurpportVirtualKeyboard(targetFrameworkElement, isSurpportVirtualKeyboard)
/// <summary>
/// 가상 키보드 지원 여부 설정하기
/// </summary>
/// <param name="targetFrameworkElement">타겟</param>
/// <param name="isSurpportVirtualKeyboard">가상 키보드 지원 여부</param>
public static void SetIsSurpportVirtualKeyboard(FrameworkElement targetFrameworkElement, bool? isSurpportVirtualKeyboard)
{
targetFrameworkElement.SetValue(IsSurpportVirtualKeyboardProperty, isSurpportVirtualKeyboard);
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 가상 키보드 지원 여부 속성 변경시 처리하기 - IsSurpportScreenKeyboard_PropertyChanged(dependencyObject, e)
/// <summary>
/// 가상 키보드 지원 여부 속성 변경시 처리하기
/// </summary>
/// <param name="dependencyObject">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void IsSurpportScreenKeyboard_PropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
FrameworkElement targetFrameworkElement = dependencyObject as FrameworkElement;
if((bool)e.NewValue)
{
targetFrameworkElement.GotFocus += _routedEventHandler;
targetFrameworkElement.LostFocus += _routedEventHandler;
}
else if(_routedEventHandler.GetInvocationList().First().Target == targetFrameworkElement)
{
targetFrameworkElement.GotFocus -= _routedEventHandler;
targetFrameworkElement.LostFocus -= _routedEventHandler;
}
}
#endregion
#region 타겟 프레임워크 엘리먼트 포커스시 처리하기 - targetFrameworkElement_Focus(sender, e)
/// <summary>
/// 타겟 프레임워크 엘리먼트 포커스시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void targetFrameworkElement_Focus(object sender, RoutedEventArgs e)
{
FrameworkElement targetFrameworkElement = sender as FrameworkElement;
FrameworkElement sourceFrameworkElement = e.Source as FrameworkElement;
if((bool?)targetFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty) == false)
{
targetFrameworkElement.GotFocus -= _routedEventHandler;
targetFrameworkElement.GotFocus -= _routedEventHandler;
return;
}
else if((bool?)sourceFrameworkElement.GetValue(IsSurpportVirtualKeyboardProperty) != false &&
e.RoutedEvent == FocusManager.GotFocusEvent && (e.Source is TextBox || e.Source is PasswordBox))
{
_process = Process.Start(_fileName);
}
else if(e.RoutedEvent == FocusManager.LostFocusEvent && _process != null)
{
if(_process.HasExited == false)
{
_process.CloseMainWindow();
}
_process = null;
}
}
#endregion
}
}
728x90
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestProject"
Width="800"
Height="600"
Title="가상 키보드 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
Margin="3">
<GroupBox Header="가상 키보드 사용">
<StackPanel
Margin="10 20 10 10"
local:VirtualKeyboardHelper.IsSurpportVirtualKeyboard="True">
<TextBlock
Margin="0 0 0 10"
Text="사용자 ID :" />
<TextBox
Margin="0 0 0 10"
Height="25" />
<TextBlock
Margin="0 0 0 10"
Text="비밀 번호 :" />
<PasswordBox
Margin="0 0 0 10"
Height="25" />
</StackPanel>
</GroupBox>
</Border>
<Border Grid.Column="1" Margin="3">
<GroupBox Header="가상 키보드 미사용">
<StackPanel
Margin="10 20 10 10"
local:VirtualKeyboardHelper.IsSurpportVirtualKeyboard="False">
<TextBlock
Margin="0 0 0 10"
Text="사용자 ID :" />
<TextBox
Margin="0 0 0 10"
Height="25" />
<TextBlock
Margin="0 0 0 10"
Text="비밀 번호 :" />
<PasswordBox
Margin="0 0 0 10"
Height="25" />
</StackPanel>
</GroupBox>
</Border>
</Grid>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] FontHelper 클래스 사용하기 (0) | 2019.06.28 |
---|---|
[C#/WPF] FontDialog 클래스 : WinForm 폰트 대화 상자 사용하기 (0) | 2019.06.28 |
[C#/WPF] 듀얼 모니터 보조 화면 창 열기 (0) | 2019.06.28 |
[C#/WPF] Window 클래스 : WindowStyle 속성을 사용해 윈도우 테두리 제거하기 (0) | 2019.06.25 |
[C#/WPF] 반투명 윈도우 사용하기 (0) | 2019.06.24 |
[C#/WPF] BlurEffect 클래스 사용하기 (0) | 2019.06.22 |
[C#/WPF] 커스텀 컨트롤 만들기 (0) | 2019.06.22 |
[C#/WPF] 반투명 윈도우 사용하기 (0) | 2019.06.16 |
[C#/WPF] 실시간 스트리밍 프토로콜(RTSP)을 사용해 동영상 재생하기 (0) | 2019.06.15 |
[C#/WPF] 웹 카메라 사용하기 (0) | 2019.06.14 |
댓글을 달아 주세요