using System;
using System.Drawing;
using System.Windows.Forms;
namespace PaintDotNet.CustomControl
{
//internal sealed class
public partial class PicturePreviewAdjustControl : BaseUserControl// Control//
{
System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量
bool isSelected = false;
public bool isShown = false;
///
/// 是否使用原图取色
///
private bool item3Checked;
///
/// 原图取色-鼠标点击事件
///
public event MouseEventHandler item3MouseDown;
private void InitializeLanguageText()
{
this.box_Picture.Text = PdnResources.GetString("Menu.Preview.text");
}
///
/// 图像处理的预览Control
/// 包括放大、缩小、选择、移动
///
public PicturePreviewAdjustControl()
{
InitializeComponent();
if (!DesignMode)
{
InitializeLanguageText();
this.ZoomInButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomInIcon.png").Reference;
this.ZoomOutButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomOutIcon.png").Reference;
this.MoveSelectionButton.Image = PdnResources.GetImageResource("Icons.MoveSelectionToolIcon.png").Reference;
this.PanButton.Image = PdnResources.GetImageResource("Icons.PanToolIcon.png").Reference;
}
}
//放大
private void ZoomInButton_Click(object sender, EventArgs e)
{
Do.TryBool(ZoomInImpl);
}
//放大
private void ZoomInImpl()
{
//int pictureBox1CenterX = pictureBox1.Location.X + pictureBox1.Width / 2;
//int pictureBox1CenterY = pictureBox1.Location.Y + pictureBox1.Height / 2;
double eDelta = pictureBox1.Height * 0.25;
double scale = 1;
if (pictureBox1.Height > 0)
{
scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
}
pictureBox1.Width += (int)(eDelta * scale * 2);
pictureBox1.Height += (int)(eDelta * 2);
pictureBox1.Left -= (int)(eDelta * scale * 1);
pictureBox1.Top -= (int)(eDelta * 1);
}
//缩小
private void ZoomOutButton_Click(object sender, EventArgs e)
{
double eDelta = pictureBox1.Height * 0.16;// 0.25;
double scale = 1;
if (pictureBox1.Height > 0)
{
scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
}
pictureBox1.Width -= (int)(eDelta * scale * 2);
pictureBox1.Height -= (int)(eDelta * 2);
pictureBox1.Left += (int)(eDelta * scale * 1);
pictureBox1.Top += (int)(eDelta * 1);
}
//选择
private void MoveSelectionButton_Click(object sender, EventArgs e)
{
if (this.item3Checked)
{
pictureBox1.Cursor = Cursors.Cross;
}
else
{
pictureBox1.Cursor = Cursors.Default;
}
}
//移动
private void PanButton_Click(object sender, EventArgs e)
{
pictureBox1.Cursor = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursor.cur"));
//Cursors.Hand;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
double scale = 1;
if (pictureBox1.Height > 0)
{
scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
}
if (pictureBox1.Width + (int)(e.Delta * scale) > 0 && pictureBox1.Height + e.Delta > 0)
{
pictureBox1.Width += (int)(e.Delta * scale);
pictureBox1.Height += e.Delta;
pictureBox1.Left -= (int)(e.Delta * scale * 0.5);
pictureBox1.Top -= (int)(e.Delta * 0.5);
}
}
//在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Hand)
{
mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型
mouseDownPoint.Y = Cursor.Position.Y;
isSelected = true;
}
else if (this.item3Checked && e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Cross)
{
//获取当前位置坐标
Rectangle rc = this.pictureBox1.ClientRectangle;
int width = (int)this.pictureBox1.Width;// (this.compositionSurface.Width * this.scaleFactor.Ratio);
int height = (int)this.pictureBox1.Height;// (this.compositionSurface.Height * this.scaleFactor.Ratio);
int x = 0;
int y = 0;
double radio = 1;
//垂直方向有空白
if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) > (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
{
y = (int)((height - width * this.pictureBox1.Image.Height / (double)this.pictureBox1.Image.Width) / 2.0);
radio = this.pictureBox1.Image.Width / (double)this.pictureBox1.Width;
}
else if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) < (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
{
x = (int)((width - height * this.pictureBox1.Image.Width / (double)this.pictureBox1.Image.Height) / 2.0);
radio = this.pictureBox1.Image.Height / (double)this.pictureBox1.Height;
}
//int x = (rc.Width < width) ? this.pictureBox1.Location.X/*this.panel.AutoScrollPosition.X + offsetHalfW*/ : (rc.Width - width) / 2;
//int y = (rc.Height < height) ? this.pictureBox1.Location.Y/*this.panel.AutoScrollPosition.Y + offsetHalfH*/ : (rc.Height - height) / 2;
Point point = e.Location;
point.X -= x;
point.Y -= y;
Point newPoint = new Point((int)((long)point.X * radio), (int)((long)point.Y * radio));
this.item3MouseDown?.Invoke(sender, new MouseEventArgs(e.Button, e.Clicks, newPoint.X, newPoint.Y, e.Delta));
}
}
public void selectColorMode(bool selectColor)
{
this.item3Checked = selectColor;
if (selectColor)
{
pictureBox1.Cursor = Cursors.Cross;
this.MoveSelectionButton.Focus();
}
else
{
pictureBox1.Cursor = Cursors.Default;
}
}
//在MouseUp处获知鼠标是否松开,终止拖动操作;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isSelected = false;
}
private bool IsMouseInPanel()
{
if (this.panel_Picture.Left + this.box_Picture.Left < PointToClient(Cursor.Position).X
&& PointToClient(Cursor.Position).X < this.panel_Picture.Left + this.box_Picture.Left
+ this.panel_Picture.Width && this.panel_Picture.Top + this.box_Picture.Top
< PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y
< this.panel_Picture.Top + this.box_Picture.Top + this.panel_Picture.Height)
{
return true;
}
else
{
return false;
}
}
//图片平移,在MouseMove处添加拖动函数操作
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内
{
this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X);
this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void picturePreviewAdjustControl_Resize(object sender, EventArgs e)
{
if (isShown)
{
this.pictureBox1.Left = 0;
this.pictureBox1.Top = 0;
this.pictureBox1.Width = panel_Picture.Width;
this.pictureBox1.Height = panel_Picture.Height;
}
}
private void box_Picture_Enter(object sender, EventArgs e)
{
}
}
}