vb.net - How to move form without form border (visual studio) -
i making windows form application in visual studio 2013 express. in order make application more customized , attractive, designed forms in application. set form border style 'none' , made own title , exit/min/max buttons. however, way move form @ runtime dragging title bar, , cannot form border , title bar gone. so, possible move form without title bar , border. maybe if have own title bar , try make control forms position? best way of doing it?
thanks
simple this, add code form-class:
#region " move form " ' [ move form ] ' ' // elektro public moveform boolean public moveform_mouseposition point public sub moveform_mousedown(sender object, e mouseeventargs) handles _ mybase.mousedown ' add more handles here (example: picturebox1.mousedown) if e.button = mousebuttons.left moveform = true me.cursor = cursors.nomove2d moveform_mouseposition = e.location end if end sub public sub moveform_mousemove(sender object, e mouseeventargs) handles _ mybase.mousemove ' add more handles here (example: picturebox1.mousemove) if moveform me.location = me.location + (e.location - moveform_mouseposition) end if end sub public sub moveform_mouseup(sender object, e mouseeventargs) handles _ mybase.mouseup ' add more handles here (example: picturebox1.mouseup) if e.button = mousebuttons.left moveform = false me.cursor = cursors.default end if end sub #end region
here updated version:
' *********************************************************************** ' author : elektro ' modified : 15-march-2015 ' *********************************************************************** ' <copyright file="formdragger.vb" company="elektro studios"> ' copyright (c) elektro studios. rights reserved. ' </copyright> ' *********************************************************************** #region " option statements " option explicit on option strict on option infer off #end region #region " usage examples " 'public class form1 ' ''' <summary> ' ''' <see cref="formdragger"/> instance manages form(s) dragging. ' ''' </summary> ' private formdragger formdragger = formdragger.empty ' private sub test() handles mybase.shown ' me.initializedrag() ' end sub ' private sub button1_click(byval sender object, byval e eventargs) _ ' handles button1.click ' me.alternatedragenabled(me) ' end sub ' private sub initializedrag() ' ' 1st way, using single-form constructor: ' me.formdragger = new formdragger(me, enabled:=true, cursor:=cursors.sizeall) ' ' 2nd way, using multiple-forms constructor: ' ' me.formdragger = new formdragger({me, form2, form3}) ' ' 3rd way, using default constructor adding form collection: ' ' me.formdragger = new formdragger ' ' me.formdragger.addform(me, enabled:=true, cursor:=cursors.sizeall) ' end sub ' ''' <summary> ' ''' alternates dragging of specified form. ' ''' </summary> ' ''' <param name="form">the form.</param> ' private sub alternatedragenabled(byval form form) ' dim forminfo formdragger.formdraginfo = me.formdragger.findformdraginfo(form) ' forminfo.enabled = not forminfo.enabled ' end sub 'end class #end region #region " imports " imports system.componentmodel #end region #region " form dragger " ''' <summary> ''' enable or disable drag @ runtime on <see cref="form"/>. ''' </summary> public notinheritable class formdragger : implements idisposable #region " properties " ''' <summary> ''' gets <see cref="ienumerable(of form)"/> collection contains forms capables perform draggable operations. ''' </summary> ''' <value>the <see cref="ienumerable(of form)"/>.</value> <editorbrowsable(editorbrowsablestate.always)> public readonly property forms ienumerable(of formdraginfo) return me.forms1 end end property ''' <summary> ''' <see cref="ienumerable(of form)"/> collection contains forms capables perform draggable operations. ''' </summary> private forms1 ienumerable(of formdraginfo) = {} ''' <summary> ''' represents <see cref="formdragger"/> instance <c>nothing</c>. ''' </summary> ''' <value><c>nothing</c></value> <editorbrowsable(editorbrowsablestate.always)> public shared readonly property empty formdragger return nothing end end property #end region #region " types " ''' <summary> ''' defines draggable info of <see cref="form"/>. ''' </summary> <serializable> public notinheritable class formdraginfo #region " properties " ''' <summary> ''' gets associated <see cref="form"/> used perform draggable operations. ''' </summary> ''' <value>the associated <see cref="form"/>.</value> <editorbrowsable(editorbrowsablestate.always)> public readonly property form form return form1 end end property ''' <summary> ''' associated <see cref="form"/> ''' </summary> <nonserialized> private readonly form1 form ''' <summary> ''' gets name of associated <see cref="form"/>. ''' </summary> ''' <value>the form.</value> <editorbrowsable(editorbrowsablestate.always)> public readonly property name string if me.form isnot nothing return form.name else return string.empty end if end end property ''' <summary> ''' gets or sets value indicating whether drag enabled on associated <see cref="form"/>. ''' </summary> ''' <value><c>true</c> if drag enabled; otherwise, <c>false</c>.</value> <editorbrowsable(editorbrowsablestate.always)> public property enabled boolean ''' <summary> ''' <see cref="formdragger"/> instance instance containing draggable information of associated <see cref="form"/>. ''' </summary> ''' <value>the draggable information.</value> <editorbrowsable(editorbrowsablestate.never)> public property draginfo formdragger = formdragger.empty ''' <summary> ''' gets or sets <see cref="cursor"/> used drag associated <see cref="form"/>. ''' </summary> ''' <value>the <see cref="cursor"/>.</value> <editorbrowsable(editorbrowsablestate.always)> public property cursor cursor = cursors.sizeall ''' <summary> ''' gets or sets old form's cursor restore after dragging. ''' </summary> ''' <value>the old form's cursor.</value> <editorbrowsable(editorbrowsablestate.never)> public property oldcursor cursor = nothing ''' <summary> ''' gets or sets initial mouse coordinates, <see cref="form.mouseposition"/>. ''' </summary> ''' <value>the initial mouse coordinates.</value> <editorbrowsable(editorbrowsablestate.never)> public property initialmousecoords point = point.empty ''' <summary> ''' gets or sets initial <see cref="form"/> location, <see cref="form.location"/>. ''' </summary> ''' <value>the initial location.</value> <editorbrowsable(editorbrowsablestate.never)> public property initiallocation point = point.empty #end region #region " constructors " ''' <summary> ''' initializes new instance of <see cref="formdraginfo"/> class. ''' </summary> ''' <param name="form">the form.</param> public sub new(byval form form) me.form1 = form me.cursor = form.cursor end sub ''' <summary> ''' prevents default instance of <see cref="formdraginfo"/> class being created. ''' </summary> private sub new() end sub #end region #region " hidden methods " ''' <summary> ''' serves hash function particular type. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function gethashcode() integer return mybase.gethashcode end function ''' <summary> ''' gets system.type of current instance. ''' </summary> ''' <returns>the exact runtime type of current instance.</returns> <editorbrowsable(editorbrowsablestate.never)> public shadows function [gettype]() type return mybase.gettype end function ''' <summary> ''' determines whether specified system.object instances considered equal. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function equals(byval obj object) boolean return mybase.equals(obj) end function ''' <summary> ''' determines whether specified system.object instances same instance. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> private shadows sub referenceequals() end sub ''' <summary> ''' returns string represents current object. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function tostring() string return mybase.tostring end function #end region end class #end region #region " constructors " ''' <summary> ''' initializes new instance of <see cref="formdragger"/> class. ''' </summary> public sub new() me.forms1={} end sub ''' <summary> ''' initializes new instance of <see cref="formdragger"/> class. ''' </summary> ''' <param name="form">the <see cref="form"/> used perform draggable operations.</param> ''' <param name="enabled">if set <c>true</c>, enable dragging on <see cref="form"/>.</param> ''' <param name="cursor">the <see cref="cursor"/> used drag specified <see cref="form"/>.</param> public sub new(byval form form, optional enabled boolean = false, optional cursor cursor = nothing) me.forms1 = { new formdraginfo(form) { .enabled = enabled, .cursor = cursor } } me.assochandlers(form) end sub ''' <summary> ''' initializes new instance of <see cref="formdragger"/> class. ''' </summary> ''' <param name="forms">the <see cref="forms"/> used perform draggable operations.</param> public sub new(byval forms ienumerable(of form)) me.forms1 = (from form form in forms select new formdraginfo(form)).toarray each form form in forms me.assochandlers(form) next form end sub ''' <summary> ''' initializes new instance of <see cref="formdragger"/> class. ''' </summary> ''' <param name="forminfo"> ''' <see cref="formdraginfo"/> instance ''' contains <see cref="form"/> reference , draggable info. ''' </param> ''' <param name="mousecoordinates">the current mouse coordinates.</param> ''' <param name="location">the current location.</param> private sub new(byval forminfo formdraginfo, byval mousecoordinates point, byval location point) forminfo.initialmousecoords = mousecoordinates forminfo.initiallocation = location end sub #end region #region " public methods " ''' <summary> ''' adds specified <see cref="form"/> draggable <see cref="forms"/> collection. ''' </summary> ''' <param name="form">the <see cref="form"/>.</param> ''' <param name="enabled">if set <c>true</c>, enable dragging on <see cref="form"/>.</param> ''' <param name="cursor">the <see cref="cursor"/> used drag specified <see cref="form"/>.</param> ''' <exception cref="system.argumentexception">the specified form added.;form</exception> public function addform(byval form form, optional enabled boolean = false, optional cursor cursor = nothing) formdraginfo each forminfo formdraginfo in me.forms1 if forminfo.form.equals(form) throw new argumentexception("the specified form added.", "form") exit function end if next forminfo dim newforminfo new formdraginfo(form) {.enabled = enabled, .cursor = cursor} me.forms1 = me.forms1.concat({newforminfo}) me.assochandlers(form) return newforminfo end function ''' <summary> ''' removes specified <see cref="form"/> draggable <see cref="forms"/> collection. ''' </summary> ''' <param name="form">the form.</param> ''' <exception cref="system.argumentexception">the specified form not found.;form</exception> public sub removeform(byval form form) dim forminfotoremove formdraginfo = nothing each forminfo formdraginfo in me.forms1 if forminfo.form.equals(form) forminfotoremove = forminfo exit end if next forminfo if forminfotoremove isnot nothing me.forms1 = forminfo formdraginfo in me.forms1 not forminfo forminfotoremove forminfotoremove.enabled = false me.deassochandlers(forminfotoremove.form) else throw new argumentexception("the specified form not found.", "form") end if end sub ''' <summary> ''' finds <see cref="formdraginfo"/> instance associated specified <see cref="form"/> reference. ''' </summary> ''' <param name="form">the <see cref="form"/>.</param> ''' <returns>the <see cref="formdraginfo"/> instance associated specified <see cref="form"/> reference.</returns> public function findformdraginfo(byval form form) formdraginfo return (from forminfo formdragger.formdraginfo in me.forms1 forminfo.form form).firstordefault end function ''' <summary> ''' finds <see cref="formdraginfo"/> instance associated specified <see cref="form"/> reference. ''' </summary> ''' <param name="name">the <see cref="form"/> name.</param> ''' <returns>the <see cref="formdraginfo"/> instance associated specified <see cref="form"/> reference.</returns> public function findformdraginfo(byval name string, optional stringcomparison stringcomparison = stringcomparison.ordinalignorecase) formdraginfo return (from forminfo formdragger.formdraginfo in me.forms1 forminfo.name.equals(name, stringcomparison)).firstordefault end function #end region #region " private methods " ''' <summary> ''' associates <see cref="form"/> handlers enable draggable operations. ''' </summary> ''' <param name="form">the form.</param> private sub assochandlers(byval form form) addhandler form.mousedown, addressof me.form_mousedown addhandler form.mouseup, addressof me.form_mouseup addhandler form.mousemove, addressof me.form_mousemove addhandler form.mouseenter, addressof me.form_mouseenter addhandler form.mouseleave, addressof me.form_mouseleave end sub ''' <summary> ''' deassociates <see cref="form"/> handlers disable draggable operations. ''' </summary> ''' <param name="form">the form.</param> private sub deassochandlers(byval form form) if not form.isdisposed andalso not form.disposing removehandler form.mousedown, addressof me.form_mousedown removehandler form.mouseup, addressof me.form_mouseup removehandler form.mousemove, addressof me.form_mousemove removehandler form.mouseenter, addressof me.form_mouseenter removehandler form.mouseleave, addressof me.form_mouseleave end if end sub ''' <summary> ''' return new location. ''' </summary> ''' <param name="forminfo"> ''' <see cref="formdraginfo"/> instance ''' contains <see cref="form"/> reference , draggable info. ''' </param> ''' <param name="mousecoordinates">the current mouse coordinates.</param> ''' <returns>the new location.</returns> private function getnewlocation(byval forminfo formdraginfo, byval mousecoordinates point) point return new point(forminfo.initiallocation.x + (mousecoordinates.x - forminfo.initialmousecoords.x), forminfo.initiallocation.y + (mousecoordinates.y - forminfo.initialmousecoords.y)) end function #end region #region " hidden methods " ''' <summary> ''' serves hash function particular type. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function gethashcode() integer return mybase.gethashcode end function ''' <summary> ''' gets system.type of current instance. ''' </summary> ''' <returns>the exact runtime type of current instance.</returns> <editorbrowsable(editorbrowsablestate.never)> public shadows function [gettype]() type return mybase.gettype end function ''' <summary> ''' determines whether specified system.object instances considered equal. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function equals(byval obj object) boolean return mybase.equals(obj) end function ''' <summary> ''' determines whether specified system.object instances same instance. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> private shadows sub referenceequals() end sub ''' <summary> ''' returns string represents current object. ''' </summary> <editorbrowsable(editorbrowsablestate.never)> public shadows function tostring() string return mybase.tostring end function #end region #region " event handlers " ''' <summary> ''' handles mouseenter event of form. ''' </summary> ''' <param name="sender">the source of event.</param> ''' <param name="e">the <see cref="eventargs"/> instance containing event data.</param> private sub form_mouseenter(byval sender object, byval e eventargs) dim forminfo formdraginfo = findformdraginfo(directcast(sender, form)) forminfo.oldcursor = forminfo.form.cursor if forminfo.enabled forminfo.form.cursor = forminfo.cursor ' optional: ' forminfo.form.bringtofront() end if end sub ''' <summary> ''' handles mouseleave event of form. ''' </summary> ''' <param name="sender">the source of event.</param> ''' <param name="e">the <see cref="eventargs"/> instance containing event data.</param> private sub form_mouseleave(byval sender object, byval e eventargs) dim forminfo formdraginfo = findformdraginfo(directcast(sender, form)) forminfo.form.cursor = forminfo.oldcursor end sub ''' <summary> ''' handles mousedown event of form. ''' </summary> ''' <param name="sender">the source of event.</param> ''' <param name="e">the <see cref="mouseeventargs"/> instance containing event data.</param> private sub form_mousedown(byval sender object, byval e mouseeventargs) dim forminfo formdraginfo = findformdraginfo(directcast(sender, form)) if forminfo.enabled forminfo.draginfo = new formdragger(forminfo, form.mouseposition, forminfo.form.location) end if end sub ''' <summary> ''' handles mousemove event of form. ''' </summary> ''' <param name="sender">the source of event.</param> ''' <param name="e">the <see cref="mouseeventargs"/> instance containing event data.</param> private sub form_mousemove(byval sender object, byval e mouseeventargs) dim forminfo formdraginfo = findformdraginfo(directcast(sender, form)) if forminfo.enabled andalso (forminfo.draginfo isnot formdragger.empty) forminfo.form.location = forminfo.draginfo.getnewlocation(forminfo, form.mouseposition) end if end sub ''' <summary> ''' handles mouseup event of form. ''' </summary> ''' <param name="sender">the source of event.</param> ''' <param name="e">the <see cref="mouseeventargs"/> instance containing event data.</param> private sub form_mouseup(byval sender object, byval e mouseeventargs) dim forminfo formdraginfo = findformdraginfo(directcast(sender, form)) forminfo.draginfo = formdragger.empty end sub #end region #region " idisposable " ''' <summary> ''' detect redundant calls when disposing. ''' </summary> private isdisposed boolean = false ''' <summary> ''' performs application-defined tasks associated freeing, releasing, or resetting unmanaged resources. ''' </summary> public sub dispose() implements idisposable.dispose me.dispose(true) gc.suppressfinalize(me) end sub ''' <summary> ''' releases unmanaged , - optionally - managed resources. ''' </summary> ''' <param name="isdisposing"> ''' <c>true</c> release both managed , unmanaged resources; ''' <c>false</c> release unmanaged resources. ''' </param> protected sub dispose(byval isdisposing boolean) if not me.isdisposed if isdisposing each forminfo formdraginfo in me.forms1 forminfo .enabled = false .oldcursor = nothing .draginfo = formdragger.empty .initialmousecoords = point.empty .initiallocation = point.empty me.deassochandlers(.form) end ' form next forminfo me.forms1 = nothing end if ' isdisposing end if ' not me.isdisposed me.isdisposed = true end sub #end region end class #end region
Comments
Post a Comment