C#中級 コーディング例
C1)マウス移動可能パネル
パネル上のラベルをクリックしてドラッグするとパネル全体をフォーム内マウス移動できる
public partial class Form1 : Form
{
//User32.dll使用
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
[DllImport("User32.dll", EntryPoint = "SendMessage")]
extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtrlParam);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool SetCapture(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public Form1()
{
InitializeComponent();
}
private void moveLabel_MouseDown(object sender, MouseEventArgs e)
{
SetCapture(panel.Handle);
ReleaseCapture();
SendMessage(panel.Handle, WM_SYSCOMMAND, SC_MOVE | 2, 0);
}
}