2010-07-03 3 views
0

나는 직사각형을 그렸다. 화면의 가로 스크롤 막대는 undrenath입니다. 이제 사각형을 확대하고 싶습니다. 확대 된 사각형의 높이가 증가하면 위치가 위로 이동하고 가로 스크롤 막대가 위로 이동합니다. 이 작업을 수행하는 방법?사각형을 확대/축소하는 방법은 무엇입니까?

rect = new Rectangle(rect.Location.X, this.Height - rect.Height,rect.Width, Convert.ToInt32(rect.Size.Height * zoom)); 
g.FillRectangle(brush, rect); 

이 사각형가 이동하지만 높이가 증가하지 않는 것입니다 사각형의 위치를 ​​작동 : 나는이 코드 조각을 쓰고 있어요. 도움!

답변

1

직사각형의 중심 주위로 직사각형의 크기를 조절하려면 직사각형의 너비와 높이를 늘리고 위치에서 증가분의 절반을 빼야합니다.

이 테스트되지 않지만, 당신에게 일반적인 생각

double newHeight = oldHeight * scale; 
double deltaY = (newHeight - oldHeight) * 0.5; 

rect = new Rectangle(
    rect.Location.X, (int)(rect.Location.Y - deltaY), 
    rect.Width, (int)newHeight); 

아마도 더 나은 대안이 Graphics.ScaleTransform를 사용하여보고하는 것를 제공해야합니다.

0

그냥 폼에 txtZoom을 추가

using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      this.txtZoom.Text = "1"; 
      this.txtZoom.KeyDown += new KeyEventHandler(txtZoom_KeyDown); 
      this.txtZoom_KeyDown(txtZoom, new KeyEventArgs(Keys.Enter)); 
     } 

     void txtZoom_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyData == Keys.Enter) 
      { 
       this.Zoom = int.Parse(txtZoom.Text); 
       this.Invalidate(); 
      } 
     } 

     public int Zoom { get; set; } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      GraphicsPath path = new GraphicsPath(); 
      path.AddRectangle(new Rectangle(10, 10, 100, 100)); 

      Matrix m = new Matrix(); 
      m.Scale(Zoom, Zoom); 

      path.Transform(m); 
      this.AutoScrollMinSize = Size.Round(path.GetBounds().Size); 

      e.Graphics.FillPath(Brushes.Black, path); 
     } 
    } 
} 
관련 문제