Mouse Resizing cursor changes to normal in WPF?

MERUN KUMAR MAITY 636 Reputation points
2021-12-05T18:01:16.973+00:00

I have a WPF application, by default the resizing perfectly work. but as soon as I set the AllowsTransparency="True" the mouse resizing from edges and corner of the application is completely gone. I need to set AllowsTransparency="True" because if it's set to "False" then a white border comes at the top of the window.

To fix this issue I give border at every four edges and give some resizing event handler to them. The problem is appear on two border. The problem is, while I drag the edges to resize the cursor changes to normal. How I overcome this issue?

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-12-06T02:43:07.893+00:00

    Here is an example of resizing the window, you could see if it can work for you successfully.
    MainWindow.xaml:

    <Window x:Class="ResizeWindow.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:ResizeWindow" Name="window"    
            mc:Ignorable="d" AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize"   
            Title="MainWindow" Height="450" Width="800">  
        <Window.Resources>  
            <Style x:Key="RectBorderStyle" TargetType="Rectangle">  
                <Setter Property="Focusable" Value="False" />  
                <Setter Property="Fill" Value="Transparent" />  
                <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />  
                <EventSetter Event="MouseLeftButtonDown" Handler="Resize_Init"/>  
                <EventSetter Event="MouseLeftButtonUp" Handler="Resize_End"/>  
                <EventSetter Event="MouseMove" Handler="Resizeing_Window"/>  
            </Style>  
        </Window.Resources>  
        <Grid>  
            <Rectangle x:Name="leftSizeGrip" Width="7" HorizontalAlignment="Left" Cursor="SizeWE"  Style="{StaticResource RectBorderStyle}" />  
            <Rectangle x:Name="rightSizeGrip"  Width="7"  HorizontalAlignment="Right"  Cursor="SizeWE"  Style="{StaticResource RectBorderStyle}" />  
            <Rectangle x:Name="topSizeGrip" Height="7" VerticalAlignment="Top" Cursor="SizeNS"  Style="{StaticResource RectBorderStyle}" />  
            <Rectangle x:Name="bottomSizeGrip"   Height="7" VerticalAlignment="Bottom"   Cursor="SizeNS" Style="{StaticResource RectBorderStyle}" />  
            <Rectangle Name="bottomRighttSizeGrip"  Width="7"  Height="7" HorizontalAlignment="Right" VerticalAlignment="Bottom" Cursor="SizeNWSE" Style="{StaticResource RectBorderStyle}" />  
    
        </Grid>  
    </Window>  
    

    The result:
    155109-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html


1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-12-06T03:05:35.317+00:00

    I failed to fill MainWindow,xaml.cs in the answer, I filled it in the comment.
    MainWindow,xaml.cs:

    bool ResizeInProcess = false;  
        private void Resize_Init(object sender, MouseButtonEventArgs e)  
        {  
          Rectangle senderRect = sender as Rectangle;  
          if (senderRect != null)  
          {  
            ResizeInProcess = true;  
            senderRect.CaptureMouse();  
          }  
        }  
        private void Resize_End(object sender, MouseButtonEventArgs e)  
        {  
          Rectangle senderRect = sender as Rectangle;  
          if (senderRect != null)  
          {  
            ResizeInProcess = false; ;  
            senderRect.ReleaseMouseCapture();  
          }  
        }  
        private void Resizeing_Window(object sender, MouseEventArgs e)  
        {  
          if (ResizeInProcess)  
          {  
            Rectangle senderRect = sender as Rectangle;  
            Window mainWindow = senderRect.Tag as Window;  
            if (senderRect != null)  
            {  
              double width = e.GetPosition(mainWindow).X;  
              double height = e.GetPosition(mainWindow).Y;  
              senderRect.CaptureMouse();  
              if (senderRect.Name.ToLower().Contains("right"))  
              {  
                width += 5;  
                if (width > 0)  
                  mainWindow.Width = width;  
              }  
              if (senderRect.Name.ToLower().Contains("left"))  
              {  
                width -= 5;  
                mainWindow.Left += width;  
                width = mainWindow.Width - width;  
                if (width > 0)  
                {  
                  mainWindow.Width = width;  
                }  
              }  
              if (senderRect.Name.ToLower().Contains("bottom"))  
              {  
                height += 5;  
                if (height > 0)  
                  mainWindow.Height = height;  
              }  
              if (senderRect.Name.ToLower().Contains("top"))  
              {  
                height -= 5;  
                mainWindow.Top += height;  
                height = mainWindow.Height - height;  
                if (height > 0)  
                {  
                  mainWindow.Height = height;  
                }  
              }  
            }  
          }  
        }  
    

    update:

    Base on my search, you could use a transparent window to avoid this problem. First maximize the transparent window, and then put the content in the Canvas.
    Users will not know that what they see on the screen is not a real "window."
    MainWindow.xaml:

    <Window x:Class="windowResizeProblem.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:windowResizeProblem"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800"   
            WindowStyle="None" WindowState="Maximized"   
            Background="Transparent" AllowsTransparency="True">  
        <Canvas>  
            <Border x:Name="Content" Canvas.Left="500" Canvas.Top="500" Width="500" Height="500"   
                    MinWidth="100" MinHeight="100" CornerRadius="2"   Background="AliceBlue">  
                <Border.Effect>  
                    <DropShadowEffect ShadowDepth="0" BlurRadius="10"/>  
                </Border.Effect>  
                <Grid>  
                    <Grid.ColumnDefinitions>  
                        <ColumnDefinition Width="4" />  
                        <ColumnDefinition Width="*" />  
                        <ColumnDefinition Width="4" />  
                    </Grid.ColumnDefinitions>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="4" />  
                        <RowDefinition Height="Auto" />  
                        <RowDefinition Height="*" />  
                        <RowDefinition Height="4" />  
                    </Grid.RowDefinitions>  
                    <Thumb x:Name="topSizeGrip" Opacity="0" Grid.Row="0" Grid.Column="1" Cursor="SizeNS"  DragDelta="Thumb_DragDelta"/>  
                    <Thumb x:Name="bottomSizeGrip" Opacity="0" Grid.Row="3" Grid.Column="1" Cursor="SizeNS"  DragDelta="Thumb_DragDelta"/>  
                    <Thumb x:Name="leftSizeGrip" Opacity="0" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Cursor="SizeWE" DragDelta="Thumb_DragDelta"/>  
                    <Thumb x:Name="rightSizeGrip" Opacity="0" Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Cursor="SizeWE" DragDelta="Thumb_DragDelta"/>  
                    <Thumb x:Name="bottomRightSizeGrip" Opacity="0" Grid.Row="3" Grid.Column="2" Cursor="SizeNWSE" Tag="BR" DragDelta="Thumb_DragDelta"/>  
                    <Border Grid.Row="2" Grid.Column="1" CornerRadius="3" BorderThickness="1" BorderBrush="LightSeaGreen" Background="LightGoldenrodYellow">  
                        <TextBlock Text="my window" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>  
                    </Border>  
                </Grid>  
            </Border>  
        </Canvas>  
    </Window>  
    

    MainWindow.xaml.cs:

    using System;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Controls.Primitives;  
      
    namespace windowResizeProblem  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)  
        {  
          var str = (string)((Thumb)sender).Name.ToLower();  
      
          if (str.Contains("top"))  
          {  
            Content.Height = Math.Min(Math.Max(Content.MinHeight, Content.ActualHeight - e.VerticalChange), Content.MaxHeight);  
            Canvas.SetTop(Content, Canvas.GetTop(Content) - Content.Height + Content.ActualHeight);  
          }  
          if (str.Contains("left"))  
          {  
            Content.Width = Math.Min(Math.Max(Content.MinWidth, Content.ActualWidth - e.HorizontalChange), Content.MaxWidth);  
            Canvas.SetLeft(Content, Canvas.GetLeft(Content) - Content.Width + Content.ActualWidth);  
          }  
          if (str.Contains("bottom"))  
          {  
            Content.Height = Math.Min(Math.Max(Content.MinHeight, Content.ActualHeight + e.VerticalChange), Content.MaxHeight);  
          }  
          if (str.Contains("right"))  
          {  
            Content.Width = Math.Min(Math.Max(Content.MinWidth, Content.ActualWidth + e.HorizontalChange), Content.MaxWidth);  
          }  
          e.Handled = true;  
        }  
      }  
    }  
    

    The result:
    155563-3.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.