123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- namespace LYFZ.ComponentLibrary
- {
- public partial class BaseForm360 : BaseShadowForm
- {
- public BaseForm360()
- {
- InitializeComponent();
- this.btnAppFormExit.Click += new EventHandler(btnAppFormExit_Click);
- this.btnAppFormMaximize.Click += new EventHandler(btnAppFormMaximize_Click);
- this.btnAppFormMaximize.Move += new EventHandler(btnAppFormMaximize_Move);
- this.btnAppFormMinimize.Click += new EventHandler(btnAppFormMinimize_Click);
- SetBtuList();
- if (this.IsMaximized)
- {
- this.btnAppFormMinimize.Enabled = true;
- }
- else
- {
- this.btnAppFormMinimize.Enabled = false;
- }
- this.Load += BaseForm360_Load;
- }
-
-
-
- #region 解决输入法BUG
- //解决输入法BUG
- [System.Runtime.InteropServices.DllImport("imm32.dll")]
- public static extern IntPtr ImmGetContext(IntPtr hwnd);
- [System.Runtime.InteropServices.DllImport("imm32.dll")]
- public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
- protected override void OnActivated(EventArgs e)
- {
- base.OnActivated(e);
- IntPtr HIme = ImmGetContext(this.Handle);
- ImmSetOpenStatus(HIme, true);
- }
- protected override void OnLoad(EventArgs e)
- {
- // 让输入法为开启半角状态
- ImeMode = ImeMode.OnHalf;
- // InputLanguage.CurrentInputLanguage
- base.OnLoad(e);
- }
-
-
- #endregion
- void BaseForm360_Load(object sender, EventArgs e)
- {
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
- }
-
- protected virtual void btnAppFormMaximize_Move(object sender, EventArgs e)
- {
- SetFormMzximizeBtn(this.WindowState);
-
- }
- /// <summary>
- /// 设置最大化按钮状态
- /// </summary>
- /// <param name="stat"></param>
- protected void SetFormMzximizeBtn(FormWindowState stat)
- {
- this.btnAppFormMaximize.IsCustomBackImg = true;
- if (stat == FormWindowState.Normal)
- {
- this.btnAppFormMaximize.BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_max;
- toolTipMessage.SetToolTip(this.btnAppFormMaximize, "最大化");
- }
- else
- {
- this.btnAppFormMaximize.BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_restore;
- toolTipMessage.SetToolTip(this.btnAppFormMaximize, "还原");
- }
-
-
- }
- protected void btnAppFormMinimize_Click(object sender, EventArgs e)
- {
- if (IsMinimize)
- {
-
- this.WindowState = FormWindowState.Minimized;
- }
-
- }
- protected void btnAppFormMaximize_Click(object sender, EventArgs e)
- {
- SetWindowState();
- }
- protected void btnAppFormExit_Click(object sender, EventArgs e)
- {
- if (IsCloseForm)
- {
- this.Close();
- }
- }
- bool isShowIcon = true;
- /// <summary>
- /// 标题栏是否显示图标
- /// </summary>
- [DescriptionAttribute("标题栏是否显示图标"), CategoryAttribute("自定义窗体属性")]
- public bool IsShowIcon
- {
- get { return isShowIcon; }
- set { isShowIcon = value; Invalidate(false); }
- }
- bool isShowTitle = true;
- /// <summary>
- /// 标题栏是否显示图标
- /// </summary>
- [DescriptionAttribute("标题栏是否显示标题文字"), CategoryAttribute("自定义窗体属性")]
- public bool IsShowTitle
- {
- get { return isShowTitle; }
- set { isShowTitle = value; Invalidate(false); }
- }
- /// <summary>
- /// 设置窗体标题
- /// </summary>
- /// <param name="e"></param>
- public void SetFormTitle(Graphics g)
- {
- try
- {
- Bitmap bit = this.Icon.ToBitmap();
- Rectangle r1, r2;
- r1 = new Rectangle(3, 3, 24, 24);
- r2 = new Rectangle(0, 0, bit.Width, bit.Height);
- if (this.IsShowIcon)
- {
- g.DrawImage(bit, r1, r2, GraphicsUnit.Pixel);
- }
- if (this.IsShowTitle)
- {
- Font fn = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
- g.DrawString(this.Text, fn, new SolidBrush(Color.White), r1.Width + 3, 3);
- }
- }
- catch
- {
- }
- }
- #region 属性设置
- bool _isCloseForm = true;
- /// <summary>
- /// 是否允许关闭窗体
- /// </summary>
- [Browsable(false), DescriptionAttribute("是否允许关闭窗体"), CategoryAttribute("自定义窗体属性")]
- public bool IsCloseForm
- {
- get { return _isCloseForm; }
- set { _isCloseForm = value; }
- }
- bool _isMinimize =true;
- /// <summary>
- /// 是否允许窗体最小化
- /// </summary>
- [Browsable(false), DescriptionAttribute("是否允许窗体最小化"), CategoryAttribute("自定义窗体属性")]
- public bool IsMinimize
- {
- get { return _isMinimize; }
- set {
- _isMinimize = value;
- //if (_isMinimize)
- //{
- // this.btnAppFormMinimize.Enabled = true;
- //}
- //else {
- // this.btnAppFormMinimize.Enabled = false;
- //}
-
- Invalidate(false);
- }
- }
- /// <summary>
- /// 重写Text属性
- /// </summary>
- [DescriptionAttribute("重写Text属性"), CategoryAttribute("自定义窗体属性")]
- public override string Text
- {
- set { base.Text = value; Invalidate(false); }
- get
- {
- return base.Text;
- }
- }
- bool _isShowBtnFormMenu = false;
- /// <summary>
- /// 是否显示菜单按钮
- /// </summary>
- [DescriptionAttribute("是否显示菜单按钮"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "false")]
- public bool IsShowBtnFormMenu
- {
- get { return _isShowBtnFormMenu; }
- set { _isShowBtnFormMenu = value;
- Invalidate(false);
- }
- }
- bool _isShowBtnFormSkin = false;
- /// <summary>
- /// 是否显示皮肤设置按钮
- /// </summary>
- [DescriptionAttribute("是否显示皮肤设置按钮"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "false")]
- public bool IsShowBtnFormSkin
- {
- get { return _isShowBtnFormSkin; }
- set { _isShowBtnFormSkin = value;
- Invalidate(false);
- }
- }
- bool _ShowBorder = true;
- /// <summary>
- /// 设置显示边框
- /// </summary>
- [DescriptionAttribute("设置显示边框"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")]
- public bool ShowBorder
- {
- get { return _ShowBorder; }
- set { _ShowBorder = value; Invalidate(false); }
- }
- Bitmap _TitleBgImage = LYFZ.ComponentLibrary.Properties.Resources.BGimg0;
- /// <summary>
- /// 设置顶部标题背景图片
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置顶部标题背景图片"), CategoryAttribute("自定义窗体属性")]
- public Bitmap TitleBgImage
- {
- get { return _TitleBgImage; }
- set { _TitleBgImage = value; Invalidate(false); }
- }
- int _TitleBgImageHeight = 100;
- /// <summary>
- /// 设置顶部标题栏高度
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置顶部标题栏高度"), CategoryAttribute("自定义窗体属性")]
- public int TitleBgImageHeight
- {
- get { return _TitleBgImageHeight; }
- set { _TitleBgImageHeight = value; Invalidate(false); }
- }
- Bitmap _BottomBgImage = LYFZ.ComponentLibrary.Properties.Resources.BG1;
- /// <summary>
- /// 设置底部状态栏背景图片
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置底部状态栏背景图片"), CategoryAttribute("自定义窗体属性")]
- public Bitmap BottomBgImage
- {
- get { return _BottomBgImage; }
- set { _BottomBgImage = value; Invalidate(false); }
- }
- int _BottomBgImageHeight = 25;
- /// <summary>
- /// 设置底部状态栏高度
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置底部状态栏高度"), CategoryAttribute("自定义窗体属性")]
- public int BottomBgImageHeight
- {
- get { return _BottomBgImageHeight; }
- set { _BottomBgImageHeight = value; Invalidate(false); }
- }
- CustomBorderStyle _CustomBorderStyle = new CustomBorderStyle();
- /// <summary>
- /// 边框样式
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置边框样式"), CategoryAttribute("自定义窗体属性"), TypeConverter(typeof(CustomBorderStyle))]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
- public CustomBorderStyle CustomBorderStyles
- {
- get { return _CustomBorderStyle; }
- set { _CustomBorderStyle = value; Invalidate(false); }
- }
- bool isShowCloseBox = true;
- /// <summary>
- /// 是否显示关闭按钮
- /// </summary>
- [Browsable(true), DescriptionAttribute("是否显示关闭按钮"), CategoryAttribute("自定义窗体属性")]
- public bool IsShowCloseBox
- {
- get { return isShowCloseBox; }
- set { isShowCloseBox = value; Invalidate(false); }
- }
- #region 自定义边框属性类
- [TypeConverter(typeof(CustomBorderStyle))]
- public class CustomBorderStyle : ExpandableObjectConverter
- {
- public CustomBorderStyle()
- {
- }
- public CustomBorderStyle(Color color, int width)
- {
- _BorderColor = color;
- _BorderWidth = width;
- }
- #region 重写转换方法
- /// <summary>
- /// 表示是否允许将给定类型的对象转换为自定义类型
- /// </summary>
- /// <param name="context">当前上下文对象</param>
- /// <param name="sourceType">给定的类型</param>
- /// <returns></returns>
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- //如果给定的类型为字符串,可以转换为自定义类型
- if (sourceType == typeof(string))
- {
- return true;
- }
- return base.CanConvertFrom(context, sourceType);
- }
- /// <summary>
- /// 表示是否允许将自定义类型转换为指定的类型
- /// </summary>
- /// <param name="context">当前上下文</param>
- /// <param name="destinationType">指定的类型</param>
- /// <returns></returns>
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- //如果目标类型是字符串,允许将自定义类型转换为字符串
- if (destinationType == typeof(string))
- {
- return true;
- }
- return base.CanConvertTo(context, destinationType);
- }
- /// <summary>
- /// 将指定类型转换为自定义类型
- /// </summary>
- /// <param name="context">当前上下文信息</param>
- /// <param name="culture">区域信息</param>
- /// <param name="value">指定类型</param>
- /// <returns></returns>
- public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
- {
- if (value == null)
- {
- return new CustomBorderStyle();
- }
- if (value is string)
- {
- string s = (string)value;
- if (s.Length == 0)
- {
- return new CustomBorderStyle();
- }
- string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
- if (parts.Length != 2)
- {
- throw new ArgumentException("输入的值转换无效", "value");
- }
- return new CustomBorderStyle(Color.FromName(parts[0]), Convert.ToInt16(parts[1]));
- }
- return base.ConvertFrom(context, culture, value);
- }
- /// <summary>
- /// 将自定义类型转换为指定类型
- /// </summary>
- /// <param name="context">当前上下文</param>
- /// <param name="culture">区域</param>
- /// <param name="value"></param>
- /// <param name="destinationType">指定类型</param>
- /// <returns></returns>
- public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
- {
- //如果要转换为自定义类型
- if (destinationType == typeof(string))
- {
- if (value is CustomBorderStyle)
- {
- CustomBorderStyle cb = (CustomBorderStyle)value;
- TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
- return String.Join(culture.TextInfo.ListSeparator,
- new string[] {
- stringConverter.ConvertToString(context, culture, cb.BorderColor.Name),
- stringConverter.ConvertToString(context, culture, cb.BorderWidth)
- });
- }
- else
- {
- throw new ArgumentException("类型转换失败,无效的 CustomBorderStyle 对象", "value");
- }
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- #endregion
- Color _BorderColor = LYFZ.ComponentLibrary.GetUIResources.FrmBorderColor;
- int _BorderWidth = 1;
- /// <summary>
- /// 设置边框颜色
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置边框颜色"), CategoryAttribute("自定义窗体属性"), NotifyParentProperty(true), DefaultValue(typeof(Color), "Blue")]
- public Color BorderColor
- {
- get { return _BorderColor; }
- set { _BorderColor = value; }
- }
- /// <summary>
- /// 设置边框线宽度
- /// </summary>
- [Browsable(true), DescriptionAttribute("设置边框线宽度"), CategoryAttribute("自定义窗体属性"), NotifyParentProperty(true), DefaultValue(1)]
- public int BorderWidth
- {
- get { return _BorderWidth; }
- set { _BorderWidth = value; }
- }
- }
- #endregion
- #endregion
- List<ButtonExpand> btnList = new List<ButtonExpand>();
- /// <summary>
- /// 添加按钮列表
- /// </summary>
- void SetBtuList()
- {
- btnList.Add(this.btnAppFormExit);
- btnList.Add(this.btnAppFormMaximize);
- btnList.Add(this.btnAppFormMinimize);
- btnList.Add(this.btnAppFormMenu);
- btnList.Add(this.btnAppFormSkin);
- }
- /// <summary>
- /// 设置按钮定位等
- /// </summary>
- void SetAppFormBtn()
- {
- try
- {
- int rightOffset = 5;
- int tempWidth = 0;
- for (int i = 0; i < btnList.Count; i++)
- {
- ButtonExpand tempBtn = btnList[i];
- if (tempBtn.Visible)
- {
- tempBtn.Location = new Point(this.Width - rightOffset - tempBtn.Width - tempWidth, 0);
- tempWidth += tempBtn.Width;
- }
- }
- }
- catch { }
- }
- bool isCustomBackgroundImgmae = false;
- /// <summary>
- /// 是否自定义背景
- /// </summary>
- public bool IsCustomBackgroundImgmae
- {
- get { return isCustomBackgroundImgmae; }
- set { isCustomBackgroundImgmae = value; }
- }
- /// <summary>
- /// 重写窗体
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BaseForm360_Paint(object sender, PaintEventArgs e)
- {
- try
- {
- if (!IsCustomBackgroundImgmae)
- {
- _TitleBgImage = LYFZ.ComponentLibrary.GetUIResources.GetTitleBgImage();
- }
- _BottomBgImage = LYFZ.ComponentLibrary.GetUIResources.GetBottomBgImage();
- Bitmap bit_top = new Bitmap(_TitleBgImage);
- Bitmap bit_bottom = new Bitmap(_BottomBgImage);
- Rectangle r1, r2;
- r1 = new Rectangle(0, 0, bit_top.Width, _TitleBgImageHeight);
- r2 = new Rectangle(0, 0, bit_top.Width, _TitleBgImageHeight);
- Graphics g = e.Graphics;
- g.SmoothingMode = SmoothingMode.HighQuality; //高质量
- g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
- for (int i = 0; i <= (this.Width / bit_top.Width); i++)
- {
- r1.X = bit_top.Width * i;
- g.DrawImage(bit_top, r1, r2, GraphicsUnit.Pixel);
- }
- r1 = new Rectangle(0, this.Height - _BottomBgImageHeight, bit_bottom.Width, _BottomBgImageHeight);
- r2 = new Rectangle(0, 0, bit_bottom.Width, _BottomBgImageHeight);
- for (int i = 0; i <= (this.Width / bit_bottom.Width); i++)
- {
- r1.X = bit_bottom.Width * i;
- g.DrawImage(bit_bottom, r1, r2, GraphicsUnit.Pixel);
- }
- if (ShowBorder)
- {
- //重绘 边线 边线颜色
- Pen p = new Pen(CustomBorderStyles.BorderColor);
- p.Width = CustomBorderStyles.BorderWidth;
- e.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
- }
- }
- catch { }
- //// Label lb=this.Controls["lbMseg"] as Label;
- try
- {
- if (!this.MinimizeBox)
- {
- this.btnAppFormMinimize.Visible = false;
- this.btnAppFormMaximize.Visible = false;
- }
- else
- {
- this.btnAppFormMinimize.Visible = true;
- if (this.MaximizeBox) { this.btnAppFormMaximize.Visible = true; }
- else
- {
- this.btnAppFormMaximize.Visible = false;
- }
- }
- if (!this.IsShowBtnFormMenu)
- {
- this.btnAppFormMenu.Visible = false;
- }
- else
- {
- this.btnAppFormMenu.Visible = true;
- }
- if (!this.IsShowBtnFormSkin)
- {
- this.btnAppFormSkin.Visible = false;
- }
- else
- {
- // this.btnAppFormSkin.Visible = true;//启用主题切换
- this.btnAppFormSkin.Visible = false;//禁用主题切换
- }
- if (IsShowCloseBox)
- {
- this.btnAppFormExit.Visible = true;
- }
- else
- {
- this.btnAppFormExit.Visible = false;
- }
-
- SetAppFormBtn();
- }
- catch {
-
- }
- try
- {
- SetFormTitle(e.Graphics);//设置标题
- }
- catch { }
-
- }
- }
- }
|