■ Window 클래스 : 윈도우 테두리 제거하기
------------------------------------------------------------------------------------------------------------------------
▶ MainWindow.xaml
<Window Name="window" x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell" Width="800" Height="600" Title="윈도우 테두리 제거하기"> <shell:WindowChrome.WindowChrome> <shell:WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0" ResizeBorderThickness="5" /> </shell:WindowChrome.WindowChrome> <Grid> <Grid.RowDefinitions> <RowDefinition Height="27" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border x:Name="headerBorder"> <Border.Background> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Color="#ff444749" Offset="0" /> <GradientStop Color="#ff0c0e0e" Offset="1" /> <GradientStop Color="#ff101012" Offset="0.504" /> </LinearGradientBrush> </Border.Background> <Grid> <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> <Grid Margin="10 0 0 0" Width="16" Height="16" Background="White"> <Rectangle Fill="Yellow" /> </Grid> <Label Margin="5 0 0 0" Foreground="White" Content="{Binding ElementName=window, Path=Title}" /> </StackPanel> <StackPanel Margin="0 0 5 0" HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal"> <Button Width="20" Click="minimizedButton_Click"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid Width="16" Height="16"> <Border x:Name="border" Background="#ff2f2f2f" Opacity="0" SnapsToDevicePixels="True" /> <Rectangle x:Name="minimizedRectangle" Margin="0 10 0 0" VerticalAlignment="Center" Width="12" Height="2" Fill="#ff7b7776" SnapsToDevicePixels="True" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true" /> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="minimizedRectangle" Property="Fill" Value="White" /> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="minimizedRectangle" Property="Fill" Value="#ff8b8988" /> <Setter TargetName="minimizedRectangle" Property="Opacity" Value="0.7" /> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> </Button> <ToggleButton Width="20" Click="maximizedButton_Click"> <ToggleButton.Style> <Style TargetType="{x:Type ToggleButton}"> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Grid Width="16" Height="16" Background="Transparent"> <Border x:Name="border" Background="#002f2f2f" SnapsToDevicePixels="True" /> <Grid> <Path x:Name="path_max" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="12" Height="10" Fill="#ff7b7776" Stretch="Fill" Visibility="Visible" Data=" M 57.001 182.001 L 46.998 182.001 L 46.998 174.026 L 57.001 174.026 z M 46 183 L 58 183 L 58 173 L 46 173 z" /> <Path x:Name="path_max_Check" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="12" Height="10" Fill="#FF7B7776" Stretch="Fill" Visibility="Collapsed" Data=" M 0.99799991 4.0260001 L 0.99799991 9.0010005 8.0009999 9.0010005 8.0009999 4.0260001 z M 3.9999999 1.0000001 L 3.9999999 3.0000001 9 3.0000001 9 6.0000001 11 6.0000001 11 1.0000001 z M 3 0 L 12 0 12 7.0000001 9 7.0000001 9 10 0 10 0 3.0000001 3 3.0000001 z" /> </Grid> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true" /> <Trigger Property="IsChecked" Value="true"> <Setter Property="Visibility" TargetName="path_max_Check" Value="Visible" /> <Setter Property="Visibility" TargetName="path_max" Value="Collapsed" /> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsChecked" Value="false" /> <Condition Property="IsMouseOver" Value="true" /> </MultiTrigger.Conditions> <Setter Property="Fill" TargetName="path_max" Value="White" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsChecked" Value="true" /> <Condition Property="IsMouseOver" Value="true" /> </MultiTrigger.Conditions> <Setter Property="Fill" TargetName="path_max_Check" Value="#ff8b8988" /> </MultiTrigger> <Trigger Property="IsMouseOver" Value="True" /> <Trigger Property="IsPressed" Value="true"> <Setter Property="Fill" TargetName="path_max" Value="White" /> <Setter Property="Fill" TargetName="path_max_Check" Value="White" /> <Setter Property="Opacity" TargetName="path_max" Value="0.7" /> <Setter Property="Opacity" TargetName="path_max_Check" Value="0.7" /> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ToggleButton.Style> </ToggleButton> <Button Width="20" Click="closeButton_Click"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid Width="16" Height="16"> <Border x:Name="border" Background="#FF2F2F2F" Opacity="0" SnapsToDevicePixels="True" /> <Path x:Name="path_Closed" HorizontalAlignment="Center" VerticalAlignment="Center" Width="11" Height="10" SnapsToDevicePixels="True" Fill="#ff7b7776" StrokeThickness="0" Stretch="Fill" Visibility="Visible" Data=" M 0.73200232 0 L 5.5 4.2873921 10.267998 0 11 0.76599883 6.2921877 4.9997287 11 9.2330017 10.268002 10 5.5 5.712142 0.73199838 10 0 9.2330017 4.7078123 4.9997287 0 0.76599883 z" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true" /> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Fill" TargetName="path_Closed" Value="White" /> <Setter Property="Width" TargetName="path_Closed" Value="12" /> <Setter Property="Height" TargetName="path_Closed" Value="10" /> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter Property="Fill" TargetName="path_Closed" Value="#ff8b8988" /> <Setter Property="Opacity" TargetName="path_Closed" Value="0.7" /> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> </Button> </StackPanel> </Grid> </Border> <Border Grid.Row="1" BorderThickness="1" BorderBrush="Black"> <Ellipse Margin="10" Fill="RoyalBlue" /> </Border> </Grid> </Window>
|
▶ MainWindow.xaml.cs
using System; using System.Diagnostics; using System.Windows; using System.Windows.Input;
namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent();
StateChanged += Window_StateChanged; this.headerBorder.MouseLeftButtonDown += headerBorder_MouseLeftButtonDown; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event
#region 윈도우 상태 변경시 처리하기 - Window_StateChanged(sender, e)
/// <summary> /// 윈도우 상태 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_StateChanged(object sender, EventArgs e) { switch(WindowState) { case WindowState.Maximized : SetMaximizedState(); break; case WindowState.Normal : case WindowState.Minimized : SetNormalOrMinimizedState(); break; } }
#endregion #region 헤더 테두리 마우스 이동시 처리하기 - headerBorder_MouseMove(sender, e)
/// <summary> /// 헤더 테두리 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void headerBorder_MouseMove(object sender, MouseEventArgs e) { if(this.WindowState != WindowState.Maximized) { return; }
if(Mouse.LeftButton != MouseButtonState.Pressed) { return; }
this.WindowState = WindowState.Normal;
this.Left = GetWindowLeft(); this.Top = 1;
this.DragMove(); }
#endregion #region 헤더 테두리 마우스 왼쪽 버튼 DOWN 처리하기 - headerBorder_MouseLeftButtonDown(sender, e)
/// <summary> /// 헤더 테두리 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void headerBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if(e.ClickCount == 2) { SwitchState(); }
if((this.ResizeMode == ResizeMode.CanResize) || (this.ResizeMode == ResizeMode.CanResizeWithGrip)) { try { this.DragMove(); } catch(Exception exception) { Debug.WriteLine(exception); } } }
#endregion #region 최소화 버튼 클릭시 처리하기 - minimizedButton_Click(sender, e)
/// <summary> /// 최소화 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void minimizedButton_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; }
#endregion #region 최대화 버튼 클릭시 처리하기 - maximizedButton_Click(sender, e)
/// <summary> /// 최대화 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void maximizedButton_Click(object sender, RoutedEventArgs e) { if(WindowState == WindowState.Maximized) { WindowState = WindowState.Normal; } else { WindowState = WindowState.Maximized; } }
#endregion #region 닫기 버튼 클릭시 처리하기 - closeButton_Click(sender, e)
/// <summary> /// 닫기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeButton_Click(object sender, RoutedEventArgs e) { Close(); }
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 최대화 상태 설정하기 - SetMaximizedState()
/// <summary> /// 최대화 상태 설정하기 /// </summary> private void SetMaximizedState() { this.headerBorder.MouseMove += headerBorder_MouseMove; }
#endregion #region 일반 또는 최소화 상태 설정하기 - SetNormalOrMinimizedState()
/// <summary> /// 일반 또는 최소화 상태 설정하기 /// </summary> private void SetNormalOrMinimizedState() { this.headerBorder.MouseMove -= headerBorder_MouseMove; }
#endregion #region 윈도우 왼쪽 구하기 - GetWindowLeft()
/// <summary> /// 윈도우 왼쪽 구하기 /// </summary> /// <returns>윈도우 왼쪽</returns> private double GetWindowLeft() { ScreenManager screenManager = ScreenManager.GetScreenManager(this);
double leftBound = screenManager.DesktopArea.Left; double rightBound = screenManager.DesktopArea.Right;
Point currentPoint = GetMousePosition();
double left = currentPoint.X - (this.Width / 2);
if(left < leftBound) { return leftBound; }
double right = currentPoint.X + (this.Width / 2);
if(right > rightBound) { return rightBound - this.Width; }
return left; }
#endregion #region 마우스 위치 구하기 - GetMousePosition()
/// <summary> /// 마우스 위치 구하기 /// </summary> /// <returns>마우스 위치</returns> private Point GetMousePosition() { System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new Point(point.X, point.Y); }
#endregion #region 상태 전화하기 - SwitchState()
/// <summary> /// 상태 전화하기 /// </summary> private void SwitchState() { switch(WindowState) { case WindowState.Normal : WindowState = WindowState.Maximized; break; case WindowState.Maximized : WindowState = WindowState.Normal; break; } }
#endregion } }
|
▶ ScreenManager.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Windows.Interop;
namespace TestProject { /// <summary> /// 화면 관리자 /// </summary> public class ScreenManager { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary> /// 화면 /// </summary> private readonly Screen screen;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public
#region 주 화면 관리자 - Primary
/// <summary> /// 주 화면 관리자 /// </summary> public static ScreenManager Primary { get { return new ScreenManager(Screen.PrimaryScreen); } }
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public
#region 데스크톱 영역 - DesktopArea
/// <summary> /// 데스크톱 영역 /// </summary> public Rect DesktopArea { get { return GetRect(this.screen.Bounds); } }
#endregion #region 작업 영역 - WorkingArea
/// <summary> /// 작업 영역 /// </summary> public Rect WorkingArea { get { return GetRect(this.screen.WorkingArea); } }
#endregion #region 주 화면 여부 - IsPrimary
/// <summary> /// 주 화면 여부 /// </summary> public bool IsPrimary { get { return this.screen.Primary; } }
#endregion #region 장치명 - DeviceName
/// <summary> /// 장치명 /// </summary> public string DeviceName { get { return this.screen.DeviceName; } }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public
#region 모든 화면 관리자들 구하기 - GetAllScreenManagers()
/// <summary> /// 모든 화면 관리자들 구하기 /// </summary> /// <returns>모든 화면 관리자들</returns> public static IEnumerable<ScreenManager> GetAllScreenManagers() { foreach(Screen screen in Screen.AllScreens) { yield return new ScreenManager(screen); } }
#endregion #region 화면 관리자 구하기 - GetScreenManager(window)
/// <summary> /// 화면 관리자 구하기 /// </summary> /// <param name="window">윈도우</param> /// <returns>화면 관리자</returns> public static ScreenManager GetScreenManager(Window window) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);
Screen screen = Screen.FromHandle(windowInteropHelper.Handle);
ScreenManager screenManager = new ScreenManager(screen);
return screenManager; }
#endregion #region 화면 관리자 구하기 - GetScreenManager(sourcePoint)
/// <summary> /// 화면 관리자 구하기 /// </summary> /// <param name="sourcePoint">소스 포인트</param> /// <returns>화면 관리자</returns> public static ScreenManager GetScreenManager(System.Windows.Point sourcePoint) { int sourceX = (int)Math.Round(sourcePoint.X); int sourceY = (int)Math.Round(sourcePoint.Y);
System.Drawing.Point targetPoint = new System.Drawing.Point(sourceX, sourceY);
Screen screen = Screen.FromPoint(targetPoint);
ScreenManager screenManager = new ScreenManager(screen);
return screenManager; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Internal
#region 생성자 - ScreenManager(screen)
/// <summary> /// 생성자 /// </summary> /// <param name="screen">화면</param> internal ScreenManager(Screen screen) { this.screen = screen; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 윈도우 시작 위치 설정하기 - SetWindowStartupPosition(window)
/// <summary> /// 윈도우 시작 위치 설정하기 /// </summary> /// <param name="window">윈도우</param> public static void SetWindowStartupPosition(Window window) { if(window.WindowStartupLocation == WindowStartupLocation.Manual) { return; }
switch(window.WindowStartupLocation) { case WindowStartupLocation.CenterOwner :
Window ownerWindow = window.Owner;
if(ownerWindow == null) { return; }
SetWindowCenterLocation(window, new Rect(ownerWindow.Left, ownerWindow.Top, ownerWindow.Width, ownerWindow.Height));
break;
case WindowStartupLocation.CenterScreen :
ScreenManager screenManager = ScreenManager.GetScreenManager(window);
SetWindowCenterLocation(window, screenManager.WorkingArea);
break; }
window.Visibility = Visibility.Visible; }
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region WPF 사각형 구하기 - GetRect(rectangle)
/// <summary> /// WPF 사각형 구하기 /// </summary> /// <param name="rectangle">WinForm 사각형</param> /// <returns>WPF 사각형</returns> private Rect GetRect(Rectangle rectangle) { return new Rect { X = rectangle.X, Y = rectangle.Y, Width = rectangle.Width, Height = rectangle.Height }; }
#endregion #region 윈도우 중앙 위치 설정하기 - SetWindowCenterLocation(window, sourceRect)
/// <summary> /// 윈도우 중앙 위치 설정하기 /// </summary> /// <param name="window">윈도우</param> /// <param name="sourceRect">소스 사각형</param> private static void SetWindowCenterLocation(Window window, Rect sourceRect) { var centerLeft = sourceRect.X + (sourceRect.Width / 2); var centerTop = sourceRect.Y + (sourceRect.Height / 2);
window.Left = centerLeft - (window.Width / 2); window.Top = centerTop - (window.Height / 2); }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Keyboard 클래스 : CTRL+C 키 입력 처리하기 (0) | 2016.01.24 |
---|---|
[C#/WPF] KeyBinding 엘리먼트 : CTRL+X 단축키 사용하기 (0) | 2016.01.24 |
[C#/WPF] CTRL+X 키 입력 처리하기 (0) | 2016.01.24 |
[C#/WPF] Point 클래스 : WinForm Screen 구하기 (0) | 2016.01.20 |
[C#/WPF] Window 클래스 : 스크린 구하기 (0) | 2016.01.20 |
[C#/WPF] Window 클래스 : 윈도우 테두리 제거하기 (0) | 2016.01.20 |
[C#/WPF] JpegBitmapEncoder 클래스 : JPEG 파일 저장하기 (0) | 2015.12.27 |
[C#/WPF] DrawingBrush 엘리먼트 : GeometryDrawing 객체를 사용해 격자 배경 브러시 만들기 (0) | 2015.12.27 |
[C#/WPF] RichTextBox 클래스 : 문서 생성하기 (0) | 2015.12.22 |
[C#/WPF] RichTextBox 클래스 : 텍스트 구하기 (0) | 2015.12.22 |
[C#/WPF] RichTextBox 엘리먼트 : 문단 간격 줄이기 (0) | 2015.12.22 |
댓글을 달아 주세요