在创建.NET WinForms应用程序图像按钮 [英] Creating an image button in .NET Winforms application

查看:115
本文介绍了在创建.NET WinForms应用程序图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Visual Studio 2010中,这只是一个图像创建我的.NET 4.0的WinForms应用程序的按钮。我有一个窗口,是无国界的,并有一个背景图片,使我的自定义皮肤这种应用。在窗口右上角的关闭/最小化按钮,我想创建2个简单的按钮是仅适用于图像看起来像典型的Windows关闭/最小化按钮。

I'm trying to create a button in my .NET 4.0 Winforms app in Visual Studio 2010 that is ONLY an image. I have a window that is borderless and has a background image that makes up my custom skin for this application. For the close/minimize buttons in the top right of the window, I wanted to create 2 simple buttons that are images only that look like the typical Windows close/minimize buttons.

我可能会去这个设计错误,因此,如果我请让我知道。到目前为止,我已经决定,我需要创建一个子类按钮只呈现图像。最后执行需要呈现不同的图像的每个按钮状态(正常,悬停,点击等)。这是我到目前为止有:

I may be going about this design wrong, so if I am please let me know. So far I've determined that I need to create a subclass for Button that only renders the image. The final implementation needs to render different images for each button state (normal, hover, clicked, etc). Here is what I have so far:

public class ImageButton : Button
{
    Pen pen = new Pen( Color.Red, 1.0f );

    public ImageButton()
    {
        SetClientSizeCore( BackgroundImage.Width, BackgroundImage.Height );
    }

    protected override void OnPaint( PaintEventArgs e )
    {
        e.Graphics.DrawImage( BackgroundImage, 0, 0 );
        //e.Graphics.DrawRectangle( pen, ClientRectangle );
        //Rectangle bounds = new Rectangle( 0, 0, Width, Height );
        //ButtonRenderer.DrawButton( e.Graphics, bounds, PushButtonState.Normal );
        //base.OnPaint(pevent);
    }

    protected override void OnPaintBackground( PaintEventArgs e )
    {
        // Do nothing
    }
}

在这一点上,假设这样的设计是合适的,我需要知道如何调用SetClientSizeCore()适当。称它在构造函数中引发异常。我想这是因为控制一直没有机会初始化呢。我不知道什么功能覆盖,让我改变了按钮的大小以适应图像后,它已被.NET初始化。对此有何想法?

At this point, assuming this design is appropriate, I need to know how to call SetClientSizeCore() appropriately. Calling it in the constructor raises an exception. I assume this is because the control hasn't had a chance to initialize yet. I'm not sure what function to override that will allow me to change the size of my button to fit the image after it has been initialized by .NET. Any ideas on this?

推荐答案

在构造函数中,的BackgroundImage

您需要设置大小时的BackgroundImage 是通过覆盖属性更改。

You need to set the size when BackgroundImage is changed by overriding the property.

您也应该遮蔽尺寸属性,并添加 [DesignerSerializationVisibilty(DesignerSerializationVisibility.Hidden)] 来$ P $被保存的设计师pvent大小。

You should also shadow the Size property and add [DesignerSerializationVisibilty(DesignerSerializationVisibility.Hidden)] to prevent the size from being saved by the designer.

这篇关于在创建.NET WinForms应用程序图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆