栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

WPF设置窗体可以使用鼠标拖动大小的方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF设置窗体可以使用鼠标拖动大小的方法

本文实例讲述了WPF设置窗体可以使用鼠标拖动大小的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // 获取窗体句柄
    IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

    // 获得窗体的 样式
    int oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);

    // 更改窗体的样式为无边框窗体
    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 详见winuse.h文件
    // 设置窗体为透明窗体
    NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);

    // 创建圆角窗体  12 这个值可以根据自身项目进行设置
    NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
}

NativeMethods.cs的代码:
复制代码 代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SEsoft
{
    public class NativeMethods
    {
        ///


        /// 带有外边框和标题的windows的样式
        ///

        public const int WS_CAPTION = 0x00C0000;
 
        ///
        /// window 扩展样式 分层显示
        ///

        public const int WS_EX_LAYERED = 0x00080000;
 
        ///
        /// 带有alpha的样式
        ///

        public const int LWA_ALPHA = 0x00000002;
 
        ///
        /// 颜色设置
        ///

        public const int LWA_COLORKEY = 0x00000001;
 
        ///
        /// window的基本样式
        ///

        public const int GWL_STYLE = -16;
 
        ///
        /// window的扩展样式
        ///

        public const int GWL_EXSTYLE = -20;
 
        ///
        /// 设置窗体的样式
        ///

        /// 操作窗体的句柄
        /// 进行设置窗体的样式类型.
        /// 新样式
        [System.Runtime.InteropServices.Dllimport("User32.dll")]
        public static extern void SetWindowLong(IntPtr handle, int oldStyle, int newStyle);
 
        ///
        /// 获取窗体指定的样式.
        ///

        /// 操作窗体的句柄
        /// 要进行返回的样式
        /// 当前window的样式
        [System.Runtime.InteropServices.Dllimport("User32.dll")]
        public static extern int GetWindowLong(IntPtr handle, int style);
 
        ///
        /// 设置窗体的工作区域.
        ///

        /// 操作窗体的句柄.
        /// 操作窗体区域的句柄.
        /// if set to true [regraw].
        /// 返回值
        [System.Runtime.InteropServices.Dllimport("User32.dll")]
        public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);
 
        ///
        /// 创建带有圆角的区域.
        ///

        /// 左上角坐标的X值.
        /// 左上角坐标的Y值.
        /// 右下角坐标的X值.
        /// 右下角坐标的Y值.
        /// 圆角椭圆的width.
        /// 圆角椭圆的height.
        /// hRgn的句柄
        [System.Runtime.InteropServices.Dllimport("gdi32.dll")]
        public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height);
 
        ///
        /// Sets the layered window attributes.
        ///

        /// 要进行操作的窗口句柄
        /// RGB的值
        /// Alpha的值,透明度
        /// 附带参数
        /// true or false
        [System.Runtime.InteropServices.Dllimport("User32.dll")]
        public static extern bool SetLayeredWindowAttributes(IntPtr handle, uint colorKey, byte alpha, int flags);
    }
}

希望本文所述对大家的WPF程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/125866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号