C#中級 コーディング例

G1)滑らかサインウェーブ
G2)単一色ペイント
   (スタック方式)
G3)ギザトゲ楕円
G4)ナミモク四角

C1)マウス移動可能パネル

この講座は中級者用です。
「using」「Form」等は、省略しています。

















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);

       }
   }