■ Window 클래스에서 제목줄 마우스 드래그를 통해 윈도우 이동을 방지하는 방법을 보여준다.
▶ NonDraggableWindow.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 |
using System; using System.Windows; using System.Windows.Interop; namespace TestProject; /// <summary> /// 드래그 불가능 윈도우 /// </summary> public class NonDraggableWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WM_NCLBUTTONDOWN /// </summary> private const int WM_NCLBUTTONDOWN = 0x00A1; /// <summary> /// HTCAPTION /// </summary> private const int HTCAPTION = 2; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 소스 초기화시 처리하기 - OnSourceInitialized(e) /// <summary> /// 소스 초기화시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); nint windowHandle = new WindowInteropHelper(this).Handle; HwndSource.FromHwnd(windowHandle)?.AddHook(WindowProcedure); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 프로시저 처리하기 - WindowProcedure(windowHandle, message, wordParameter, longParameter, handled) /// <summary> /// 윈도우 프로시저 처리하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <param name="handled">처리 여부</param> /// <returns>처리 결과</returns> private IntPtr WindowProcedure(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter, ref bool handled) { if(message == WM_NCLBUTTONDOWN && wordParameter.ToInt32() == HTCAPTION) { handled = true; return IntPtr.Zero; } return IntPtr.Zero; } #endregion } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<local:NonDraggableWindow 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" Title="TestProject" Width="800" Height="600" FontFamily="나눔고딕코딩" FontSize="16"> </local:NonDraggableWindow> |
▶ MainWindow.xaml.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 |
namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : NonDraggableWindow { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } |