BaseForm360.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Globalization;
  11. namespace LYFZ.ComponentLibrary
  12. {
  13. public partial class BaseForm360 : BaseShadowForm
  14. {
  15. public BaseForm360()
  16. {
  17. InitializeComponent();
  18. this.btnAppFormExit.Click += new EventHandler(btnAppFormExit_Click);
  19. this.btnAppFormMaximize.Click += new EventHandler(btnAppFormMaximize_Click);
  20. this.btnAppFormMaximize.Move += new EventHandler(btnAppFormMaximize_Move);
  21. this.btnAppFormMinimize.Click += new EventHandler(btnAppFormMinimize_Click);
  22. SetBtuList();
  23. if (this.IsMaximized)
  24. {
  25. this.btnAppFormMinimize.Enabled = true;
  26. }
  27. else
  28. {
  29. this.btnAppFormMinimize.Enabled = false;
  30. }
  31. this.Load += BaseForm360_Load;
  32. }
  33. #region 解决输入法BUG
  34. //解决输入法BUG
  35. [System.Runtime.InteropServices.DllImport("imm32.dll")]
  36. public static extern IntPtr ImmGetContext(IntPtr hwnd);
  37. [System.Runtime.InteropServices.DllImport("imm32.dll")]
  38. public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
  39. protected override void OnActivated(EventArgs e)
  40. {
  41. base.OnActivated(e);
  42. IntPtr HIme = ImmGetContext(this.Handle);
  43. ImmSetOpenStatus(HIme, true);
  44. }
  45. protected override void OnLoad(EventArgs e)
  46. {
  47. // 让输入法为开启半角状态
  48. ImeMode = ImeMode.OnHalf;
  49. // InputLanguage.CurrentInputLanguage
  50. base.OnLoad(e);
  51. }
  52. #endregion
  53. void BaseForm360_Load(object sender, EventArgs e)
  54. {
  55. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  56. }
  57. protected virtual void btnAppFormMaximize_Move(object sender, EventArgs e)
  58. {
  59. SetFormMzximizeBtn(this.WindowState);
  60. }
  61. /// <summary>
  62. /// 设置最大化按钮状态
  63. /// </summary>
  64. /// <param name="stat"></param>
  65. protected void SetFormMzximizeBtn(FormWindowState stat)
  66. {
  67. this.btnAppFormMaximize.IsCustomBackImg = true;
  68. if (stat == FormWindowState.Normal)
  69. {
  70. this.btnAppFormMaximize.BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_max;
  71. toolTipMessage.SetToolTip(this.btnAppFormMaximize, "最大化");
  72. }
  73. else
  74. {
  75. this.btnAppFormMaximize.BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_restore;
  76. toolTipMessage.SetToolTip(this.btnAppFormMaximize, "还原");
  77. }
  78. }
  79. protected void btnAppFormMinimize_Click(object sender, EventArgs e)
  80. {
  81. if (IsMinimize)
  82. {
  83. this.WindowState = FormWindowState.Minimized;
  84. }
  85. }
  86. protected void btnAppFormMaximize_Click(object sender, EventArgs e)
  87. {
  88. SetWindowState();
  89. }
  90. protected void btnAppFormExit_Click(object sender, EventArgs e)
  91. {
  92. if (IsCloseForm)
  93. {
  94. this.Close();
  95. }
  96. }
  97. bool isShowIcon = true;
  98. /// <summary>
  99. /// 标题栏是否显示图标
  100. /// </summary>
  101. [DescriptionAttribute("标题栏是否显示图标"), CategoryAttribute("自定义窗体属性")]
  102. public bool IsShowIcon
  103. {
  104. get { return isShowIcon; }
  105. set { isShowIcon = value; Invalidate(false); }
  106. }
  107. bool isShowTitle = true;
  108. /// <summary>
  109. /// 标题栏是否显示图标
  110. /// </summary>
  111. [DescriptionAttribute("标题栏是否显示标题文字"), CategoryAttribute("自定义窗体属性")]
  112. public bool IsShowTitle
  113. {
  114. get { return isShowTitle; }
  115. set { isShowTitle = value; Invalidate(false); }
  116. }
  117. /// <summary>
  118. /// 设置窗体标题
  119. /// </summary>
  120. /// <param name="e"></param>
  121. public void SetFormTitle(Graphics g)
  122. {
  123. try
  124. {
  125. Bitmap bit = this.Icon.ToBitmap();
  126. Rectangle r1, r2;
  127. r1 = new Rectangle(3, 3, 24, 24);
  128. r2 = new Rectangle(0, 0, bit.Width, bit.Height);
  129. if (this.IsShowIcon)
  130. {
  131. g.DrawImage(bit, r1, r2, GraphicsUnit.Pixel);
  132. }
  133. if (this.IsShowTitle)
  134. {
  135. Font fn = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
  136. g.DrawString(this.Text, fn, new SolidBrush(Color.White), r1.Width + 3, 3);
  137. }
  138. }
  139. catch
  140. {
  141. }
  142. }
  143. #region 属性设置
  144. bool _isCloseForm = true;
  145. /// <summary>
  146. /// 是否允许关闭窗体
  147. /// </summary>
  148. [Browsable(false), DescriptionAttribute("是否允许关闭窗体"), CategoryAttribute("自定义窗体属性")]
  149. public bool IsCloseForm
  150. {
  151. get { return _isCloseForm; }
  152. set { _isCloseForm = value; }
  153. }
  154. bool _isMinimize =true;
  155. /// <summary>
  156. /// 是否允许窗体最小化
  157. /// </summary>
  158. [Browsable(false), DescriptionAttribute("是否允许窗体最小化"), CategoryAttribute("自定义窗体属性")]
  159. public bool IsMinimize
  160. {
  161. get { return _isMinimize; }
  162. set {
  163. _isMinimize = value;
  164. //if (_isMinimize)
  165. //{
  166. // this.btnAppFormMinimize.Enabled = true;
  167. //}
  168. //else {
  169. // this.btnAppFormMinimize.Enabled = false;
  170. //}
  171. Invalidate(false);
  172. }
  173. }
  174. /// <summary>
  175. /// 重写Text属性
  176. /// </summary>
  177. [DescriptionAttribute("重写Text属性"), CategoryAttribute("自定义窗体属性")]
  178. public override string Text
  179. {
  180. set { base.Text = value; Invalidate(false); }
  181. get
  182. {
  183. return base.Text;
  184. }
  185. }
  186. bool _isShowBtnFormMenu = false;
  187. /// <summary>
  188. /// 是否显示菜单按钮
  189. /// </summary>
  190. [DescriptionAttribute("是否显示菜单按钮"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "false")]
  191. public bool IsShowBtnFormMenu
  192. {
  193. get { return _isShowBtnFormMenu; }
  194. set { _isShowBtnFormMenu = value;
  195. Invalidate(false);
  196. }
  197. }
  198. bool _isShowBtnFormSkin = false;
  199. /// <summary>
  200. /// 是否显示皮肤设置按钮
  201. /// </summary>
  202. [DescriptionAttribute("是否显示皮肤设置按钮"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "false")]
  203. public bool IsShowBtnFormSkin
  204. {
  205. get { return _isShowBtnFormSkin; }
  206. set { _isShowBtnFormSkin = value;
  207. Invalidate(false);
  208. }
  209. }
  210. bool _ShowBorder = true;
  211. /// <summary>
  212. /// 设置显示边框
  213. /// </summary>
  214. [DescriptionAttribute("设置显示边框"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")]
  215. public bool ShowBorder
  216. {
  217. get { return _ShowBorder; }
  218. set { _ShowBorder = value; Invalidate(false); }
  219. }
  220. Bitmap _TitleBgImage = LYFZ.ComponentLibrary.Properties.Resources.BGimg0;
  221. /// <summary>
  222. /// 设置顶部标题背景图片
  223. /// </summary>
  224. [Browsable(true), DescriptionAttribute("设置顶部标题背景图片"), CategoryAttribute("自定义窗体属性")]
  225. public Bitmap TitleBgImage
  226. {
  227. get { return _TitleBgImage; }
  228. set { _TitleBgImage = value; Invalidate(false); }
  229. }
  230. int _TitleBgImageHeight = 100;
  231. /// <summary>
  232. /// 设置顶部标题栏高度
  233. /// </summary>
  234. [Browsable(true), DescriptionAttribute("设置顶部标题栏高度"), CategoryAttribute("自定义窗体属性")]
  235. public int TitleBgImageHeight
  236. {
  237. get { return _TitleBgImageHeight; }
  238. set { _TitleBgImageHeight = value; Invalidate(false); }
  239. }
  240. Bitmap _BottomBgImage = LYFZ.ComponentLibrary.Properties.Resources.BG1;
  241. /// <summary>
  242. /// 设置底部状态栏背景图片
  243. /// </summary>
  244. [Browsable(true), DescriptionAttribute("设置底部状态栏背景图片"), CategoryAttribute("自定义窗体属性")]
  245. public Bitmap BottomBgImage
  246. {
  247. get { return _BottomBgImage; }
  248. set { _BottomBgImage = value; Invalidate(false); }
  249. }
  250. int _BottomBgImageHeight = 25;
  251. /// <summary>
  252. /// 设置底部状态栏高度
  253. /// </summary>
  254. [Browsable(true), DescriptionAttribute("设置底部状态栏高度"), CategoryAttribute("自定义窗体属性")]
  255. public int BottomBgImageHeight
  256. {
  257. get { return _BottomBgImageHeight; }
  258. set { _BottomBgImageHeight = value; Invalidate(false); }
  259. }
  260. CustomBorderStyle _CustomBorderStyle = new CustomBorderStyle();
  261. /// <summary>
  262. /// 边框样式
  263. /// </summary>
  264. [Browsable(true), DescriptionAttribute("设置边框样式"), CategoryAttribute("自定义窗体属性"), TypeConverter(typeof(CustomBorderStyle))]
  265. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
  266. public CustomBorderStyle CustomBorderStyles
  267. {
  268. get { return _CustomBorderStyle; }
  269. set { _CustomBorderStyle = value; Invalidate(false); }
  270. }
  271. bool isShowCloseBox = true;
  272. /// <summary>
  273. /// 是否显示关闭按钮
  274. /// </summary>
  275. [Browsable(true), DescriptionAttribute("是否显示关闭按钮"), CategoryAttribute("自定义窗体属性")]
  276. public bool IsShowCloseBox
  277. {
  278. get { return isShowCloseBox; }
  279. set { isShowCloseBox = value; Invalidate(false); }
  280. }
  281. #region 自定义边框属性类
  282. [TypeConverter(typeof(CustomBorderStyle))]
  283. public class CustomBorderStyle : ExpandableObjectConverter
  284. {
  285. public CustomBorderStyle()
  286. {
  287. }
  288. public CustomBorderStyle(Color color, int width)
  289. {
  290. _BorderColor = color;
  291. _BorderWidth = width;
  292. }
  293. #region 重写转换方法
  294. /// <summary>
  295. /// 表示是否允许将给定类型的对象转换为自定义类型
  296. /// </summary>
  297. /// <param name="context">当前上下文对象</param>
  298. /// <param name="sourceType">给定的类型</param>
  299. /// <returns></returns>
  300. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  301. {
  302. //如果给定的类型为字符串,可以转换为自定义类型
  303. if (sourceType == typeof(string))
  304. {
  305. return true;
  306. }
  307. return base.CanConvertFrom(context, sourceType);
  308. }
  309. /// <summary>
  310. /// 表示是否允许将自定义类型转换为指定的类型
  311. /// </summary>
  312. /// <param name="context">当前上下文</param>
  313. /// <param name="destinationType">指定的类型</param>
  314. /// <returns></returns>
  315. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  316. {
  317. //如果目标类型是字符串,允许将自定义类型转换为字符串
  318. if (destinationType == typeof(string))
  319. {
  320. return true;
  321. }
  322. return base.CanConvertTo(context, destinationType);
  323. }
  324. /// <summary>
  325. /// 将指定类型转换为自定义类型
  326. /// </summary>
  327. /// <param name="context">当前上下文信息</param>
  328. /// <param name="culture">区域信息</param>
  329. /// <param name="value">指定类型</param>
  330. /// <returns></returns>
  331. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  332. {
  333. if (value == null)
  334. {
  335. return new CustomBorderStyle();
  336. }
  337. if (value is string)
  338. {
  339. string s = (string)value;
  340. if (s.Length == 0)
  341. {
  342. return new CustomBorderStyle();
  343. }
  344. string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
  345. if (parts.Length != 2)
  346. {
  347. throw new ArgumentException("输入的值转换无效", "value");
  348. }
  349. return new CustomBorderStyle(Color.FromName(parts[0]), Convert.ToInt16(parts[1]));
  350. }
  351. return base.ConvertFrom(context, culture, value);
  352. }
  353. /// <summary>
  354. /// 将自定义类型转换为指定类型
  355. /// </summary>
  356. /// <param name="context">当前上下文</param>
  357. /// <param name="culture">区域</param>
  358. /// <param name="value"></param>
  359. /// <param name="destinationType">指定类型</param>
  360. /// <returns></returns>
  361. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
  362. {
  363. //如果要转换为自定义类型
  364. if (destinationType == typeof(string))
  365. {
  366. if (value is CustomBorderStyle)
  367. {
  368. CustomBorderStyle cb = (CustomBorderStyle)value;
  369. TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
  370. return String.Join(culture.TextInfo.ListSeparator,
  371. new string[] {
  372. stringConverter.ConvertToString(context, culture, cb.BorderColor.Name),
  373. stringConverter.ConvertToString(context, culture, cb.BorderWidth)
  374. });
  375. }
  376. else
  377. {
  378. throw new ArgumentException("类型转换失败,无效的 CustomBorderStyle 对象", "value");
  379. }
  380. }
  381. return base.ConvertTo(context, culture, value, destinationType);
  382. }
  383. #endregion
  384. Color _BorderColor = LYFZ.ComponentLibrary.GetUIResources.FrmBorderColor;
  385. int _BorderWidth = 1;
  386. /// <summary>
  387. /// 设置边框颜色
  388. /// </summary>
  389. [Browsable(true), DescriptionAttribute("设置边框颜色"), CategoryAttribute("自定义窗体属性"), NotifyParentProperty(true), DefaultValue(typeof(Color), "Blue")]
  390. public Color BorderColor
  391. {
  392. get { return _BorderColor; }
  393. set { _BorderColor = value; }
  394. }
  395. /// <summary>
  396. /// 设置边框线宽度
  397. /// </summary>
  398. [Browsable(true), DescriptionAttribute("设置边框线宽度"), CategoryAttribute("自定义窗体属性"), NotifyParentProperty(true), DefaultValue(1)]
  399. public int BorderWidth
  400. {
  401. get { return _BorderWidth; }
  402. set { _BorderWidth = value; }
  403. }
  404. }
  405. #endregion
  406. #endregion
  407. List<ButtonExpand> btnList = new List<ButtonExpand>();
  408. /// <summary>
  409. /// 添加按钮列表
  410. /// </summary>
  411. void SetBtuList()
  412. {
  413. btnList.Add(this.btnAppFormExit);
  414. btnList.Add(this.btnAppFormMaximize);
  415. btnList.Add(this.btnAppFormMinimize);
  416. btnList.Add(this.btnAppFormMenu);
  417. btnList.Add(this.btnAppFormSkin);
  418. }
  419. /// <summary>
  420. /// 设置按钮定位等
  421. /// </summary>
  422. void SetAppFormBtn()
  423. {
  424. try
  425. {
  426. int rightOffset = 5;
  427. int tempWidth = 0;
  428. for (int i = 0; i < btnList.Count; i++)
  429. {
  430. ButtonExpand tempBtn = btnList[i];
  431. if (tempBtn.Visible)
  432. {
  433. tempBtn.Location = new Point(this.Width - rightOffset - tempBtn.Width - tempWidth, 0);
  434. tempWidth += tempBtn.Width;
  435. }
  436. }
  437. }
  438. catch { }
  439. }
  440. bool isCustomBackgroundImgmae = false;
  441. /// <summary>
  442. /// 是否自定义背景
  443. /// </summary>
  444. public bool IsCustomBackgroundImgmae
  445. {
  446. get { return isCustomBackgroundImgmae; }
  447. set { isCustomBackgroundImgmae = value; }
  448. }
  449. /// <summary>
  450. /// 重写窗体
  451. /// </summary>
  452. /// <param name="sender"></param>
  453. /// <param name="e"></param>
  454. private void BaseForm360_Paint(object sender, PaintEventArgs e)
  455. {
  456. try
  457. {
  458. if (!IsCustomBackgroundImgmae)
  459. {
  460. _TitleBgImage = LYFZ.ComponentLibrary.GetUIResources.GetTitleBgImage();
  461. }
  462. _BottomBgImage = LYFZ.ComponentLibrary.GetUIResources.GetBottomBgImage();
  463. Bitmap bit_top = new Bitmap(_TitleBgImage);
  464. Bitmap bit_bottom = new Bitmap(_BottomBgImage);
  465. Rectangle r1, r2;
  466. r1 = new Rectangle(0, 0, bit_top.Width, _TitleBgImageHeight);
  467. r2 = new Rectangle(0, 0, bit_top.Width, _TitleBgImageHeight);
  468. Graphics g = e.Graphics;
  469. g.SmoothingMode = SmoothingMode.HighQuality; //高质量
  470. g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
  471. for (int i = 0; i <= (this.Width / bit_top.Width); i++)
  472. {
  473. r1.X = bit_top.Width * i;
  474. g.DrawImage(bit_top, r1, r2, GraphicsUnit.Pixel);
  475. }
  476. r1 = new Rectangle(0, this.Height - _BottomBgImageHeight, bit_bottom.Width, _BottomBgImageHeight);
  477. r2 = new Rectangle(0, 0, bit_bottom.Width, _BottomBgImageHeight);
  478. for (int i = 0; i <= (this.Width / bit_bottom.Width); i++)
  479. {
  480. r1.X = bit_bottom.Width * i;
  481. g.DrawImage(bit_bottom, r1, r2, GraphicsUnit.Pixel);
  482. }
  483. if (ShowBorder)
  484. {
  485. //重绘 边线 边线颜色
  486. Pen p = new Pen(CustomBorderStyles.BorderColor);
  487. p.Width = CustomBorderStyles.BorderWidth;
  488. e.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
  489. }
  490. }
  491. catch { }
  492. //// Label lb=this.Controls["lbMseg"] as Label;
  493. try
  494. {
  495. if (!this.MinimizeBox)
  496. {
  497. this.btnAppFormMinimize.Visible = false;
  498. this.btnAppFormMaximize.Visible = false;
  499. }
  500. else
  501. {
  502. this.btnAppFormMinimize.Visible = true;
  503. if (this.MaximizeBox) { this.btnAppFormMaximize.Visible = true; }
  504. else
  505. {
  506. this.btnAppFormMaximize.Visible = false;
  507. }
  508. }
  509. if (!this.IsShowBtnFormMenu)
  510. {
  511. this.btnAppFormMenu.Visible = false;
  512. }
  513. else
  514. {
  515. this.btnAppFormMenu.Visible = true;
  516. }
  517. if (!this.IsShowBtnFormSkin)
  518. {
  519. this.btnAppFormSkin.Visible = false;
  520. }
  521. else
  522. {
  523. // this.btnAppFormSkin.Visible = true;//启用主题切换
  524. this.btnAppFormSkin.Visible = false;//禁用主题切换
  525. }
  526. if (IsShowCloseBox)
  527. {
  528. this.btnAppFormExit.Visible = true;
  529. }
  530. else
  531. {
  532. this.btnAppFormExit.Visible = false;
  533. }
  534. SetAppFormBtn();
  535. }
  536. catch {
  537. }
  538. try
  539. {
  540. SetFormTitle(e.Graphics);//设置标题
  541. }
  542. catch { }
  543. }
  544. }
  545. }