• WPF学习笔记“窗口”二:入门


      制作自定义窗口

      

    View Code
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            AllowsTransparency="True" Background="{x:Null}" WindowStyle="None"
            x:Name="WindowName">
        <Grid>
            <Border>
                <Border.CornerRadius>
                    <CornerRadius TopLeft="20" TopRight="20" BottomLeft="20" BottomRight="20"></CornerRadius>
                </Border.CornerRadius>
                <Border.Background>
                    <LinearGradientBrush>
                        <GradientStop Offset="0" Color="Red"></GradientStop>
                        <GradientStop Offset="1" Color="Black"></GradientStop>
                        <GradientStop Offset="0.5" Color="Azure"></GradientStop>
                    </LinearGradientBrush>
                </Border.Background>
                <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
            </Border>
        </Grid>
    </Window>
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Interop;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            protected override void OnSourceInitialized( EventArgs e)
            {
    
                base.OnSourceInitialized(e);  
                HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
                if (hwndSource.IsDisposed == true)
                {
                    hwndSource.AddHook(new HwndSourceHook(this.WndProc));
                }
                else
                {
                   
                }
    
            }
    
            private const int WM_NCHITTEST = 0x0084;
            private readonly int agWidth = 12; //拐角宽度   
            private readonly int bThickness = 4; // 边框宽度   
            private Point mousePoint = new Point(); //鼠标坐标   
    
            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
    
                switch (msg)
                {
                    case WM_NCHITTEST:
                        this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
                        this.mousePoint.Y = (lParam.ToInt32() >> 16);
    
    
                        // 窗口左上角   
                        if (this.mousePoint.Y - this.Top <= this.agWidth
                           && this.mousePoint.X - this.Left <= this.agWidth)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTTOPLEFT);
                        }
                        // 窗口左下角       
                        else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth
                           && this.mousePoint.X - this.Left <= this.agWidth)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                        }
                        // 窗口右上角   
                        else if (this.mousePoint.Y - this.Top <= this.agWidth
                           && this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTTOPRIGHT);
                        }
                        // 窗口右下角   
                        else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth
                           && this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                        }
                        // 窗口左侧   
                        else if (this.mousePoint.X - this.Left <= this.bThickness)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTLEFT);
                        }
                        // 窗口右侧   
                        else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTRIGHT);
                        }
                        // 窗口上方   
                        else if (this.mousePoint.Y - this.Top <= this.bThickness)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTTOP);
                        }
                        // 窗口下方   
                        else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTBOTTOM);
                        }
                        else // 窗口移动   
                        {
                            handled = true;
                            return new IntPtr((int)HitTest.HTCAPTION);
                        }
                }
                return IntPtr.Zero;   
    
            }
            public static Int32 GET_X_LPARAM(int lParam)
            {
    
                return (lParam & 0xffff);
    
            }
            public static Int32 GET_Y_LPARAM(int lParam)
            {
    
                return (lParam >> 16);
    
            }
            public enum HitTest
            {
    
                HTERROR = -2,
    
                HTTRANSPARENT = -1,
    
                HTNOWHERE = 0,
    
                HTCLIENT = 1,
    
                HTCAPTION = 2,
    
                HTSYSMENU = 3,
    
                HTGROWBOX = 4,
    
                HTSIZE = HTGROWBOX,
    
                HTMENU = 5,
    
                HTHSCROLL = 6,
    
                HTVSCROLL = 7,
    
                HTMINBUTTON = 8,
    
                HTMAXBUTTON = 9,
    
                HTLEFT = 10,
    
                HTRIGHT = 11,
    
                HTTOP = 12,
    
                HTTOPLEFT = 13,
    
                HTTOPRIGHT = 14,
    
                HTBOTTOM = 15,
    
                HTBOTTOMLEFT = 16,
    
                HTBOTTOMRIGHT = 17,
    
                HTBORDER = 18,
    
                HTREDUCE = HTMINBUTTON,
    
                HTZOOM = HTMAXBUTTON,
    
                HTSIZEFIRST = HTLEFT,
    
                HTSIZELAST = HTBOTTOMRIGHT,
    
                HTOBJECT = 19,
    
                HTCLOSE = 20,
    
                HTHELP = 21,
    
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                
            }
        }
    }
  • 相关阅读:
    .net core 2.x默认不支持gb2312
    获取枚举描述
    C#中DateTime.Ticks
    验证组件FluentValidation的使用示例
    python计算出现错误
    EF的导航属性
    webpack不打包指定的js文件
    递归树处理,配合vue的vueTreeselect组件使用
    elementUI 日期时间选择器el-date-picker开始时间与结束时间约束
    Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
  • 原文地址:https://www.cnblogs.com/gengyuanchao/p/2725295.html
Copyright © 2020-2023  润新知