0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WPF で Aero Snap を無効化

Last updated at Posted at 2021-12-07

概要

AeroSnap をどうしても消したかったの!😣
ちょっといじくったら Windows Forms でも使えそう?

こんな感じ (適当に変えてね)

MainWindow.xaml
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" 
        WindowStyle="None">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="0" ResizeBorderThickness="0"/>
    </WindowChrome.WindowChrome>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="45"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button x:Name="CloseButton" Background="Transparent" Content="&#xE8BB;" 
                FontFamily="Segoe MDL2 Assets" FontSize="8.5" 
                BorderThickness="0" RenderOptions.EdgeMode="Aliased"
                Grid.Column = "1"/>
        <Grid Grid.Row="1" Grid.ColumnSpan="2"/>
        <Canvas x:Name="DragHandle" Background="Transparent"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int GWL_STYLE           = -16;
        int WS_OVERLAPPEDWINDOW = 0;
        int WS_THICKFRAME       = 0x00040000;

        [DllImport("user32")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwLong);

        public MainWindow()
        {
            InitializeComponent();
            
            this.CloseButton.Click                      
                += CloseButton_Click;                       //ウィンドウ閉じるボタン

            this.DragHandle.PreviewMouseLeftButtonDown  
                += DragHandle_PreviewMouseLeftButtonDown;   //ドラッグ移動

            
            this.Loaded += MainWindow_Loaded; //ウィンドウがロードされたら
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            => SetWindowLong(
                    new WindowInteropHelper(this).Handle,
                    GWL_STYLE,
                    WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME);

        private void CloseButton_Click(object sender, RoutedEventArgs e)
            => this.Close();

        private void DragHandle_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            => this.DragMove();

    }
}

試せ

MainWindow.xaml.cs
this.Loaded += MainWindow_Loaded; //ウィンドウがロードされたら

(多分)32行目の、上のやつコメントアウトしたりしなかったりして違いを確かめよう!

ウィンドウをドラッグで移動させてマウスカーソルが画面端でごっつんこしなかったら成功!

(コピペしてもうまく動かなかったらごめんなさい僕は動いたんです😥)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?