拖动PictureBox 代码片断

本文介绍了两种实现PictureBox拖动的方法,并提供了一段用于自定义UserControl边框绘制的代码。第一种方法通过监听PictureBox的鼠标事件来实现拖动功能;第二种方法利用了Windows API实现更为灵活的拖动效果。

 

ContractedBlock.gifExpandedBlockStart.gifCode
        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

 

另一种方法

 

ContractedBlock.gifExpandedBlockStart.gifCode
        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...

 

ContractedBlock.gifExpandedBlockStart.gifCode
        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( 179179179 ) );
        }

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值