나의 개발일지
WinUi 3 마우스로 창 이동 본문
MainWindow.xaml
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="AppTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="Grid_MainWindow" Background="Aquamarine" PointerPressed="Grid_PointerPressed" PointerMoved="Grid_PointerMoved">
</Grid>
</Window>
MainWindow.xaml.cs
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Runtime.InteropServices;
using WinRT.Interop;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace AppTest
{
/// <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 AppWindow m_AppWindow;
private OverlappedPresenter m_AppWindow_overlapped;
private Windows.Foundation.Point _mouseDownLocation;
private DisplayArea _displayArea;
public MainWindow()
{
this.InitializeComponent();
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
m_AppWindow = AppWindow.GetFromWindowId(wndId);
m_AppWindow_overlapped = (OverlappedPresenter)m_AppWindow.Presenter;
if (m_AppWindow is not null)
{
_displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Nearest);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT lpPoint);
private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender is Grid grid && e.OriginalSource == sender)
{
if (e.GetCurrentPoint(grid).Properties.IsLeftButtonPressed)
{
POINT mousePosition;
var position = m_AppWindow.Position;
if (GetCursorPos(out mousePosition))
{
_mouseDownLocation.X = mousePosition.X - position.X;
_mouseDownLocation.Y = mousePosition.Y - position.Y;
}
}
}
}
private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (sender is Grid grid && e.OriginalSource == sender)
{
if (e.GetCurrentPoint(grid).Properties.IsLeftButtonPressed)
{
POINT mousePosition;
double point_X = 0;
double point_y = 0;
if (GetCursorPos(out mousePosition) && _displayArea is not null)
{
point_X = mousePosition.X - _mouseDownLocation.X;
point_y = mousePosition.Y - _mouseDownLocation.Y;
var position = m_AppWindow.Position;
position.X = (int)point_X;
position.Y = (int)point_y;
m_AppWindow.Move(position);
}
}
}
}
}
}
결과
'어플리케이션 > WinUi(C#)' 카테고리의 다른 글
WinUi 3 Full Custom Window And TitleBar (0) | 2024.01.03 |
---|---|
VisualStudio 2022 WinUi 3 윈도우 여러개 디버깅 오류(multi window debug Error In App.xaml.cs) (0) | 2023.12.28 |
WinUi 3 포커스(focus 비슷한)? 확인 (0) | 2023.12.22 |
WinUi 3 새로운 창 만들기 (0) | 2023.12.22 |
WinUi 3 GridView 사용법 (0) | 2023.12.13 |