2010-12-14 4 views
0

제목 줄에 설명되어있는대로.Override 된 OnPaint 메서드에서 ImageAlign.MiddleCenter를 얻는 방법

나는 .NET

내가 버튼의의 OnPaint 이벤트를 무시하고있는 버튼 컨트롤의 이미지 속성의 동작 ImageAlign.MiddleCenter을 달성하고자합니다. 사용 e.Graphics.DrawImage (Image, oDrawRectagle); 버튼 컨트롤 안에 이미지를 칠합니다. 그러나 이것은 항상 Button의 Lefthand쪽에 나타납니다. oDrawRectagle은 항상 ClientRectangle에 대한 0,0 좌표를 반환하기 때문에.

이미지가 항상 정렬되도록하고 싶습니다. ImageAlign.MiddleCenter. 나는 그것을 어떻게 성취합니까?

감사

protected override void OnPaint(PaintEventArgs e) 
      { 
       Graphics g = e.Graphics; 

       int iHeight; 
       int iWidth; 

       if (Image != null) 
       { 
        //newSize = MaintainAspectRatio(Image, ClientRectangle.Width, ClientRectangle.Height); 
        //iHeight = newSize.Height; 
        //iWidth = newSize.Width; 
        Rectangle ResizedRectangle = MaintainAspectRatio(Image,ClientRectangle); 
        iHeight = ResizedRectangle.Height; 
        iWidth = ResizedRectangle.Width; 

       } 
       else 
       { 
        iWidth = ClientRectangle.Width; 
        iHeight = ClientRectangle.Height; 
       } 

       g.FillRectangle(new SolidBrush(Color.FromArgb(213, 221, 224)), ClientRectangle); 

       // Draw border 
       Color oLeftTopColor = SystemColors.ControlLightLight; 
       Color oRightBottomColor = SystemColors.ActiveCaption; 
       Pen oLeftTopPen = new Pen(oLeftTopColor); 
       Pen oRightBottomPen = new Pen(oRightBottomColor); 
       // top line 
       g.DrawLine(oLeftTopPen, 0, 0, iWidth - 1, 0); 
       //g.DrawLine(new Pen(SystemColors.ControlLight), 1, 1, iWidth-2, 1); 
       // bottom line 
       g.DrawLine(oRightBottomPen, 0, iHeight, iWidth - 1, iHeight); 
       //g.DrawLine(new Pen(SystemColors.ControlDark), 1, iHeight-2, iWidth-2, iHeight-2); 
       // right line 
       g.DrawLine(oRightBottomPen, iWidth, 0, iWidth, iHeight - 1); 
       //g.DrawLine(new Pen(SystemColors.ControlDark), iWidth-2, 1, iWidth-2, iHeight-2); 
       // left line 
       g.DrawLine(oLeftTopPen, 0, 0, 0, iHeight - 1); 
       //g.DrawLine(new Pen(SystemColors.ControlLightLight), 1, 1, 1, iHeight-2); 

       // Draw image 
       if (Image != null) 
       { 
        //Rectangle oDrawRectagle = new Rectangle(
        // 8, 5, iWidth - 20, iHeight - 10); 
        Rectangle oDrawRectagle = new Rectangle(
         0, 0, iWidth, iHeight); 
        if (Enabled == false) 
        { 
         e.Graphics.DrawImage(Image, oDrawRectagle, 
          0, 0, Image.Width, Image.Height, 
          GraphicsUnit.Pixel); 
        } 
        else 
        { 
         e.Graphics.DrawImage(Image,oDrawRectagle); 

        } 
       } 

       // Draw text 
       if (Text != null) 
       { 
        int iTextTop = 5; 
        int iTextLeft = 5; 
        int iMaxTextWidth = iWidth - 10; 
        int iMaxTextHeight = iHeight - 10; 
       } 
      } 

답변

4

그냥 수동으로 버튼의 크기와 이미지 크기를 사용하여 중앙으로 위치를 변경할 수 있습니다.

oDrawRectangle = new Rectangle(this.Width/2 - iWidth/2, this.Height/2 - iHeight/2, iWidth, iHeight); 
관련 문제