C#中,如何随意拖动PictureBox?

看到这个问题,我自然而然就联想到了以前的拖动无边框窗体的实现上,其实,不只是PictureBox,基本上所有的控件都可以被拖动。

实现,就是利用Windows的API函数:SendMessage 和 ReleaseCapture 即可了,代码很简单。

核心代码如下:

const uint WM_SYSCOMMAND = 0x0112;  

const uint SC_MOVE = 0xF010;

const uint HTCAPTION = 0x0002;



[DllImport("user32.dll",   EntryPoint="SendMessageA")]  

  private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);  

  [DllImport("user32.dll")]  

  private static extern int ReleaseCapture();   



void PictureBox1MouseDown(object sender, MouseEventArgs e)

{

    ReleaseCapture();

    SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

}

你可能感兴趣的:(C#)