
Code
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
另一种方法

Code
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...

Code
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 );
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架