c# - Custom control rendering weird in Tapcontrol -
here odd 1 , bit long describe bear me on one.
i have control made c# 4 client profile called toggleswitch
(tsw). toggleswitch design act metro switch, , splendidly ... until added tap control/tap page :
the rendering acts weird not drawing background , on/off label don’t show up. affects other controls well, whole form won't draw.
also both vs 2010 , 2013 design editors properties fields freeze can't access other controls
and if left in normal program won't build ... crashed vs..
but, if change tap control normal either flatbutton or button stops , runs smoothly , fine, started wondering why don't render in normal
so here have tried
the tsw extends
buttonbase
clickability. changed controlonpaint
gets called in unlimited loop don’t help, click don’t work less important righti extended of button, no there still weird
override every single method both button , buttonbase see if helped still nothing.
after looking @ forms designer found property gets changed
usevisualstylebackcolor
when false works when true don't, made override forces false, still doesn't work :/i tried remove partial keyword.
i spend half day reading defriend methods of both tap , button controls
even found custom control test extended of button worked fine, there no reason because different see if statement in
onpaint
rendering.
.... nothing works ....
here togglewitch in entirety:
public partial class toggleswith : buttonbase { object _lock = new object(); private color _offcolor = systemcolors.controldarkdark; private color _oncolor = color.limegreen; private bool _colortext = false; // private bool _beendrawn = false; private bool _checked = false; [browsable(false)] public override string text { { return null; } } [browsable(false)] public override bool autosize{get{return false;}} [browsable(false)] public override contentalignment textalign { { return contentalignment.middleleft; } } [category("appearance")] public virtual color offcolor { { return _offcolor; } set { _offcolor = value; invalidate(); } } [category("appearance")] public virtual color oncolor { { return _oncolor; } set { _oncolor = value; invalidate(); } } [category("appearance")] public virtual bool colortext { { return _colortext; } set { _colortext = value; invalidate(); } } [category("appearance")] public virtual bool checked { { return _checked; } set { _checked = value; invalidate(); } } new public bool usevisualstylebackcolor { { return false; } set{} } protected override size defaultsize { { return new size(80, 18); } } protected override size defaultminimumsize { { return new size(40, 13); } } public event eventhandler togglechanged; public toggleswith() { this.size = new size(80, 18); initializecomponent(); this.click += toggleswith_click; } void toggleswith_click(object sender, eventargs e) { lock (_lock) { if (_checked) _checked = false; else _checked = true; } ontogglechanged(); } private void ontogglechanged() { if (togglechanged != null) { eventargs ea = new eventargs(); togglechanged(this, ea); } } protected override void onpaint(painteventargs e) { this.controls.clear(); int toggleblocsize=(int)this.size.width/8; int indent = 23; int labelx = 2; // declare , instantiate new pen. solidbrush offpen = new solidbrush(this.offcolor); solidbrush onpen = new solidbrush(this.oncolor); solidbrush backgroundpen = new solidbrush(this.backcolor); solidbrush blackpen = new solidbrush(systemcolors.controltext); pen controldarkpen = new pen(systemcolors.controldark); pen controlpen = new pen(systemcolors.control); e.graphics.fillrectangle(backgroundpen,0, 0, this.size.width,this.size.height); e.graphics.drawrectangle(controldarkpen, indent, -0, this.size.width - (indent+1), this.size.height-1); label l = new label(); l.font = this.font; l.size = new size((indent-labelx),this.size.height); // if(this.size.height <13)l.location= new point(labelx,-1); // else l.location = new point(labelx, 0); l.textalign = textalign; l.forecolor = this.forecolor; if (this._checked) { //ligth e.graphics.fillrectangle(onpen, (indent + 2), 2, this.size.width - (indent + 4), this.size.height - 4); //toggle e.graphics.drawrectangle(controlpen, this.size.width - (toggleblocsize+1), -0, this.size.width - (indent + 1), this.size.height - 1); e.graphics.fillrectangle(blackpen, this.size.width - (toggleblocsize), -1, this.size.width, this.size.height + 1); if (colortext) l.forecolor = oncolor; l.text = "on"; } else { //ligth e.graphics.fillrectangle(offpen, (indent + 2), 2, this.size.width - (indent + 4), this.size.height - 4); //toggle e.graphics.drawrectangle(controlpen, indent , -0, (toggleblocsize + 1), this.size.height - 1); e.graphics.fillrectangle(blackpen, (indent+1) , -1,(toggleblocsize), this.size.height + 1); if (colortext) l.forecolor = this.forecolor; l.text = "off"; } this.controls.add(l); } public override size getpreferredsize(size proposedsize) { return new size(80, 18); } }
it not can't live flat tapcontrol why doesn't work bugs me ...
any appreciated :)
lastly visual documentation of problems:
here how should work:
this tap on normal
got fixed work, turns out there few things wrong.
first of base class wrong, should control
public class toggleswith : control
and on paint event changed
protected override void onpaint(painteventargs e) { //this.controls.clear(); int toggleblocsize = (int)this.size.width / 6; int indent = 23; int labelx = 2; // declare , instantiate new pen. solidbrush offpen = new solidbrush(this.offcolor); solidbrush onpen = new solidbrush(this.oncolor); rectangle texttangle = new rectangle(new point(labelx, 0), new size(indent - labelx, this.size.height)); solidbrush backgroundbrush = new solidbrush(this.parent.backcolor); solidbrush togglebrush = new solidbrush(systemcolors.controltext); solidbrush textbrush = new solidbrush(systemcolors.controltext); pen controldarkpen = new pen(systemcolors.controldark); pen controlpen = new pen(systemcolors.control); e.graphics.fillrectangle(backgroundbrush, 0, 0, this.size.width, this.size.height); e.graphics.fillrectangle(new solidbrush(backcolor), indent, 0, this.size.width - indent, this.size.height); e.graphics.drawrectangle(controldarkpen, indent, -0, this.size.width - (indent + 1), this.size.height - 1); if (!this.enabled) { offpen = new solidbrush(systemcolors.control); onpen = new solidbrush(systemcolors.control); togglebrush = new solidbrush(systemcolors.controldark); textbrush = new solidbrush(systemcolors.control); } if (this._checked) { //ligth e.graphics.fillrectangle(onpen, (indent + 2), 2, this.size.width - (indent + 4), this.size.height - 4); //toggle e.graphics.drawrectangle(controlpen, this.size.width - (toggleblocsize + 1), -0, this.size.width - (indent + 1), this.size.height - 1); e.graphics.fillrectangle(togglebrush, this.size.width - (toggleblocsize), -1, this.size.width, this.size.height + 1); if (colortext && this.enabled) textbrush = new solidbrush(oncolor);//_label.forecolor = oncolor; // _label.text = "on"; // draw string screen. textrenderer.drawtext(e.graphics, ontext, this.font, texttangle, textbrush.color, this.parent.backcolor, textformatflags.horizontalcenter | textformatflags.verticalcenter | textformatflags.glyphoverhangpadding); } else { //ligth e.graphics.fillrectangle(offpen, (indent + 2), 2, this.size.width - (indent + 4), this.size.height - 4); //toggle e.graphics.drawrectangle(controlpen, indent, -0, (toggleblocsize + 1), this.size.height - 1); e.graphics.fillrectangle(togglebrush, (indent + 1), -1, (toggleblocsize), this.size.height + 1); if (colortext && this.enabled) textbrush = new solidbrush(systemcolors.controltext); // draw string screen. textrenderer.drawtext(e.graphics, offtext, this.font, texttangle, textbrush.color, this.parent.backcolor, textformatflags.horizontalcenter | textformatflags.verticalcenter | textformatflags.glyphoverhangpadding); }
that got graphics result looking for.
Comments
Post a Comment