c# - TextBox with bottom border -
i want have textbox
bottom border graphics drawn textbox
distorted/broken on resize because of color.transparent
.
using code found, able create underlined textbox (drawn rectangle tranparent top, left, right). problem when resize form/window: when resize smaller, resize again expand it, graphics drawn distorted. any fix this?
here photos: the second photo has been resized smaller, larger size.
here's code:
[dllimport("user32")] private static extern intptr getwindowdc(intptr hwnd); struct rect { public int left, top, right, bottom; } struct nccalsize_params { public rect newwindow; public rect oldwindow; public rect clientwindow; intptr windowpos; } float clientpadding = 0; int actualborderwidth = 2; color bordercolor = color.black; protected override void wndproc(ref message m) { //we have change clientsize make room borders //if not, border limited in how thick is. if (m.msg == 0x83) { //wm_nccalcsize if (m.wparam == intptr.zero) { rect rect = (rect)marshal.ptrtostructure(m.lparam, typeof(rect)); rect.left += 2; rect.right -= 2; rect.top += 0; rect.bottom -= 0;// (int)clientpadding; marshal.structuretoptr(rect, m.lparam, false); } else { nccalsize_params rects = (nccalsize_params)marshal.ptrtostructure(m.lparam, typeof(nccalsize_params)); rects.newwindow.left += (int)clientpadding; rects.newwindow.right -= (int)clientpadding; rects.newwindow.top += (int)clientpadding; rects.newwindow.bottom -= 2; marshal.structuretoptr(rects, m.lparam, false); } } if (m.msg == 0x85) {//wm_ncpaint intptr wdc = getwindowdc(handle); using (graphics g = graphics.fromhdc(wdc)) { controlpaint.drawborder(g, new rectangle(0, 0, size.width, size.height), color.transparent, 1, buttonborderstyle.solid, color.transparent, 1, buttonborderstyle.solid, color.transparent, 1, buttonborderstyle.solid, bordercolor, actualborderwidth, buttonborderstyle.solid); } return; } base.wndproc(ref m); }
edit :
found issue, it's because ofcolor.transparent
fixed changing color.white, since have white background. then, not case, how prevent "flickering/tearing" while using color.transparent?
to have textbox
bottom border, simple workaround can offer docking 1 pixel height lable (or other control) bottom of textbox
:
using system.drawing; using system.windows.forms; public class mytextbox : textbox { public mytextbox() { borderstyle = system.windows.forms.borderstyle.none; autosize = false; //allows change height have bottom padding controls.add(new label() { height = 1, dock = dockstyle.bottom, backcolor = color.black }); } }
Comments
Post a Comment