如何在重写的OnPaint方法中实现ImageAlign.MiddleCenter

发布于 2024-10-07 16:37:09 字数 3446 浏览 6 评论 0原文

正如主题行所解释的那样。

我想在 Button 的 Image 属性中实现行为 ImageAlign.MiddleCenter .NET 中的控件

我正在重写按钮的 OnPaint 事件。 我使用e.Graphics.DrawImage(Image,oDrawRectagle); 在 Button 控件内绘制图像。 然而,它总是出现在按钮的左侧。 由于 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;
                 }
            }

As the subject line explains it.

I want to achieve the behaviour ImageAlign.MiddleCenter in the Image property of a Button
control in .NET

i am overriding the Button's OnPaint event.
I use e.Graphics.DrawImage(Image,oDrawRectagle);
to paint the image inside the Button control.
However this always appears on the Lefthand side of the Button.
Since the oDrawRectagle always returns the 0,0 co-ordinates for the ClientRectangle.

I want the image to be always aligned ImageAlign.MiddleCenter. How do i achieve it ?

Thanks

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-10-14 16:37:09

您可以使用按钮大小和图像大小手动将位置更改为中心。

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

You can just manually change the position to the center using the button size and the image size.

oDrawRectangle = new Rectangle(this.Width / 2 - iWidth / 2, this.Height / 2 - iHeight / 2, iWidth, iHeight);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文