c# - How to restrict form movement to horizontal? -
i have standard form standard title bar user may grab , move form around. in situations want restrict movement horizontal only, no matter how mouse moves, form remains on same y coordinate.
to this, catch move event , when detect deviation y, move form original y. that:
private void templateslide_move(object sender, eventargs e) { int y = slidesettings.lastlocation.y; if (y != this.location.y) { slidesettings.lastlocation = new point(this.location.x, y); this.location=settings.lastlocation; } }
but causes lot of flicker. because form moves brief moment away desired y causes other issues specific program.
is there way prevent form moving away desired y coordinate?
trap wm_moving , modify rect structure in lparam accordingly.
something like:
public partial class form1 : form { public const int wm_moving = 0x216; public struct rect { public int left; public int top; public int right; public int bottom; } private int originaly = 0; private int originalheight = 0; private bool horizontalmovementonly = true; public form1() { initializecomponent(); this.shown += new eventhandler(form1_shown); this.sizechanged += new eventhandler(form1_sizechanged); this.move += new eventhandler(form1_move); } void form1_move(object sender, eventargs e) { this.savevalues(); } void form1_sizechanged(object sender, eventargs e) { this.savevalues(); } void form1_shown(object sender, eventargs e) { this.savevalues(); } private void savevalues() { this.originaly = this.location.y; this.originalheight = this.size.height; } protected override void wndproc(ref message m) { switch (m.msg) { case wm_moving: if (this.horizontalmovementonly) { rect rect = (rect)system.runtime.interopservices.marshal.ptrtostructure(m.lparam, typeof(rect)); rect.top = this.originaly; rect.bottom = rect.top + this.originalheight; system.runtime.interopservices.marshal.structuretoptr(rect, m.lparam, false); } break; } base.wndproc(ref m); } }
Comments
Post a Comment