运行时通过拖拽动态改变控件的大小

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  WindowsApplication11
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int oldx;
InBlock.gif        
private int oldy;
InBlock.gif
InBlock.gif        
enum mPosition
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            left,
InBlock.gif            right,
InBlock.gif            top,
InBlock.gif            bottom
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private mPosition adjust;
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void button1_MouseDown(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oldx 
= e.X;
InBlock.gif            oldy 
= e.Y;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_MouseMove(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Rectangle rectLeft 
= new Rectangle(022, button1.Height - 4);
InBlock.gif            Rectangle rectRight 
= new Rectangle(button1.Width - 222, button1.Width);
InBlock.gif            Rectangle rectTop 
= new Rectangle(20, button1.Width - 42);
InBlock.gif            Rectangle rectBottom 
= new Rectangle(2, button1.Height - 2, button1.Width - 42);
InBlock.gif
InBlock.gif            
if (rectLeft.Contains(e.X, e.Y))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                adjust 
= mPosition.left;
InBlock.gif                button1.Cursor 
= Cursors.SizeWE;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (rectRight.Contains(e.X, e.Y))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                adjust 
= mPosition.right;
InBlock.gif                button1.Cursor 
= Cursors.SizeWE;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (rectTop.Contains(e.X, e.Y))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                adjust 
= mPosition.top;
InBlock.gif                button1.Cursor 
= Cursors.SizeNS;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (rectBottom.Contains(e.X, e.Y))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                adjust 
= mPosition.bottom;
InBlock.gif                button1.Cursor 
= Cursors.SizeNS;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                button1.Cursor 
= Cursors.Default;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_MouseUp(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (e.Button == System.Windows.Forms.MouseButtons.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int dx = e.X - oldx;
InBlock.gif                
int dy = e.Y - oldy;
InBlock.gif                
switch (adjust)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case mPosition.left:
InBlock.gif                        button1.Left 
+= dx;
InBlock.gif                        button1.Width 
-= dy;
InBlock.gif                        
break;
InBlock.gif                    
case mPosition.right:
InBlock.gif                        button1.Width 
+= dx;
InBlock.gif                        
break;
InBlock.gif                    
case mPosition.top:
InBlock.gif                        button1.Top 
+= dy;
InBlock.gif                        button1.Height 
-= dy;
InBlock.gif                        
break;
InBlock.gif                    
case mPosition.bottom:
InBlock.gif                        button1.Height 
+= dy;
InBlock.gif                        
break;
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值