You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

102 lines
3.4 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShenTun.ImageCollection.Test
{
public partial class ImageMouseMoveForm : Form
{
private Panel panel;
private PictureBox pictureBox;
private HScrollBar hScrollBar;
private VScrollBar vScrollBar;
private Label positionLabel;
private bool isDragging = false;
private Point dragStartPoint;
public ImageMouseMoveForm()
{
this.Width = 800;
this.Height = 600;
this.Text = "Image Viewer with Scrollbars";
panel = new Panel();
panel.Dock = DockStyle.Fill;
panel.AutoScroll = true;
pictureBox = new PictureBox();
pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox.Image = Image.FromFile(@"d:\\log\\2407240149_0001.jpg"); // 替换为你的图片路径
pictureBox.MouseMove += PictureBox_MouseMove;
pictureBox.MouseDown += PictureBox_MouseDown;
pictureBox.MouseUp += PictureBox_MouseUp;
panel.Controls.Add(pictureBox);
hScrollBar = new HScrollBar();
hScrollBar.Dock = DockStyle.Bottom;
hScrollBar.Scroll += HScrollBar_Scroll;
vScrollBar = new VScrollBar();
vScrollBar.Dock = DockStyle.Right;
vScrollBar.Scroll += VScrollBar_Scroll;
positionLabel = new Label();
positionLabel.AutoSize = true;
positionLabel.Text = "Mouse Position: (0, 0)";
positionLabel.Dock = DockStyle.Top;
this.Controls.Add(positionLabel);
this.Controls.Add(panel);
this.Controls.Add(hScrollBar);
this.Controls.Add(vScrollBar);
pictureBox.MouseMove += PictureBox_MouseMove;
hScrollBar.Scroll += HScrollBar_Scroll;
vScrollBar.Scroll += VScrollBar_Scroll;
}
private void PictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
dragStartPoint = e.Location;
}
}
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBox.ClientRectangle.Contains(pictureBox.PointToClient(Cursor.Position)))
{
// 计算鼠标位置相对于图片的位置
Point pictureBoxLocation = pictureBox.PointToClient(Cursor.Position);
int relativeX = pictureBoxLocation.X - panel.AutoScrollPosition.X;
int relativeY = pictureBoxLocation.Y - panel.AutoScrollPosition.Y;
positionLabel.Text = $"Mouse Position: ({relativeX}, {relativeY})";
}
}
private void PictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void HScrollBar_Scroll(object sender, ScrollEventArgs e)
{
panel.AutoScrollPosition = new Point(-hScrollBar.Value, panel.AutoScrollPosition.Y);
}
private void VScrollBar_Scroll(object sender, ScrollEventArgs e)
{
panel.AutoScrollPosition = new Point(panel.AutoScrollPosition.X, -vScrollBar.Value);
}
}
}