c# - Snap window size to fixed multiples -
the windows command console allows resize window multiple of character size. "snapping" of window size instantaneous , not flicker. in native code, done processing wm_sizing
message , modifying rect
structure accordingly.
in c#, tried overriding onresize
method, computing "snapped" size, , setting form's clientsize
property accordingly. unfortunately, size keeps jumping between snapped size , whatever size cursor dictates.
protected override void onresize(eventargs e) { int tgtcols = (clientsize.width + 4) / 8; int tgtlines = (clientsize.height + 8) / 15; if (cols != tgtcols || lines != tgtlines) { cols = tgtcols; lines = tgtlines; int tgtwidth = cols * 8; int tgtheight = lines * 15; //clientsize = new size(tgtwidth, tgtheight); size = new size(tgtwidth + exwidth, tgtheight + exheight); } base.onresize(e); }
as can see, i've tried using both size
, clientsize
properties, both yield same effect. there better way of constraining size? or need manually intercept wm_sizing
message?
edit: tried manually intercepting wm_sizing
, same result:
protected override void wndproc(ref message m) { switch (m.msg) { case wm_sizing: dosizesnap(ref m); m.result = new intptr(1); break; default: base.wndproc(ref m); break; } } private unsafe void dosizesnap(ref message m) { int edge = m.wparam.toint32(); rect *prect = (rect *)m.lparam.topointer(); int tgtcols = (prect->right - prect->left - exwidth + 4) / 8; int tgtlines = (prect->bottom - prect->top - exheight + 8) / 15; if (cols != tgtcols || lines != tgtlines) { cols = tgtcols; lines = tgtlines; int tgtwidth = cols * 8; int tgtheight = lines * 15; // todo: handle edge prect->right = prect->left + tgtwidth + exwidth; prect->bottom = prect->top + tgtheight + exheight; } }
subscribing resize
event doesn't work either.
the problem statement if (cols != tgtcols || lines != tgtlines)
. causes size snapping occur when target lines/columns changes, not when window width/height changes. prevent infinite recursion, resize
event not fired if size not change, if
statement unnecessary.
Comments
Post a Comment