c# - Changing the color of the elements of a UserControl -
i have problem whit usercontrol. when try hover change color of components , function "changecolor" isn't fire correctly.
if hover on label or picturebox of usercontrol, evoked event mouseleave
this usercontrol
public partial class infouser : usercontrol { public infouser() { initializecomponent(); } public void setnome(string nome) { labeluserlogged.text = nome; } public void changecolor(system.drawing.color color) { labeluserlogged.backcolor = color; pictureboxuser.backcolor = color; } private void infouser_mousehover(object sender, eventargs e) { changecolor(color.cadetblue); } private void infouser_mouseleave(object sender, eventargs e) { changecolor(color.whitesmoke); } }
code of designer
private void initializecomponent() { this.labeluserlogged = new system.windows.forms.label(); this.pictureboxuser = new system.windows.forms.picturebox(); ((system.componentmodel.isupportinitialize)(this.pictureboxuser)).begininit(); this.suspendlayout(); // // labeluserlogged // this.labeluserlogged.backcolor = system.drawing.systemcolors.control; this.labeluserlogged.borderstyle = system.windows.forms.borderstyle.fixedsingle; this.labeluserlogged.cursor = system.windows.forms.cursors.hand; this.labeluserlogged.location = new system.drawing.point(0, 0); this.labeluserlogged.name = "labeluserlogged"; this.labeluserlogged.size = new system.drawing.size(167, 27); this.labeluserlogged.tabindex = 3; this.labeluserlogged.text = "non loggato"; this.labeluserlogged.textalign = system.drawing.contentalignment.middlecenter; // // pictureboxuser // this.pictureboxuser.backcolor = system.drawing.color.transparent; this.pictureboxuser.cursor = system.windows.forms.cursors.hand; this.pictureboxuser.image = global::castor.gestionale.properties.resources.user_icon; this.pictureboxuser.location = new system.drawing.point(5, 6); this.pictureboxuser.name = "pictureboxuser"; this.pictureboxuser.size = new system.drawing.size(18, 15); this.pictureboxuser.sizemode = system.windows.forms.pictureboxsizemode.centerimage; this.pictureboxuser.tabindex = 4; this.pictureboxuser.tabstop = false; // // infouser // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.controls.add(this.pictureboxuser); this.controls.add(this.labeluserlogged); this.name = "infouser"; this.size = new system.drawing.size(171, 30); this.mouseleave += new system.eventhandler(this.infouser_mouseleave); this.mousehover += new system.eventhandler(this.infouser_mousehover); ((system.componentmodel.isupportinitialize)(this.pictureboxuser)).endinit(); this.resumelayout(false); }
child controls of usercontrol receive mouse-input when cursor within it's bounds. so, when mouse "enter" label/picturebox "leave" usercontrol , etc.
make specific child control "transparent" mouse events can use following trick:
const int wm_nchittest = 0x84, httransparent = (-1); class hittesttransparentpicturebox : picturebox { protected override void wndproc(ref message m) { if(m.msg == wm_nchittest) { m.result = new intptr(httransparent); return; } base.wndproc(ref m); } } class hittesttransparentlabel : label { protected override void wndproc(ref message m) { if(m.msg == wm_nchittest) { m.result = new intptr(httransparent); return; } base.wndproc(ref m); } }
then can replace specific controls in usercontrol "mouse-transparent" analogs:
this.labeluserlogged = new hittesttransparentlabel(); this.pictureboxuser = new hittesttransparentpicturebox();
after can use following approach create hover-effect on usercontrol:
public infouser() { initializecomponent(); mouseenter += infouser_mouseenter; mouseleave += infouser_mouseleave; } void infouser_mouseleave(object sender, eventargs e) { hover = false; } void infouser_mouseenter(object sender, eventargs e) { hover = true; } bool hovercore; protected bool hover { { return hovercore; } set { if(hovercore == value) return; hovercore = value; onhoverchanged(); } } void onhoverchanged() { changecolor(hover ? color.cadetblue : color.whitesmoke); }
Comments
Post a Comment