BaseForm.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.Imaging;
  10. using System.Drawing.Drawing2D;
  11. using HXX.Scanner.Client.Properties;
  12. namespace HXX.Scanner.Client.Base
  13. {
  14. public partial class BaseForm : Form
  15. {
  16. public BaseForm()
  17. {
  18. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  19. InitializeComponent();
  20. InitParts();
  21. }
  22. protected override void OnPaint(PaintEventArgs e)
  23. {
  24. Calculate();
  25. DrawBackground(e.Graphics);
  26. base.OnPaint(e);
  27. }
  28. PartBase partTopBG_Left;
  29. //PartBase partTopBG_Right;
  30. PartBase partTopBG_Center;
  31. PartBase partTopLine;
  32. PartBase partBottomLine;
  33. PartBase partLeftLine;
  34. PartBase partRightLine;
  35. PartBase partBottomSquare;
  36. FormWindowState _formState = FormWindowState.Normal;
  37. int _orginWidth;
  38. int _orginHeight;
  39. int _orginX;
  40. int _orginY;
  41. private void InitParts()
  42. {
  43. partTopLine = new PartBase();
  44. partTopLine.BackgroundBitmap = Resources.HLine;
  45. partBottomLine = new PartBase();
  46. partBottomLine.BackgroundBitmap = Resources.HLine;
  47. partLeftLine = new PartBase();
  48. partLeftLine.BackgroundBitmap = Resources.VLine;
  49. partRightLine = new PartBase();
  50. partRightLine.BackgroundBitmap = Resources.VLine;
  51. partTopBG_Left = new PartBase();
  52. partTopBG_Left.BackgroundBitmap = Resources.topbg_left;
  53. //partTopBG_Right = new PartBase();
  54. //partTopBG_Right.BackgroundBitmap = Resources.topbg_Right21;
  55. partTopBG_Center = new PartBase();
  56. partTopBG_Center.BackgroundBitmap = Resources.topbg_Center;
  57. partBottomSquare = new PartBase();
  58. partBottomSquare.BackgroundBitmap = Resources.BottomMiddle;
  59. b1.ReadButtonImage(Resources.MinNormal, Resources.MinMove, Resources.MinDown);
  60. //b2.ReadButtonImage(Resources.MaxNormal, Resources.MaxMove, Resources.MaxDown);
  61. b3.ReadButtonImage(Resources.CloseNormal, Resources.CloseMove, Resources.CloseDown);
  62. }
  63. private void Calculate()
  64. {
  65. partTopLine.X = 0;
  66. partTopLine.Y = 0;
  67. partTopLine.Height = partTopLine.BackgroundBitmap.Height;
  68. partTopLine.Width = this.Width;
  69. partBottomLine.X = 0;
  70. partBottomLine.Y = this.Height - partBottomLine.BackgroundBitmap.Height;
  71. partBottomLine.Height = partBottomLine.BackgroundBitmap.Height;
  72. partBottomLine.Width = this.Width;
  73. partLeftLine.X = 0;
  74. partLeftLine.Y = 0;
  75. partLeftLine.Height = this.Height;
  76. partLeftLine.Width = partLeftLine.BackgroundBitmap.Width;
  77. partRightLine.X = this.Width - partLeftLine.BackgroundBitmap.Width;
  78. partRightLine.Y = 0;
  79. partRightLine.Height = this.Height;
  80. partRightLine.Width = partRightLine.BackgroundBitmap.Width;
  81. partTopBG_Left.X = partLeftLine.Width;
  82. partTopBG_Left.Y = partTopLine.Height;
  83. partTopBG_Left.Height = partTopBG_Left.BackgroundBitmap.Height;
  84. partTopBG_Left.Width = partTopBG_Left.BackgroundBitmap.Width;
  85. //partTopBG_Right.X = partRightLine.X - partTopBG_Right.BackgroundBitmap.Width;
  86. //partTopBG_Right.Y = partTopLine.Height;
  87. //partTopBG_Right.Height = partTopBG_Right.BackgroundBitmap.Height;
  88. //partTopBG_Right.Width = partTopBG_Right.BackgroundBitmap.Width;
  89. partTopBG_Center.X = partTopBG_Left.X + partTopBG_Left.Width;
  90. partTopBG_Center.Y = partTopLine.Height;
  91. partTopBG_Center.Height = partTopBG_Center.BackgroundBitmap.Height;
  92. partTopBG_Center.Width = this.Width - partLeftLine.Width - partRightLine.Width - partTopBG_Left.Width;// - partTopBG_Right.Width;
  93. partBottomSquare.X = partLeftLine.Width;
  94. partBottomSquare.Y = this.Height - partBottomLine.Height - partBottomSquare.BackgroundBitmap.Height;
  95. partBottomSquare.Height = partBottomSquare.BackgroundBitmap.Height;
  96. partBottomSquare.Width = this.Width - partLeftLine.Width - partRightLine.Width;
  97. palCenter.Top = partTopBG_Left.Y + partTopBG_Left.Height;
  98. palCenter.Left = partLeftLine.Width;
  99. palCenter.Height = this.Height - partTopLine.Height - partTopBG_Left.Height - partBottomSquare.Height - partBottomLine.Height;
  100. palCenter.Width = this.Width - partLeftLine.Width - partRightLine.Width;
  101. b3.Top = partTopLine.Height;
  102. b3.Left = this.Width - 50;
  103. b2.Top = b3.Top;
  104. b2.Left = b3.Left - b2.Width + 1;
  105. b2.Visible = false;
  106. b1.Top = b3.Top;
  107. b1.Left = b2.Left - b1.Width + 1;
  108. }
  109. private void DrawBackground(Graphics g)
  110. {
  111. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  112. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  113. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  114. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  115. ImageAttributes attribute = new ImageAttributes();
  116. attribute.SetWrapMode(WrapMode.TileFlipXY);
  117. //g.Clear(Color.Transparent);
  118. //边框线
  119. partTopLine.DrawSelf(g, attribute);
  120. partBottomLine.DrawSelf(g, attribute);
  121. partLeftLine.DrawSelf(g, attribute);
  122. partRightLine.DrawSelf(g, attribute);
  123. //Title图
  124. partTopBG_Left.DrawSelf(g, null);
  125. //partTopBG_Right.DrawSelf(g, null);
  126. if (partTopBG_Center.Width > 0)
  127. {
  128. partTopBG_Center.DrawSelf(g, attribute);
  129. }
  130. //底部横块
  131. partBottomSquare.DrawSelf(g, attribute);
  132. attribute.Dispose();
  133. }
  134. private void NewBaseForm_MouseMove(object sender, MouseEventArgs e)
  135. {
  136. if (e.Button == MouseButtons.Left && _formState != FormWindowState.Maximized) //拖动窗体
  137. {
  138. if (e.Y < partTopBG_Left.X + partTopBG_Left.Height)
  139. {
  140. BaseFormWin32.ReleaseCapture();
  141. BaseFormWin32.SendMessage(Handle, 274, 61440 + 9, 0);
  142. return;
  143. }
  144. }
  145. }
  146. protected virtual void maxButton_Click(object sender, EventArgs e)
  147. {
  148. if (_formState == FormWindowState.Normal)
  149. {
  150. MaxWindow();
  151. }
  152. else
  153. {
  154. NormalWindow();
  155. }
  156. }
  157. protected virtual void minButton_Click(object sender, EventArgs e)
  158. {
  159. WindowState = FormWindowState.Minimized;
  160. }
  161. protected virtual void closeButton_Click(object sender, EventArgs e)
  162. {
  163. Close();
  164. }
  165. private void NormalWindow()
  166. {
  167. this.Invalidate();
  168. Left = _orginX;
  169. Top = _orginY;
  170. Width = _orginWidth;
  171. Height = _orginHeight;
  172. _formState = FormWindowState.Normal;
  173. }
  174. private void MaxWindow()
  175. {
  176. this.Invalidate();
  177. //最大化之前记录窗口信息便于缩小
  178. _orginHeight = Height;
  179. _orginWidth = Width;
  180. _orginX = Location.X;
  181. _orginY = Location.Y;
  182. Width = Screen.PrimaryScreen.WorkingArea.Width + 4;
  183. Height = Screen.PrimaryScreen.WorkingArea.Height + 4;
  184. Top = -1;
  185. Left = -1;
  186. _formState = FormWindowState.Maximized;
  187. }
  188. private void NewBaseForm_DoubleClick(object sender, EventArgs e)
  189. {
  190. //if (e.Y < partTopBG_Left.X + partTopBG_Left.Height)
  191. {
  192. maxButton_Click(null, null);
  193. }
  194. }
  195. private void NewBaseForm_Click(object sender, EventArgs e)
  196. {
  197. //Point p = this.PointToClient(Control.MousePosition);
  198. //if (p.X > partTopBG_Right.X && p.X < partTopBG_Right.X + partTopBG_Right.Width
  199. // && p.Y > partTopBG_Right.Y && p.Y < partTopBG_Right.Y + partTopBG_Right.Height)
  200. //{
  201. // frmHelp frm = new frmHelp();
  202. // frm.ShowDialog();
  203. //}
  204. }
  205. }
  206. class PartBase
  207. {
  208. public int X { get; set; }
  209. public int Y { get; set; }
  210. public int Width { get; set; }
  211. public int Height { get; set; }
  212. public Bitmap BackgroundBitmap { get; set; }
  213. public Rectangle Rectangle
  214. {
  215. get { return new Rectangle(X, Y, Width, Height); }
  216. }
  217. public void DrawSelf(Graphics g, ImageAttributes attribute)
  218. {
  219. if (attribute != null)
  220. {
  221. g.DrawImage(BackgroundBitmap, Rectangle, 0, 0, Width, Height, GraphicsUnit.Pixel, attribute);
  222. }
  223. else
  224. {
  225. g.DrawImage(BackgroundBitmap, Rectangle);
  226. }
  227. }
  228. }
  229. }