c# - Creating an image button in .NET Winforms application -
i'm trying create button in .net 4.0 winforms app in visual studio 2010 image. have window borderless , has background image makes custom skin application. close/minimize buttons in top right of window, wanted create 2 simple buttons images typical windows close/minimize buttons.
i may going design wrong, if please let me know. far i've determined need create subclass button renders image. final implementation needs render different images each button state (normal, hover, clicked, etc). here have 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 ) { // nothing } }
at point, assuming design appropriate, need know how call setclientsizecore() appropriately. calling in constructor raises exception. assume because control hasn't had chance initialize yet. i'm not sure function override allow me change size of button fit image after has been initialized .net. ideas on this?
in constructor, backgroundimage
null
.
you need set size when backgroundimage
changed overriding property.
you should shadow size
property , add [designerserializationvisibilty(designerserializationvisibility.hidden)]
prevent size being saved designer.
Comments
Post a Comment