非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

C#: フォーム内に付箋紙を作る方法

   

フォーム上に付箋紙を作る方法です。普通の付箋紙同様、マウスでドラッグして自由に移動できます。普通の付箋紙と違ってフォームの中で動きます。

何に使うか??ですが、面白いのでソースコードをシェアしておきます。

以下が実行例です。起動するとフォーム右上に付箋紙が現れています。

 

マウスでドラッグするとフォーム内を自由に動かすことができます。

[ad#top-1]

 

ソースコードはこちら。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class Test{
  static void Main(){
    Application.Run(new FormMain());
  }
}

public class FormMain : Form{
  private Panel pane;
  private Label label;

  const int WM_SYSCOMMAND = 0x0112;
  const int SC_MOVE = 0xF010;
  const int SC_SIZE = 0xF000;

  [DllImport("User32.dll", EntryPoint = "SendMessage")]
  extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  [DllImport("User32.dll")]
  public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
  [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 FormMain(){
    this.StartPosition = FormStartPosition.CenterScreen;
    this.Width = 800;
    this.Height = 650;
    this.Text = "Test";

    pane = new Panel(){
      Size = new Size(200, 150),
      BackColor = Color.Blue,
      Location = new Point(10, 10),
      Parent = this,
    };
    pane.MouseDown += new MouseEventHandler(this.Panel_MouseDown);

    label = new Label(){
      Dock = DockStyle.Fill,
      BackColor = Color.LightPink,
      BorderStyle = BorderStyle.FixedSingle,
      Parent=pane,
    };
    label.MouseDown += new MouseEventHandler(this.Panel_MouseDown);
  }

  private void Panel_MouseDown(object sender, MouseEventArgs e){
    SetCapture(pane.Handle);
    ReleaseCapture();
    SendMessage(pane.Handle, WM_SYSCOMMAND, SC_MOVE | 2, 0);
  }
}
 

 

[ad#ad-1]

スポンサーリンク

 - C#応用編