WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法
本文實(shí)例講述了WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
{
// 獲取窗體句柄
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
// 獲得窗體的 樣式
int oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);
// 更改窗體的樣式為無(wú)邊框窗體
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 詳見(jiàn)winuse.h文件
// 設(shè)置窗體為透明窗體
NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);
// 創(chuàng)建圓角窗體 12 這個(gè)值可以根據(jù)自身項(xiàng)目進(jìn)行設(shè)置
NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
}
NativeMethods.cs的代碼:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SEsoft
{
public class NativeMethods
{
/// <summary>
/// 帶有外邊框和標(biāo)題的windows的樣式
/// </summary>
public const int WS_CAPTION = 0X00C0000;
/// <summary>
/// window 擴(kuò)展樣式 分層顯示
/// </summary>
public const int WS_EX_LAYERED = 0x00080000;
/// <summary>
/// 帶有alpha的樣式
/// </summary>
public const int LWA_ALPHA = 0x00000002;
/// <summary>
/// 顏色設(shè)置
/// </summary>
public const int LWA_COLORKEY = 0x00000001;
/// <summary>
/// window的基本樣式
/// </summary>
public const int GWL_STYLE = -16;
/// <summary>
/// window的擴(kuò)展樣式
/// </summary>
public const int GWL_EXSTYLE = -20;
/// <summary>
/// 設(shè)置窗體的樣式
/// </summary>
/// <param name="handle">操作窗體的句柄</param>
/// <param name="oldStyle">進(jìn)行設(shè)置窗體的樣式類(lèi)型.</param>
/// <param name="newStyle">新樣式</param>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern void SetWindowLong(IntPtr handle, int oldStyle, int newStyle);
/// <summary>
/// 獲取窗體指定的樣式.
/// </summary>
/// <param name="handle">操作窗體的句柄</param>
/// <param name="style">要進(jìn)行返回的樣式</param>
/// <returns>當(dāng)前window的樣式</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr handle, int style);
/// <summary>
/// 設(shè)置窗體的工作區(qū)域.
/// </summary>
/// <param name="handle">操作窗體的句柄.</param>
/// <param name="handleRegion">操作窗體區(qū)域的句柄.</param>
/// <param name="regraw">if set to <c>true</c> [regraw].</param>
/// <returns>返回值</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);
/// <summary>
/// 創(chuàng)建帶有圓角的區(qū)域.
/// </summary>
/// <param name="x1">左上角坐標(biāo)的X值.</param>
/// <param name="y1">左上角坐標(biāo)的Y值.</param>
/// <param name="x2">右下角坐標(biāo)的X值.</param>
/// <param name="y2">右下角坐標(biāo)的Y值.</param>
/// <param name="width">圓角橢圓的width.</param>
/// <param name="height">圓角橢圓的height.</param>
/// <returns>hRgn的句柄</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">要進(jìn)行操作的窗口句柄</param>
/// <param name="colorKey">RGB的值</param>
/// <param name="alpha">Alpha的值,透明度</param>
/// <param name="flags">附帶參數(shù)</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);
}
}
希望本文所述對(duì)大家的WPF程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)文件操作(復(fù)制,移動(dòng),刪除)的方法詳解
File類(lèi)提供了常見(jiàn)的文件操作函數(shù),包括復(fù)制、移動(dòng)、刪除、創(chuàng)建快捷方式等,本文將通過(guò)一些簡(jiǎn)單的示例為大家詳細(xì)講講具體的使用,希望對(duì)大家有所幫助2023-05-05C#實(shí)現(xiàn)Oracle批量寫(xiě)入數(shù)據(jù)的方法詳解
往數(shù)據(jù)庫(kù)批量寫(xiě)入數(shù)據(jù),這個(gè)功能使用頻率相對(duì)還是比較高的,特別是在做一些導(dǎo)入等功能的時(shí)候。本文為大家介紹了C#實(shí)現(xiàn)Oracle批量寫(xiě)入數(shù)據(jù)的方法,需要的可以參考一下2022-11-11在Winform和WPF中注冊(cè)全局快捷鍵實(shí)現(xiàn)思路及代碼
如果注冊(cè)快捷鍵,RegisterHotKey中的fsModifiers參數(shù)為0,即None選項(xiàng),一些安全軟件會(huì)警報(bào),可能因?yàn)檫@樣就可以全局監(jiān)聽(tīng)鍵盤(pán)而造成安全問(wèn)題,感興趣的你可以參考下本文2013-02-02詳解C#如何使用屏障實(shí)現(xiàn)多線程并發(fā)操作保持同步
這篇文章主要為大家詳細(xì)介紹了C#如何使用屏障實(shí)現(xiàn)多線程并發(fā)操作保持同步,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-01-01WinForm實(shí)現(xiàn)的圖片拖拽與縮放功能示例
這篇文章主要介紹了WinForm實(shí)現(xiàn)的圖片拖拽與縮放功能,結(jié)合具體實(shí)例形式分析了WinForm鼠標(biāo)事件響應(yīng)及圖片元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05