PicturePreviewAdjustControl.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.CustomControl
  5. {
  6. //internal sealed class
  7. public partial class PicturePreviewAdjustControl : BaseUserControl// Control//
  8. {
  9. System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量
  10. bool isSelected = false;
  11. public bool isShown = false;
  12. /// <summary>
  13. /// 是否使用原图取色
  14. /// </summary>
  15. private bool item3Checked;
  16. /// <summary>
  17. /// 原图取色-鼠标点击事件
  18. /// </summary>
  19. public event MouseEventHandler item3MouseDown;
  20. private void InitializeLanguageText()
  21. {
  22. this.box_Picture.Text = PdnResources.GetString("Menu.Preview.text");
  23. }
  24. /// <summary>
  25. /// 图像处理的预览Control
  26. /// 包括放大、缩小、选择、移动
  27. /// </summary>
  28. public PicturePreviewAdjustControl()
  29. {
  30. InitializeComponent();
  31. if (!DesignMode)
  32. {
  33. InitializeLanguageText();
  34. this.ZoomInButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomInIcon.png").Reference;
  35. this.ZoomOutButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomOutIcon.png").Reference;
  36. this.MoveSelectionButton.Image = PdnResources.GetImageResource("Icons.MoveSelectionToolIcon.png").Reference;
  37. this.PanButton.Image = PdnResources.GetImageResource("Icons.PanToolIcon.png").Reference;
  38. }
  39. }
  40. //放大
  41. private void ZoomInButton_Click(object sender, EventArgs e)
  42. {
  43. Do.TryBool(ZoomInImpl);
  44. }
  45. //放大
  46. private void ZoomInImpl()
  47. {
  48. //int pictureBox1CenterX = pictureBox1.Location.X + pictureBox1.Width / 2;
  49. //int pictureBox1CenterY = pictureBox1.Location.Y + pictureBox1.Height / 2;
  50. double eDelta = pictureBox1.Height * 0.25;
  51. double scale = 1;
  52. if (pictureBox1.Height > 0)
  53. {
  54. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  55. }
  56. pictureBox1.Width += (int)(eDelta * scale * 2);
  57. pictureBox1.Height += (int)(eDelta * 2);
  58. pictureBox1.Left -= (int)(eDelta * scale * 1);
  59. pictureBox1.Top -= (int)(eDelta * 1);
  60. }
  61. //缩小
  62. private void ZoomOutButton_Click(object sender, EventArgs e)
  63. {
  64. double eDelta = pictureBox1.Height * 0.16;// 0.25;
  65. double scale = 1;
  66. if (pictureBox1.Height > 0)
  67. {
  68. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  69. }
  70. pictureBox1.Width -= (int)(eDelta * scale * 2);
  71. pictureBox1.Height -= (int)(eDelta * 2);
  72. pictureBox1.Left += (int)(eDelta * scale * 1);
  73. pictureBox1.Top += (int)(eDelta * 1);
  74. }
  75. //选择
  76. private void MoveSelectionButton_Click(object sender, EventArgs e)
  77. {
  78. if (this.item3Checked)
  79. {
  80. pictureBox1.Cursor = Cursors.Cross;
  81. }
  82. else
  83. {
  84. pictureBox1.Cursor = Cursors.Default;
  85. }
  86. }
  87. //移动
  88. private void PanButton_Click(object sender, EventArgs e)
  89. {
  90. pictureBox1.Cursor = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursor.cur"));
  91. //Cursors.Hand;
  92. }
  93. private void pictureBox1_Click(object sender, EventArgs e)
  94. {
  95. }
  96. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  97. {
  98. double scale = 1;
  99. if (pictureBox1.Height > 0)
  100. {
  101. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  102. }
  103. if (pictureBox1.Width + (int)(e.Delta * scale) > 0 && pictureBox1.Height + e.Delta > 0)
  104. {
  105. pictureBox1.Width += (int)(e.Delta * scale);
  106. pictureBox1.Height += e.Delta;
  107. pictureBox1.Left -= (int)(e.Delta * scale * 0.5);
  108. pictureBox1.Top -= (int)(e.Delta * 0.5);
  109. }
  110. }
  111. //在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值;
  112. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  113. {
  114. if (e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Hand)
  115. {
  116. mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型
  117. mouseDownPoint.Y = Cursor.Position.Y;
  118. isSelected = true;
  119. }
  120. else if (this.item3Checked && e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Cross)
  121. {
  122. //获取当前位置坐标
  123. Rectangle rc = this.pictureBox1.ClientRectangle;
  124. int width = (int)this.pictureBox1.Width;// (this.compositionSurface.Width * this.scaleFactor.Ratio);
  125. int height = (int)this.pictureBox1.Height;// (this.compositionSurface.Height * this.scaleFactor.Ratio);
  126. int x = 0;
  127. int y = 0;
  128. double radio = 1;
  129. //垂直方向有空白
  130. if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) > (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
  131. {
  132. y = (int)((height - width * this.pictureBox1.Image.Height / (double)this.pictureBox1.Image.Width) / 2.0);
  133. radio = this.pictureBox1.Image.Width / (double)this.pictureBox1.Width;
  134. }
  135. else if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) < (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
  136. {
  137. x = (int)((width - height * this.pictureBox1.Image.Width / (double)this.pictureBox1.Image.Height) / 2.0);
  138. radio = this.pictureBox1.Image.Height / (double)this.pictureBox1.Height;
  139. }
  140. //int x = (rc.Width < width) ? this.pictureBox1.Location.X/*this.panel.AutoScrollPosition.X + offsetHalfW*/ : (rc.Width - width) / 2;
  141. //int y = (rc.Height < height) ? this.pictureBox1.Location.Y/*this.panel.AutoScrollPosition.Y + offsetHalfH*/ : (rc.Height - height) / 2;
  142. Point point = e.Location;
  143. point.X -= x;
  144. point.Y -= y;
  145. Point newPoint = new Point((int)((long)point.X * radio), (int)((long)point.Y * radio));
  146. this.item3MouseDown?.Invoke(sender, new MouseEventArgs(e.Button, e.Clicks, newPoint.X, newPoint.Y, e.Delta));
  147. }
  148. }
  149. public void selectColorMode(bool selectColor)
  150. {
  151. this.item3Checked = selectColor;
  152. if (selectColor)
  153. {
  154. pictureBox1.Cursor = Cursors.Cross;
  155. this.MoveSelectionButton.Focus();
  156. }
  157. else
  158. {
  159. pictureBox1.Cursor = Cursors.Default;
  160. }
  161. }
  162. //在MouseUp处获知鼠标是否松开,终止拖动操作;
  163. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  164. {
  165. isSelected = false;
  166. }
  167. private bool IsMouseInPanel()
  168. {
  169. if (this.panel_Picture.Left + this.box_Picture.Left < PointToClient(Cursor.Position).X
  170. && PointToClient(Cursor.Position).X < this.panel_Picture.Left + this.box_Picture.Left
  171. + this.panel_Picture.Width && this.panel_Picture.Top + this.box_Picture.Top
  172. < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y
  173. < this.panel_Picture.Top + this.box_Picture.Top + this.panel_Picture.Height)
  174. {
  175. return true;
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. }
  182. //图片平移,在MouseMove处添加拖动函数操作
  183. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  184. {
  185. if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内
  186. {
  187. this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X);
  188. this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y);
  189. mouseDownPoint.X = Cursor.Position.X;
  190. mouseDownPoint.Y = Cursor.Position.Y;
  191. }
  192. }
  193. private void picturePreviewAdjustControl_Resize(object sender, EventArgs e)
  194. {
  195. if (isShown)
  196. {
  197. this.pictureBox1.Left = 0;
  198. this.pictureBox1.Top = 0;
  199. this.pictureBox1.Width = panel_Picture.Width;
  200. this.pictureBox1.Height = panel_Picture.Height;
  201. }
  202. }
  203. private void box_Picture_Enter(object sender, EventArgs e)
  204. {
  205. }
  206. }
  207. }