WinUI 3: Title Bar에서 더블 클릭 차단

발행: (2025년 12월 5일 오후 01:01 GMT+9)
2 min read
원문: Dev.to

Source: Dev.to

목표

제목 표시줄을 더블 클릭했을 때 메인 창이 최대화되는 것을 방지하고, 나머지 동작은 그대로 유지합니다.

목표

전체 코드

using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using System.Runtime.InteropServices;
using Windows.Graphics;
using WinRT.Interop;

namespace TestApp.WinUI3
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        private readonly IntPtr _hwnd;
        private readonly IntPtr _oldWndProc;
        private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
        private readonly WndProcDelegate _newWndProc;
        private const uint WM_NCLBUTTONDBLCLK = 0x00A3;
        private const int GWLP_WNDPROC = -4;

        public MainWindow()
        {
            InitializeComponent();
            AppWindow.TitleBar.PreferredTheme = TitleBarTheme.UseDefaultAppMode;
            ExtendsContentIntoTitleBar = true;
            SetTitleBar(titleBar);
            OverlappedPresenter? presenter = OverlappedPresenter.Create();
            presenter.IsResizable = false;
            presenter.IsMaximizable = false;
            presenter.SetBorderAndTitleBar(true, true);
            AppWindow.Resize(new SizeInt32(425, 600));
            AppWindow.SetPresenter(presenter);
            _hwnd = WindowNative.GetWindowHandle(this);
            _newWndProc = new WndProcDelegate(WndProc);
            _oldWndProc = SetWindowLongPtr(_hwnd, GWLP_WNDPROC, _newWndProc);
        }

        private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == WM_NCLBUTTONDBLCLK)
            {
                return IntPtr.Zero;
            }
            return CallWindowProc(_oldWndProc, hWnd, msg, wParam, lParam);
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
        private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, WndProcDelegate newProc);

        [DllImport("user32.dll", EntryPoint = "CallWindowProc")]
        private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    }
}
Back to Blog

관련 글

더 보기 »

C#에서 Excel을 HTML로 변환하는 방법

소개 웹에서 표 형식 데이터를 표시하는 것은 종종 어려울 수 있습니다. Excel 스프레드시트는 데이터 조직 및 분석에 강력하지만, 그들의 nativ...