![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Point p = new Point();
public Form1()
{
InitializeComponent();
this.pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
}
#region capture picturebox
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.pictureBox1.Capture)
{
this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - p.X);
this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - p.Y);
p.X = Cursor.Position.X;
p.Y = Cursor.Position.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this.pictureBox1.Capture = false;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.pictureBox1.Capture = true;
p.X = Cursor.Position.X;
p.Y = Cursor.Position.Y;
}
#endregion
另一种方法
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void pictureBox1_MouseDown( object sender, MouseEventArgs e ) {
PictureBox1MouseDown( sender, e );
//this.pictureBox1.BringToFront();
}
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 );
this.Validate();
}
重画UserControl的边框代码,暂时有一个问题没搞定,就是这个边框是画上去,没有实际的范围"约束",我指子控件可以放到"边"上边,很不爽,继续研究ing...
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private Pen borderPen;
public TstringBox() {
InitializeComponent();
this.SetStyle( ControlStyles.AllPaintingInWmPaint, true );
this.SetStyle( ControlStyles.OptimizedDoubleBuffer, true );
this.SetStyle( ControlStyles.ResizeRedraw, true );
this.SetStyle( ControlStyles.UserPaint, true );
this.SetStyle( ControlStyles.SupportsTransparentBackColor, true );
borderPen = new Pen( Color.FromArgb( 179, 179, 179 ) );
}
protected override void OnPaint( PaintEventArgs e ) {
e.Graphics.Clear( Color.White );
e.Graphics.DrawRectangle( borderPen, new Rectangle( this.ClientRectangle.Location, new Size( this.ClientRectangle.Size.Width - 3,this.ClientRectangle.Size.Height - 3 ) ) );
base.OnPaint( e );
}