WPF sets the size of a form that can be dragged with the mouse
- 2020-12-09 00:59:26
- OfStack
This example shows how WPF sets the size of a form that can be dragged with the mouse. Share to everybody for everybody reference. Specific implementation methods are as follows:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Gets the form handle
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
// Get form's style
int oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);
// Change the style of the form to a bezel-less form
NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_STYLE, oldstyle & ~NativeMethods.WS_CAPTION);
// SetWindowLong(hwnd, GWL_EXSTYLE, oldstyle & ~WS_EX_LAYERED);
// 1 | 2 << 8 | 3 << 16 r=1,g=2,b=3 As shown in the winuse.h file
// Set the form to be transparent
NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);
// Create a rounded form 12 This value can be set according to its own project
NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
}
NativeMethods. cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SEsoft
{
public class NativeMethods
{
/// <summary>
/// With an outer border and a title windows The style of the
/// </summary>
public const int WS_CAPTION = 0X00C0000;
/// <summary>
/// window Extension style Hierarchical display
/// </summary>
public const int WS_EX_LAYERED = 0x00080000;
/// <summary>
/// with alpha The style of the
/// </summary>
public const int LWA_ALPHA = 0x00000002;
/// <summary>
/// Color Settings
/// </summary>
public const int LWA_COLORKEY = 0x00000001;
/// <summary>
/// window Basic style of
/// </summary>
public const int GWL_STYLE = -16;
/// <summary>
/// window Extended style of
/// </summary>
public const int GWL_EXSTYLE = -20;
/// <summary>
/// Sets the style of the form
/// </summary>
/// <param name="handle"> Handle to the action form </param>
/// <param name="oldStyle"> Sets the style type of the form .</param>
/// <param name="newStyle"> New style </param>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern void SetWindowLong(IntPtr handle, int oldStyle, int newStyle);
/// <summary>
/// Gets the style specified by the form .
/// </summary>
/// <param name="handle"> Handle to the action form </param>
/// <param name="style"> The style to return </param>
/// <returns> The current window The style of the </returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr handle, int style);
/// <summary>
/// Sets the work area of the form .
/// </summary>
/// <param name="handle"> Handle to the action form .</param>
/// <param name="handleRegion"> Handle to manipulate the form region .</param>
/// <param name="regraw">if set to <c>true</c> [regraw].</param>
/// <returns> The return value </returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);
/// <summary>
/// Create an area with rounded corners .
/// </summary>
/// <param name="x1"> Top-left coordinate X value .</param>
/// <param name="y1"> Top-left coordinate Y value .</param>
/// <param name="x2"> Of the lower right hand corner X value .</param>
/// <param name="y2"> Of the lower right hand corner Y value .</param>
/// <param name="width"> Oval with rounded corners width.</param>
/// <param name="height"> Oval with rounded corners height.</param>
/// <returns>hRgn The handle of </returns>
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height);
/// <summary>
/// Sets the layered window attributes.
/// </summary>
/// <param name="handle"> Handle to the window to operate on </param>
/// <param name="colorKey">RGB The value of the </param>
/// <param name="alpha">Alpha The value of transparency </param>
/// <param name="flags"> With parameters </param>
/// <returns>true or false</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr handle, uint colorKey, byte alpha, int flags);
}
}
I hope this article has been helpful for your WPF programming.