logo
VitNX3 Documentation

# VitNX_Button.OnPaint Method

Namespace: VitNX.UI.ControlsV1.Controls
Assembly: VitNX3 (in VitNX3.dll)
Raises the OnPaint event.
VB
C#
F#
C++
JScript
Copy

    protected override void OnPaint(
       PaintEventArgs e
    )

    This language is not supported or no code example is available.

#### Parameters

e
  PaintEventArgs

Source code
VB
C#
F#
C++
JScript
Copy

    protected override void OnPaint(PaintEventArgs e)
     {
         var g = e.Graphics;
         var rect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
         var textColor = Colors.LightText;
         var borderColor = Colors.GreySelection;
         var fillColor = _isDefault ? Colors.VitNXBlueBackground : Colors.LightBackground;
         if (Enabled)
         {
             if (ButtonStyle == VitNX_ButtonStyle.Normal)
             {
                 if (Focused && TabStop) { borderColor = Colors.BlueHighlight; }
                 switch (ButtonState)
                 {
                     case VitNX_ControlState.Hover:
                         fillColor = _isDefault ? Colors.BlueBackground : Colors.LighterBackground;
                         break;

                     case VitNX_ControlState.Pressed:
                         fillColor = _isDefault ? Colors.VitNXBackground : Colors.VitNXBackground;
                         break;
                 }
             }
             else if (ButtonStyle == VitNX_ButtonStyle.Flat)
             {
                 switch (ButtonState)
                 {
                     case VitNX_ControlState.Normal:
                         fillColor = Colors.GreyBackground;
                         break;

                     case VitNX_ControlState.Hover:
                         fillColor = Colors.MediumBackground;
                         break;

                     case VitNX_ControlState.Pressed:
                         fillColor = Colors.VitNXBackground;
                         break;
                 }
             }
         }
         else
         {
             textColor = Colors.DisabledText;
             fillColor = Colors.VitNXGreySelection;
         }
         using (var b = new SolidBrush(fillColor)) { g.FillRectangle(b, rect); }
         if (ButtonStyle == VitNX_ButtonStyle.Normal)
         {
             using (var p = new Pen(borderColor, 1))
             {
                 var modRect = new Rectangle(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                 g.DrawRectangle(p, modRect);
             }
         }
         var textOffsetX = 0;
         var textOffsetY = 0;
         if (Image != null)
         {
             var stringSize = g.MeasureString(Text, Font, rect.Size);
             var x = (ClientSize.Width / 2) - (Image.Size.Width / 2);
             var y = (ClientSize.Height / 2) - (Image.Size.Height / 2);
             switch (TextImageRelation)
             {
                 case TextImageRelation.ImageAboveText:
                     textOffsetY = (Image.Size.Height / 2) + (ImagePadding / 2);
                     y = y - ((int)(stringSize.Height / 2) + (ImagePadding / 2));
                     break;

                 case TextImageRelation.TextAboveImage:
                     textOffsetY = ((Image.Size.Height / 2) + (ImagePadding / 2)) * -1;
                     y = y + ((int)(stringSize.Height / 2) + (ImagePadding / 2));
                     break;

                 case TextImageRelation.ImageBeforeText:
                     textOffsetX = Image.Size.Width + (ImagePadding * 2);
                     x = ImagePadding;
                     break;

                 case TextImageRelation.TextBeforeImage:
                     x = x + (int)stringSize.Width;
                     break;
             }
             g.DrawImageUnscaled(Image, x, y);
         }
         using (var b = new SolidBrush(textColor))
         {
             var modRect = new Rectangle(rect.Left + textOffsetX + Padding.Left, rect.Top + textOffsetY + Padding.Top, rect.Width - Padding.Horizontal, rect.Height - Padding.Vertical);
             var stringFormat = new StringFormat
             {
                 LineAlignment = StringAlignment.Center,
                 Alignment = StringAlignment.Center,
                 Trimming = StringTrimming.EllipsisCharacter
             };
             g.DrawString(Text, Font, b, modRect, stringFormat);
         }
     }

    This language is not supported or no code example is available.

Applies to

#### .NET Framework

Supported in: 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

#### .NET Core

Supported in: 5.0+, 6.0+
See Also
VitNX_Button Class
VitNX.UI.ControlsV1.Controls Namespace