Drag PictureBox

问题描述 投票:0回答:1

我想拖动一个PictureBox,并且已经成功做到了。但是我的应用程序执行起来不如Windows photo viewer顺利。我的意思是区别并不大,也没有任何区别,但值得注意。有什么我可以做的使它变得不那么混乱吗?这是我的简单代码:

int MOUSE_X = 0;
int MOUSE_Y = 0;

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    picBox.Image = Image.FromFile(@"D:\test_big.png");
    picBox.Width = 3300;
    picBox.Height = 5100;
}

private void picBox_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        MOUSE_X = e.X;
        MOUSE_Y = e.Y;
    }
}

private void picBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        picBox.Left = picBox.Left + (e.X - MOUSE_X);
        picBox.Top = picBox.Top + (e.Y - MOUSE_Y);
    }
}
c# forms optimization picturebox drag
1个回答
0
投票
测试您的代码会产生:

SOQ60819266A

而建议的代码:

using System.Runtime.InteropServices; //... private const int WM_SYSCOMMAND = 0x112; private const int MOUSE_MOVE = 0xF012; [DllImport("user32.dll")] private static extern IntPtr SendMessage( IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] private static extern int ReleaseCapture(IntPtr hWnd); private void picBox_MouseMove(object sender, MouseEventArgs e) { if (!DesignMode && e.Button == MouseButtons.Left) { ReleaseCapture(picBox.Handle); SendMessage(picBox.Handle, WM_SYSCOMMAND, (IntPtr)MOUSE_MOVE, IntPtr.Zero); } }

产品:

SOQ60819266B

请注意,如果我这么说,我也会使用背景图片使情况变得更糟。但是,如果没有背景图片,则很难检测到使用了哪个代码段。

© www.soinside.com 2019 - 2024. All rights reserved.