珂珂的个人博客 - 一个程序猿的个人网站

FYJ.Winui系列(四)-基窗体

   这当然也是费时间很多的一个点,我还在想着winform的继承思想,由于要继承不能有XAML代码,不过我们可以用变种方法来实现。WPF做界面确实比Winform好太多了.

我们可以把窗体写成一个Style,然后写一个类来作为基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Controls;
using FYJ.Winui.Util;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.ComponentModel;

namespace FYJ.Winui
{
    ///     /// Windows窗体基类
    ///     public class BaseWindow : Window
    {
        private enum ELocation
        {
            None,
            Top,
            LeftTop,
            RightTop
        }

        #region 常量
        private const int RESIZE_BORDER = 8;
        private const int HIDE_BORDER = 3;
        private const int FLASH_TIME = 500;
        private const int MAGNET_BORDER = 20;
        #endregion

        #region 变量
        private HwndSource _HwndSource;
        private ELocation _Location = ELocation.None;
        private bool _IsHidded = false;
        private Rect oldRect;
        private ColorPickerWindow pickerWindow;
        #endregion


        [Browsable(true)]
        [Description("是否显示换肤按钮")]
        public bool IsShowSkinButton
        {
            get;
            set;
        }

        [Browsable(true)]
        [Description("是否显示菜单按钮")]
        public bool IsShowMenuButton
        {
            get;
            set;
        }
        #region 构造函数
        public BaseWindow()
        {
            //this.Style = (Style)App.Current.Resources["MainWindowStyle"];
            this.SourceInitialized += delegate(object sender, EventArgs e)
            {
                this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
                this._HwndSource.AddHook(Win32.WindowProc);
            };

            this.Loaded += delegate
            {
                try
                {
                    ControlTemplate mainWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];

                    ButtonUserControl btnMenu = (ButtonUserControl)mainWindowTemplate.FindName("btnMenu", this);
                    ButtonUserControl btnSkin = (ButtonUserControl)mainWindowTemplate.FindName("btnSkin", this);
                    ButtonUserControl btnMin = (ButtonUserControl)mainWindowTemplate.FindName("btnMin", this);
                    ButtonUserControl btnMax = (ButtonUserControl)mainWindowTemplate.FindName("btnMax", this);

                    if (!IsShowMenuButton)
                    {
                        btnMenu.Visibility = Visibility.Collapsed;
                    }
                    if (IsShowSkinButton)
                    {
                        pickerWindow = new ColorPickerWindow();
                        ChangeColor(Color.FromArgb(pickerWindow.CurrentColor.A, pickerWindow.CurrentColor.R, pickerWindow.CurrentColor.G, pickerWindow.CurrentColor.B));
                        pickerWindow.ColorChanged += delegate(System.Windows.Media.Color c)
                        {
                            ChangeColor(c);
                        };

                        pickerWindow.SkinChanged += delegate(BitmapSource bitmapSource)
                        {
                            ImageBrush brush = new ImageBrush();
                            brush.ImageSource = bitmapSource;
                            brush.TileMode = TileMode.Tile;
                            //brush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox;
                            //brush.Viewport = new Rect(0, 0, 1, 1);
                            //brush.ViewboxUnits = BrushMappingMode.Absolute;
                            //brush.Viewbox = new Rect(0, 0, 0, 18);
                            Grid grid = (Grid)mainWindowTemplate.FindName("gridTitle", this);
                            if (grid != null)
                            {
                                grid.Background = brush;
                            }
                        };

                        pickerWindow.OpacityChanged += delegate(int value)
                        {
                            this.Opacity = Math.Round(value / 100.0, 2);
                        };
                    }
                    else
                    {
                        btnSkin.Visibility = Visibility.Collapsed;
                    }

                    if (this.ResizeMode == ResizeMode.NoResize)
                    {
                        btnMax.Visibility = Visibility.Collapsed;
                        btnMin.Visibility = Visibility.Collapsed;
                    }

                    if (this.ResizeMode == ResizeMode.CanMinimize)
                    {
                        btnMax.IsEnabled = false;
                    }

                    if (this.ResizeMode == ResizeMode.CanResize || this.ResizeMode == ResizeMode.CanResizeWithGrip)
                    {
                        ImageSourceConverter imgConv = new ImageSourceConverter();
                        if (this.WindowState == WindowState.Normal)
                        {
                            btnMax.ImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_max_normal.png");
                            btnMax.MouseEnterImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_max_highlight.png");
                            btnMax.ToolTip = "最大化";
                        }
                        else
                        {
                            btnMax.ImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_restore_normal.png");
                            btnMax.MouseEnterImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_restore_highlight.png");
                            btnMax.ToolTip = "向下还原";
                        }
                    }

                    InitializeEvent();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载失败:" + ex.Message);
                }
            };

            this.StateChanged += delegate
            {
                if (this.WindowState == WindowState.Maximized)
                {
                    System.Drawing.Size size = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size;
                    this.Width = size.Width;
                    this.Height = size.Height;
                }
                else
                {
                    this.Width = oldRect.Width;
                    this.Height = oldRect.Height;
                    this.Top = oldRect.Top;
                    this.Left = oldRect.Left;
                }
            };
        }
        #endregion

        private void ChangeColor(Color c)
        {
            ControlTemplate mainWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];
            SolidColorBrush brush = new SolidColorBrush(c);
            Grid grid = (Grid)mainWindowTemplate.FindName("gridTitle", this);
            if (grid != null)
            {
                grid.Background = brush;
            }
        }

        private void InitializeEvent()
        {
            ControlTemplate mainWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];
            Button minBtn = (Button)mainWindowTemplate.FindName("btnMin", this);
            ButtonUserControl maxBtn = (ButtonUserControl)mainWindowTemplate.FindName("btnMax", this);
            Button closeBtn = (Button)mainWindowTemplate.FindName("btnClose", this);
            Button skinBtn = (Button)mainWindowTemplate.FindName("btnSkin", this);

            minBtn.Click += delegate
            {
                oldRect = new Rect(this.Left, this.Top, this.Width, this.Height);
                this.WindowState = WindowState.Minimized;
            };

            maxBtn.Click += delegate
            {
                ImageSourceConverter imgConv = new ImageSourceConverter();
                if (this.WindowState == WindowState.Normal)
                {
                    oldRect = new Rect(this.Left, this.Top, this.Width, this.Height);
                    this.WindowState = WindowState.Maximized;
                    maxBtn.ImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_restore_normal.png");
                    maxBtn.MouseEnterImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_restore_highlight.png");
                    maxBtn.ToolTip = "向下还原";
                }
                else
                {
                    this.WindowState = WindowState.Normal;
                    maxBtn.ImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_max_normal.png");
                    maxBtn.MouseEnterImgSource = (ImageSource)imgConv.ConvertFromString("pack://application:,,,/FYJ.Winui;component/Resources/btn_max_highlight.png");
                    maxBtn.ToolTip = "最大化";
                }
            };

            closeBtn.Click += delegate
            {
                //Application.Current.Shutdown();
                this.Close();
                if (pickerWindow != null)
                {
                    pickerWindow.Close();
                }
            };

            //Border borderTitle = (Border)mainWindowTemplate.FindName("borderTitle", this);
            //borderTitle.MouseMove += delegate(object sender, MouseEventArgs e)
            //{
            //    if (e.LeftButton == MouseButtonState.Pressed)
            //    {
            //        this.DragMove();
            //    }
            //};
            //borderTitle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
            //{
            //    if (e.ClickCount >= 2)
            //    {
            //        maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            //    }
            //};

            skinBtn.Click += delegate
            {
                pickerWindow.WindowStartupLocation = WindowStartupLocation.Manual;
                pickerWindow.Top = this.Top + 18;
                pickerWindow.Left = this.Left + this.Width - 138 - pickerWindow.Width;
                pickerWindow.Show();
            };
        }

        #region 事件
        protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
        {
            if (pickerWindow != null)
            {
                pickerWindow.Hide();
            }

            base.OnMouseRightButtonDown(e);
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (pickerWindow != null)
            {
                pickerWindow.Hide();
            }

            Win32.POINT p;
            if (!Win32.GetCursorPos(out p))
                return;
            if (this.Left + RESIZE_BORDER  p.x && this.Top + RESIZE_BORDER  p.y)
            {
                if (this.WindowState == WindowState.Normal)
                {
                    this._Location = ELocation.None;
                }
                this.DragMove();
            }

            //双击最大化或还原
            if (this.ResizeMode == ResizeMode.CanResize || this.ResizeMode == ResizeMode.CanResizeWithGrip)
            {
                ControlTemplate mainWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];
                Button maxBtn = (Button)mainWindowTemplate.FindName("btnMax", this);
                if (e.ClickCount >= 2)
                {
                    maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                }
            }

            base.OnMouseLeftButtonDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.ResizeMode != ResizeMode.CanResize)
            {
                base.OnMouseMove(e);
                return;
            }

            Win32.POINT p;
            if (!Win32.GetCursorPos(out p))
                return;
            if (this.Left = p.x
                && this.Top = p.y)
            {
                this.Cursor = Cursors.SizeNWSE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61444), IntPtr.Zero);
            }
            else if (this.Left = p.x
                && this.Top + this.ActualHeight - RESIZE_BORDER = p.y)
            {
                this.Cursor = Cursors.SizeNESW;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61447), IntPtr.Zero);
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER = p.x
                && this.Top = p.y)
            {
                this.Cursor = Cursors.SizeNESW;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61445), IntPtr.Zero);
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER = p.x
                && this.Top + this.ActualHeight - RESIZE_BORDER = p.y)
            {
                this.Cursor = Cursors.SizeNWSE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61448), IntPtr.Zero);
            }
            else if (this.Top = p.y)
            {
                this.Cursor = Cursors.SizeNS;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61443), IntPtr.Zero);
            }
            else if (this.Left = p.x)
            {
                this.Cursor = Cursors.SizeWE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61441), IntPtr.Zero);
            }
            else if (this.Top + this.ActualHeight - RESIZE_BORDER = p.y)
            {
                this.Cursor = Cursors.SizeNS;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61446), IntPtr.Zero);
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER = p.x)
            {
                this.Cursor = Cursors.SizeWE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61442), IntPtr.Zero);
            }
            else
            {
                this.Cursor = Cursors.Arrow;
            }
            if (this.WindowState == WindowState.Normal)
            {
                if (this._IsHidded)
                {
                    if (this.Left  p.x && this.Top  p.y)
                    {
                        this.Top = 0;
                        this.Topmost = false;
                    }
                }
                else
                {
                    if (this.Top = SystemParameters.VirtualScreenWidth - this.ActualWidth)
                    {
                        this.Left = SystemParameters.VirtualScreenWidth - this.ActualWidth;
                        this.Top = HIDE_BORDER - this.ActualHeight;
                        this._Location = ELocation.RightTop;
                        this._IsHidded = true;
                        this.Topmost = true;
                    }
                    else if (this.Top " _ue_custom_node_="true">

上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧