Ruler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows.Forms;
  7. using Microsoft.VisualBasic.CompilerServices;
  8. namespace Dongke.IBOSS.PRD.Framework.Controls
  9. {
  10. [DesignerGenerated]
  11. [Browsable(false)]
  12. [ToolboxItem(false)]
  13. [Description("绘制标尺的控件")]
  14. [EditorBrowsable(EditorBrowsableState.Never)]
  15. public class Ruler : UserControl
  16. {
  17. private IContainer components;
  18. [AccessedThroughProperty("Label1")]
  19. private Label _Label1;
  20. private bool myDirection;
  21. private float myx;
  22. private float myy;
  23. private Color myTextColor;
  24. private Color myLineColor;
  25. private int MaxWidth;
  26. private bool myIsUserCentimeter;
  27. private int mystep;
  28. internal virtual Label Label1
  29. {
  30. get
  31. {
  32. return _Label1;
  33. }
  34. [MethodImpl(MethodImplOptions.Synchronized)]
  35. set
  36. {
  37. _Label1 = value;
  38. }
  39. }
  40. public bool IsUserCentimeter
  41. {
  42. get
  43. {
  44. return myIsUserCentimeter;
  45. }
  46. set
  47. {
  48. myIsUserCentimeter = value;
  49. ReDrawLable();
  50. }
  51. }
  52. public bool Direction
  53. {
  54. get
  55. {
  56. return myDirection;
  57. }
  58. set
  59. {
  60. myDirection = value;
  61. if (value)
  62. {
  63. Label1.TextAlign = ContentAlignment.TopCenter;
  64. }
  65. else
  66. {
  67. Label1.TextAlign = ContentAlignment.MiddleLeft;
  68. }
  69. Refresh();
  70. }
  71. }
  72. public float GetX => myx;
  73. public float GetY => myy;
  74. public Color LineColor
  75. {
  76. get
  77. {
  78. return myLineColor;
  79. }
  80. set
  81. {
  82. myLineColor = value;
  83. Refresh();
  84. }
  85. }
  86. public Color TextColor
  87. {
  88. get
  89. {
  90. return myTextColor;
  91. }
  92. set
  93. {
  94. myTextColor = value;
  95. Refresh();
  96. }
  97. }
  98. [DebuggerNonUserCode]
  99. protected override void Dispose(bool disposing)
  100. {
  101. try
  102. {
  103. if (disposing && components != null)
  104. {
  105. components.Dispose();
  106. }
  107. }
  108. finally
  109. {
  110. base.Dispose(disposing);
  111. }
  112. }
  113. [System.Diagnostics.DebuggerStepThrough]
  114. private void InitializeComponent()
  115. {
  116. this.Label1 = new System.Windows.Forms.Label();
  117. base.SuspendLayout();
  118. this.Label1.BackColor = System.Drawing.Color.FromArgb(192, 255, 192);
  119. this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  120. this.Label1.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 134);
  121. this.Label1.ForeColor = System.Drawing.Color.FromArgb(0, 0, 192);
  122. System.Windows.Forms.Control label = this.Label1;
  123. System.Drawing.Point location = new System.Drawing.Point(64, -2);
  124. label.Location = location;
  125. this.Label1.Name = "Label1";
  126. System.Windows.Forms.Control label2 = this.Label1;
  127. System.Drawing.Size size = new System.Drawing.Size(100, 23);
  128. label2.Size = size;
  129. this.Label1.TabIndex = 1;
  130. this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  131. this.Label1.Visible = false;
  132. System.Drawing.SizeF sizeF = new System.Drawing.SizeF(6f, 12f);
  133. base.AutoScaleDimensions = sizeF;
  134. base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  135. this.BackColor = System.Drawing.Color.White;
  136. base.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  137. base.Controls.Add(this.Label1);
  138. this.Cursor = System.Windows.Forms.Cursors.Default;
  139. this.DoubleBuffered = true;
  140. base.Name = "Ruler";
  141. size = new System.Drawing.Size(263, 31);
  142. base.Size = size;
  143. base.ResumeLayout(false);
  144. }
  145. public float ConvertToMM(float tt, bool isX)
  146. {
  147. if (isX)
  148. {
  149. return (float)((double)(tt / myx) * 2.54 * 10.0);
  150. }
  151. return (float)((double)(tt / myy) * 2.54 * 10.0);
  152. }
  153. public float ConvertToPoint(float tt, bool isx)
  154. {
  155. float num = ConvertToMM(tt, isx);
  156. return (float)((double)(num * 72f) / 25.4);
  157. }
  158. public float ConvertToPointFromMM(float tt)
  159. {
  160. return (float)((double)(tt * 72f) / 25.4);
  161. }
  162. public float ConvertFromMM(float tt, bool isx)
  163. {
  164. if (isx)
  165. {
  166. return (float)((double)(tt * myx) / 2.54 / 10.0);
  167. }
  168. return (float)((double)(tt * myy) / 2.54 / 10.0);
  169. }
  170. private void Ruler_Load(object sender, EventArgs e)
  171. {
  172. if (base.Controls.Count >= 5)
  173. {
  174. ReDrawLable();
  175. }
  176. }
  177. private void ReDrawLable()
  178. {
  179. checked
  180. {
  181. try
  182. {
  183. if (IsUserCentimeter)
  184. {
  185. int num = 0;
  186. int num2 = (int)Math.Round((double)MaxWidth / 10.0);
  187. for (int i = num; i <= num2; i++)
  188. {
  189. Label label = (Label)base.Controls["t" + Conversions.ToString(i)];
  190. label.Text = Conversions.ToString(i);
  191. }
  192. }
  193. else
  194. {
  195. int num3 = 0;
  196. int num4 = (int)Math.Round((double)MaxWidth / 10.0);
  197. for (int j = num3; j <= num4; j++)
  198. {
  199. Label label2 = (Label)base.Controls["t" + Conversions.ToString(j)];
  200. label2.Text = Conversions.ToString(j * 10);
  201. }
  202. }
  203. MyRefresh();
  204. }
  205. catch (Exception projectError)
  206. {
  207. ProjectData.SetProjectError(projectError);
  208. ProjectData.ClearProjectError();
  209. }
  210. }
  211. }
  212. private void Ruler_Resize(object sender, EventArgs e)
  213. {
  214. checked
  215. {
  216. if (Direction)
  217. {
  218. Label1.Height = (int)Math.Round((double)base.Height / 3.0 * 1.5);
  219. Label1.Top = base.Height - Label1.Height;
  220. }
  221. else
  222. {
  223. Label1.Width = (int)Math.Round((double)base.Width / 3.0 * 1.5);
  224. Label1.Left = base.Width - Label1.Width;
  225. }
  226. if (base.Controls.Count >= 5)
  227. {
  228. MyRefresh();
  229. }
  230. }
  231. }
  232. public void MyRefresh()
  233. {
  234. int num = 0;
  235. Label label = null;
  236. base.Visible = false;
  237. num = 0;
  238. Label label2 = null;
  239. label = null;
  240. checked
  241. {
  242. try
  243. {
  244. if (myDirection)
  245. {
  246. label = (Label)base.Controls["t0"];
  247. label.Left = (int)Math.Round(ConvertFromMM(num, isx: true));
  248. label.Top = 0;
  249. label.ForeColor = TextColor;
  250. label.Visible = true;
  251. label.BringToFront();
  252. label2 = (Label)base.Controls["l0"];
  253. label2.SetBounds(0, 0, 1, base.Height);
  254. label2.Visible = true;
  255. label2.BringToFront();
  256. int num2 = mystep;
  257. int maxWidth = MaxWidth;
  258. int num3 = mystep;
  259. for (num = num2; ((num3 >> 31) ^ num) <= ((num3 >> 31) ^ maxWidth); num += num3)
  260. {
  261. label2 = (Label)base.Controls["l" + Conversions.ToString(num)];
  262. label2.Width = 1;
  263. if (unchecked(num % 10) == 0)
  264. {
  265. label2.SetBounds((int)Math.Round(ConvertFromMM(num, isx: true)), 0, 1, base.Height);
  266. label = (Label)base.Controls["t" + Conversions.ToString((double)num / 10.0)];
  267. label.Left = (int)Math.Round(ConvertFromMM(num, isx: true));
  268. label.Top = 0;
  269. label.ForeColor = TextColor;
  270. try
  271. {
  272. label.Visible = true;
  273. }
  274. catch (Exception projectError)
  275. {
  276. ProjectData.SetProjectError(projectError);
  277. ProjectData.ClearProjectError();
  278. }
  279. label.BringToFront();
  280. }
  281. else if (unchecked(num % 5) == 0)
  282. {
  283. label2.SetBounds((int)Math.Round(ConvertFromMM(num, isx: true)), (int)Math.Round((float)base.Height - (float)((double)base.Height / 3.0 * 2.0)), 1, (int)Math.Round((double)base.Height / 3.0 * 2.0));
  284. }
  285. else
  286. {
  287. label2.SetBounds((int)Math.Round(ConvertFromMM(num, isx: true)), (int)Math.Round((float)base.Height - (float)((double)base.Height / 3.0)), 1, (int)Math.Round((double)base.Height / 3.0));
  288. }
  289. label2.Visible = true;
  290. label2.BringToFront();
  291. }
  292. }
  293. else
  294. {
  295. label = (Label)base.Controls["t0"];
  296. label.Top = (int)Math.Round(ConvertFromMM(num, isx: false));
  297. label.Left = 0;
  298. label.ForeColor = TextColor;
  299. label.Visible = true;
  300. label.BringToFront();
  301. label2 = (Label)base.Controls["l0"];
  302. label2.BringToFront();
  303. label2.SetBounds(0, 0, base.Width, 1);
  304. label2.Visible = true;
  305. int num4 = mystep;
  306. int maxWidth2 = MaxWidth;
  307. int num5 = mystep;
  308. for (num = num4; ((num5 >> 31) ^ num) <= ((num5 >> 31) ^ maxWidth2); num += num5)
  309. {
  310. label2 = (Label)base.Controls["l" + Conversions.ToString(num)];
  311. if (unchecked(num % 10) == 0)
  312. {
  313. label2.SetBounds(0, (int)Math.Round(ConvertFromMM(num, isx: true)), base.Width, 1);
  314. label = (Label)base.Controls["t" + Conversions.ToString((double)num / 10.0)];
  315. label.Top = (int)Math.Round(ConvertFromMM(num, isx: false));
  316. label.Left = 0;
  317. label.ForeColor = TextColor;
  318. label.Visible = true;
  319. label.BringToFront();
  320. }
  321. else if (unchecked(num % 5) == 0)
  322. {
  323. label2.SetBounds((int)Math.Round((double)base.Width - (double)base.Width / 3.0 * 2.0), (int)Math.Round(ConvertFromMM(num, isx: true)), (int)Math.Round((double)base.Width / 3.0 * 2.0), 1);
  324. }
  325. else
  326. {
  327. label2.SetBounds((int)Math.Round((double)base.Width - (double)base.Width / 3.0), (int)Math.Round(ConvertFromMM(num, isx: true)), (int)Math.Round((double)base.Width / 3.0), 1);
  328. }
  329. label2.Visible = true;
  330. label2.BringToFront();
  331. }
  332. }
  333. }
  334. catch (Exception projectError2)
  335. {
  336. ProjectData.SetProjectError(projectError2);
  337. ProjectData.ClearProjectError();
  338. }
  339. base.Visible = true;
  340. }
  341. }
  342. public void SetLableVisible(bool isvisible)
  343. {
  344. Label1.Visible = isvisible;
  345. }
  346. public void SetLabelPos(float xory, int widthorheight, bool iswidth, float baseheight)
  347. {
  348. checked
  349. {
  350. if (Direction)
  351. {
  352. Label1.SetBounds((int)Math.Round(xory), Label1.Top, widthorheight, Label1.Height);
  353. }
  354. else
  355. {
  356. Label1.SetBounds(Label1.Left, (int)Math.Round(baseheight + xory), Label1.Width, widthorheight);
  357. }
  358. try
  359. {
  360. Label1.Refresh();
  361. }
  362. catch (Exception projectError)
  363. {
  364. ProjectData.SetProjectError(projectError);
  365. ProjectData.ClearProjectError();
  366. }
  367. }
  368. }
  369. public Ruler()
  370. {
  371. base.Load += Ruler_Load;
  372. base.Resize += Ruler_Resize;
  373. myDirection = true;
  374. myTextColor = Color.Red;
  375. myLineColor = Color.Blue;
  376. MaxWidth = 500;
  377. myIsUserCentimeter = true;
  378. mystep = 10;
  379. InitializeComponent();
  380. SetStyle(ControlStyles.UserPaint, value: true);
  381. SetStyle(ControlStyles.AllPaintingInWmPaint, value: true);
  382. SetStyle(ControlStyles.OptimizedDoubleBuffer, value: true);
  383. UpdateStyles();
  384. myx = CreateGraphics().DpiX;
  385. myy = CreateGraphics().DpiY;
  386. Label1.SendToBack();
  387. int num = 0;
  388. int maxWidth = MaxWidth;
  389. int num2 = mystep;
  390. checked
  391. {
  392. for (int i = num; ((num2 >> 31) ^ i) <= ((num2 >> 31) ^ maxWidth); i += num2)
  393. {
  394. Label label = new Label();
  395. label.Visible = false;
  396. label.SetBounds(0, 10, 1, 1);
  397. label.Text = "";
  398. label.Name = "l" + Conversions.ToString(i);
  399. label.BorderStyle = BorderStyle.FixedSingle;
  400. base.Controls.Add(label);
  401. label.BringToFront();
  402. }
  403. int num3 = 0;
  404. int num4 = (int)Math.Round((double)MaxWidth / 10.0);
  405. for (int i = num3; i <= num4; i++)
  406. {
  407. Label value = new Label
  408. {
  409. Visible = false,
  410. Left = 0,
  411. AutoSize = true,
  412. Text = Conversions.ToString(i),
  413. Name = "t" + Conversions.ToString(i),
  414. BorderStyle = BorderStyle.None
  415. };
  416. base.Controls.Add(value);
  417. }
  418. try
  419. {
  420. MyRefresh();
  421. }
  422. catch (Exception projectError)
  423. {
  424. ProjectData.SetProjectError(projectError);
  425. ProjectData.ClearProjectError();
  426. }
  427. }
  428. }
  429. }
  430. }