하아찡
CustomTitleBar작업 -2(완) 본문
이번 글은 이전글에서 Xaml에서 Binding을 사용해서 가져왔던 cs 코드 입니다.
CustomTitleBar.cs 코드 입니다.
using Prism.Commands;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TitleBar.Themas.ResourceClass
{
internal class CustomTitleBar : UserControl
{
//기본 타이틀바 넓이
private static int TitleBarWidth = 50;
private int iconWidth = 30;
#region 의존성속성 값
//배경색 및 오버됐을때 색상
public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Transparent));
public static readonly DependencyProperty HoverBackColorProperty = DependencyProperty.Register("HoverBackColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Transparent));
//타이틀바 텍스트 색상
public static readonly DependencyProperty ContentColorProperty = DependencyProperty.Register("ContentColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Black));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(string), typeof(CustomTitleBar), new PropertyMetadata(string.Empty));
public static readonly DependencyProperty WindowFormProperty = DependencyProperty.Register("WindowForm", typeof(object), typeof(CustomTitleBar), new PropertyMetadata(null));
//종료 버튼
public static readonly DependencyProperty CloseButtonProperty = DependencyProperty.Register("CloseButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//축소 최대화 버튼
public static readonly DependencyProperty MinMaxButtonProperty = DependencyProperty.Register("MinMaxButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//최소화 버튼
public static readonly DependencyProperty MinimizeButtonProperty = DependencyProperty.Register("MinimizeButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//타이틀 이름
public static readonly DependencyProperty TitleNameProperty = DependencyProperty.Register("TitleName", typeof(string), typeof(CustomTitleBar), new PropertyMetadata("WindowForm"));
#endregion
#region 각종 이벤트
public ICommand CloseFormCommand { get; private set; }
public ICommand MinMaxFormCommand { get; private set; }
public ICommand MinimizeFormCommand { get; private set; }
public ICommand DragFormCommand { get; private set; }
#endregion
#region 값들
public Brush BackColor
{
get { return (Brush)GetValue(BackColorProperty); }
set { SetValue(BackColorProperty, value); }
}
public Brush HoverBackColor
{
get { return (Brush)GetValue(HoverBackColorProperty); }
set { SetValue(HoverBackColorProperty, value); }
}
public Brush ContentColor
{
get { return (Brush)GetValue(ContentColorProperty); }
set { SetValue(ContentColorProperty, value); }
}
public int IconWidth
{
get {
if (Icon == null || Icon == "")
return 0;
else
return iconWidth;
}
}
public string Icon
{
get { return (string)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public string TitleName
{
get { return (string)GetValue(TitleNameProperty); }
set { SetValue(TitleNameProperty, value); }
}
public object WindowForm
{
get { return (object)GetValue(WindowFormProperty); }
set { SetValue(WindowFormProperty, value); }
}
public int CloseButton
{
get { return (int)GetValue(CloseButtonProperty); }
set { SetValue(CloseButtonProperty, value); }
}
public int MinMaxButton
{
get { return (int)GetValue(MinMaxButtonProperty); }
set { SetValue(MinMaxButtonProperty, value); }
}
public int MinimizeButton
{
get { return (int)GetValue(MinimizeButtonProperty); }
set { SetValue(MinimizeButtonProperty, value); }
}
#endregion
public CustomTitleBar()
{
CloseFormCommand = new DelegateCommand(CloseForm);
MinMaxFormCommand = new DelegateCommand(MinMaxForm);
MinimizeFormCommand = new DelegateCommand(MinimizeForm);
DragFormCommand = new DelegateCommand(DragForm);
}
/// <summary>
/// 드래그 이벤트
/// </summary>
/// <param name="parameter"></param>
public void DragForm()
{
if (WindowForm is Window windowForm)
{
((Window)WindowForm).DragMove();
}
}
/// <summary>
/// 폼 종료 이벤트
/// </summary>
/// <param name="parameter"></param>
public void CloseForm()
{
if (WindowForm is Window windowForm)
{
((Window)WindowForm).Close();
}
}
/// <summary>
/// 축소 및 확대 이벤트
/// </summary>
/// <param name="parameter"></param>
public void MinMaxForm()
{
if (WindowForm is Window windowForm)
{
Window w = ((Window)WindowForm);
//최대화
if (w.WindowState == WindowState.Normal)
w.WindowState = WindowState.Maximized;
else
w.WindowState = WindowState.Normal;
}
}
/// <summary>
/// 최소화 이벤트
/// </summary>
/// <param name="parameter"></param>
public void MinimizeForm()
{
if (WindowForm is Window windowForm)
{
Window w = ((Window)WindowForm);
w.WindowState = WindowState.Minimized;
}
}
}
}
전체적인 코드에서 하나씩 살펴 보겠습니다.
그중에 의존성속성 코드를 먼저 보겠습니다. 아래와같이 CustomTitleBar컨트롤을 사용하기전에 필요한 속성값을을 미리 생성해 두었습니다.
public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Transparent));
public static readonly DependencyProperty HoverBackColorProperty = DependencyProperty.Register("HoverBackColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Transparent));
//타이틀바 텍스트 색상
public static readonly DependencyProperty ContentColorProperty = DependencyProperty.Register("ContentColor", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(Brushes.Black));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(string), typeof(CustomTitleBar), new PropertyMetadata(string.Empty));
public static readonly DependencyProperty WindowFormProperty = DependencyProperty.Register("WindowForm", typeof(object), typeof(CustomTitleBar), new PropertyMetadata(null));
//종료 버튼
public static readonly DependencyProperty CloseButtonProperty = DependencyProperty.Register("CloseButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//축소 최대화 버튼
public static readonly DependencyProperty MinMaxButtonProperty = DependencyProperty.Register("MinMaxButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//최소화 버튼
public static readonly DependencyProperty MinimizeButtonProperty = DependencyProperty.Register("MinimizeButton", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(TitleBarWidth));
//타이틀 이름
public static readonly DependencyProperty TitleNameProperty = DependencyProperty.Register("TitleName", typeof(string), typeof(CustomTitleBar), new PropertyMetadata("WindowForm"));
해당 타이틀바 배경색을 커스텀하기위해 BackColorProperty과 버튼을 호버했을때 변경될 HoverBackColorProperty값을 등록했습니다.
타이틀명을 저장 할 TitleNameProperty과 타이틀명 색상 및 버튼 Content 색상을 변경해줄 ContentColorProperty값을 등록.
해당 폼을 컨트롤하기위해 해당폼의 값을 전달받을 WindowFormProperty값을 등록.
각종 버튼 활성화 및 비활성화를 하기위해 넓이값을 저장하기위해
CloseButtonProperty, MinMaxButtonProperty, MinimizeButtonProperty값을 등록했음. 기본 값 private static int TitleBarWidth = 50 으로 등록 했습니다.
아이콘 등록을위해 아이콘 리소스 경로를 저장할 IconProperty값을 등록했습니다.
이벤트
#region 각종 이벤트
public ICommand CloseFormCommand { get; private set; }
public ICommand MinMaxFormCommand { get; private set; }
public ICommand MinimizeFormCommand { get; private set; }
public ICommand DragFormCommand { get; private set; }
#endregion
각종 버튼 및 드래그 이벤트를위해 ICommand를사용해서 이벤트 생성
생성된 이벤트 연결
public CustomTitleBar()
{
CloseFormCommand = new DelegateCommand(CloseForm);
MinMaxFormCommand = new DelegateCommand(MinMaxForm);
MinimizeFormCommand = new DelegateCommand(MinimizeForm);
DragFormCommand = new DelegateCommand(DragForm);
}
드래그 이벤트
public void DragForm()
{
if (WindowForm is Window windowForm)
{
((Window)WindowForm).DragMove();
}
}
Window.DragMove()를 사용해서 창을 움직일 수 있게 해줌.
폼 종료 이벤트
public void CloseForm()
{
if (WindowForm is Window windowForm)
{
((Window)WindowForm).Close();
}
}
Window.Close()를 사용해서 전달받은 폼을 종료시킴
축소 및 최대화 이벤트
public void MinMaxForm()
{
if (WindowForm is Window windowForm)
{
Window w = ((Window)WindowForm);
//최대화
if (w.WindowState == WindowState.Normal)
w.WindowState = WindowState.Maximized;
else
w.WindowState = WindowState.Normal;
}
}
Window.State값이 존재하는데 Normal일경우 일반적인 상태이고 Maximized면 최대화를 해주는 상태이다.
최소화 이벤트
public void MinimizeForm()
{
if (WindowForm is Window windowForm)
{
Window w = ((Window)WindowForm);
w.WindowState = WindowState.Minimized;
}
}
Window.State값이 Minimized 일경우 최소화 된 상태로 변경이 된다.
여기까지 CustomTitleBar 였습니다.
작업된 코드는 아래 파일로 업로드 해두었습니다.
감사합니다.
'WPF > XAML조각모음' 카테고리의 다른 글
CustomTitleBar작업 -1 (0) | 2023.08.18 |
---|---|
XAML TextBox에 TextBlock을 사용하여 PlaceHolder를 추가함. - 3 (0) | 2023.08.05 |
XAML TextBox에 TextBlock을 사용하여 PlaceHolder를 추가함. - 2 (0) | 2023.08.03 |
XAML TextBox에 TextBlock을 사용하여 PlaceHolder를 추가함. - 1 (0) | 2023.08.03 |
WPF XAML 작업중 헷갈리는 내용을 끄적이는곳 (0) | 2023.08.03 |