banboshi_V1/halftoneproject-master/AssistClient/UI/FrmScannerShow.cs
2023-10-31 13:19:29 +08:00

140 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssistClient.UI
{
public partial class FrmScannerShow : Form
{
public Action reDraw;
private Size size;
private double ratio = 1; // 图片的起始显示比例
private double ratioStep = 0.1;
private Size pic_size;
private int xPos;
private int yPos;
public IntPtr picHwnd
{
get { return this.pictureBox1.Handle; }
}
public FrmScannerShow(Size _size)
{
InitializeComponent();
this.Width = Screen.PrimaryScreen.WorkingArea.Width/3;
this.Location=new Point(0,0);
size = _size;
this.ClientSize = new Size(this.ClientSize.Width,
(int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width)));
this.pictureBox1.Size = this.ClientSize;
this.pictureBox1.Location=new Point(0,0);
}
private void FrmScannerShow_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.MouseDown += pictureBox1_MouseDown;
this.ActiveControl = this.pictureBox1; // 设置焦点
pic_size = this.pictureBox1.Size;
}
public void updateBmp(Bitmap bmp)
{
this.pictureBox1.Image = bmp;
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
//Point pictureBoxPoint = this.PointToClient(Cursor.Position);
//this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
// $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
// $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
if (e.Delta > 0)
{
ratio += ratioStep;
if (ratio > 100) // 放大上限
ratio = 100;
else
{
int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
this.changePictureBoxSize(new Point(x, y), ratio);
}
}
else if (ratio - ratioStep >= 1)
{
ratio -= ratioStep;
//if (ratio < 0.1) // 放大下限
// ratio = 0.1;
//else
int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
this.changePictureBoxSize(new Point(x, y), ratio);
}
}
private void changePictureBoxSize(Point location, double ratio)
{
var picSize = pictureBox1.Size;
picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
pictureBox1.Size = picSize;
if (location.X > 0) location.X = 0;
if (location.Y > 0) location.Y = 0;
if (picSize.Width + location.X < this.ClientSize.Width) location.X = -(picSize.Width - this.ClientSize.Width);
if (picSize.Height + location.Y < this.ClientSize.Height) location.Y = -(picSize.Height - this.ClientSize.Height);
//Point location = new Point();
//location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
//location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
this.pictureBox1.Location = location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
try
{
// 鼠标按下拖拽图片
if (e.Button == MouseButtons.Left)
{
// 限制拖拽出框
if (pictureBox1.Width > this.ClientSize.Width || pictureBox1.Height > this.ClientSize.Height)
{
int moveX = e.X - xPos;
int moveY = e.Y - yPos;
if ((pictureBox1.Left + moveX) <= 0
&& (pictureBox1.Top + moveY) <= 0
&& (pictureBox1.Right + moveX) >= this.ClientSize.Width
&& (pictureBox1.Bottom + moveY) >= this.ClientSize.Height)
{
pictureBox1.Left += moveX;//设置x坐标.
pictureBox1.Top += moveY;//设置y坐标.
}
}
}
}
catch (Exception dd)
{
MessageBox.Show(dd.Message);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
xPos = e.X;//当前x坐标.
yPos = e.Y;//当前y坐标.
}
}
}