2010-12-14 5 views
0

Comme l'explique la ligne d'objet.Comment réaliser ImageAlign.MiddleCenter dans la méthode OnPaint surchargée

Je veux obtenir le comportement ImageAlign.MiddleCenter dans la propriété Image d'un bouton contrôle dans .NET

je suis PRÉPONDÉRANTS événement OnPaint du bouton. J'utilise e.Graphics.DrawImage (Image, oDrawRectagle); pour peindre l'image à l'intérieur du contrôle Button. Cependant, cela apparaît toujours sur le côté gauche du bouton. Étant donné que oDrawRectagle renvoie toujours les coordonnées 0,0 pour le ClientRectangle.

Je souhaite que l'image soit toujours alignée ImageAlign.MiddleCenter. Comment puis-je y arriver?

Merci

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

Répondre

4

Vous pouvez simplement changer manuellement la position du centre en utilisant la taille du bouton et la taille de l'image.

oDrawRectangle = new Rectangle(this.Width/2 - iWidth/2, this.Height/2 - iHeight/2, iWidth, iHeight); 
Questions connexes