vb.net - VB Windows form will not size to applications width/height -
i have visual basic windows form size set applications width/height it's not working.
while works fine me in vba, not working desired addin:
dim newform new exportingform newform.showdialog() public class exportingform private sub exportingform_layout(sender object, e eventargs) handles mybase.layout dim exclapp excel.application = globals.thisaddin.application if exclapp.windowstate = excel.xlwindowstate.xlmaximized me.windowstate = system.windows.forms.formwindowstate.maximized else me.size = new drawing.point(exclapp.width, exclapp.height) end if end sub end class
additionally in designer mode here settings windows form:
ismdicontainer false location 0,0 maximumsize 0,0 minimumsize 0,0 padding 0,0,0,0 size 250,250 startposition centerparent
it centers fine , can alter width/height programmatically fine well, however, when setting applications width/height changes point , stops. need correct this?
i've tried :
me.size = new drawing.point(exclapp.activewindow.width, exclapp.activewindow.height)
and i've tried setting size before showing form:
dim newform..... newform.size = new drawing.point(exclapp.width, exclapp.height) newform.showdialog()
i can translate language have long works visual studio
correction
in theory method presented should work, there issues excel pointstoscreenpixels methods. internet search on functions indicates results unreliable @ best. therefore, recommend using win32 api function getwindowrect retrieve excel application's position , size. api function definitions obtained http://www.pinvoke.net/index.aspx.
imports excel = microsoft.office.interop.excel imports system.runtime.interopservices public class form1 private sub button1_click(sender object, e eventargs) handles button1.click test() end sub sub test() dim medpi pointf = getdpi(me.handle) dim app new excel.application app.visible = true dim apphwnd intptr = new intptr(app.hwnd) ' setting excel's size , position -- test verification purposes setwindowpos(apphwnd, intptr.zero, 10, 10, 500, 300, setwindowposflags.donotactivate) dim rc rect getwindowrect(apphwnd, rc) ' retrieve excel's size , position rc app.usercontrol = true ' return control user console.writeline("excel located @ x: {0}, y: {1}, width: {2}, height: {3}", rc.left, rc.top, rc.width, rc.height) me.location = rc.location me.size = rc.size me.activate() ' bring form front me.opacity = 0.5 ' allow view thru excel end sub public shared function getdpi(hwnd intptr) pointf dim ret pointf using g graphics = graphics.fromhwnd(hwnd) ret.x = g.dpix ret.y = g.dpiy end using return ret end function <dllimport("user32.dll")> _ private shared function getwindowrect(byval hwnd intptr, byref lprect rect) boolean end function <structlayout(layoutkind.sequential)> _ public structure rect private _left integer, _top integer, _right integer, _bottom integer public sub new(byval rectangle rectangle) me.new(rectangle.left, rectangle.top, rectangle.right, rectangle.bottom) end sub public sub new(byval left integer, byval top integer, byval right integer, byval bottom integer) _left = left _top = top _right = right _bottom = bottom end sub public property x integer return _left end set(byval value integer) _right = _right - _left + value _left = value end set end property public property y integer return _top end set(byval value integer) _bottom = _bottom - _top + value _top = value end set end property public property left integer return _left end set(byval value integer) _left = value end set end property public property top integer return _top end set(byval value integer) _top = value end set end property public property right integer return _right end set(byval value integer) _right = value end set end property public property bottom integer return _bottom end set(byval value integer) _bottom = value end set end property public property height() integer return _bottom - _top end set(byval value integer) _bottom = value + _top end set end property public property width() integer return _right - _left end set(byval value integer) _right = value + _left end set end property public property location() point return new point(left, top) end set(byval value point) _right = _right - _left + value.x _bottom = _bottom - _top + value.y _left = value.x _top = value.y end set end property public property size() size return new size(width, height) end set(byval value size) _right = value.width + _left _bottom = value.height + _top end set end property public shared widening operator ctype(byval rectangle rect) rectangle return new rectangle(rectangle.left, rectangle.top, rectangle.width, rectangle.height) end operator public shared widening operator ctype(byval rectangle rectangle) rect return new rect(rectangle.left, rectangle.top, rectangle.right, rectangle.bottom) end operator public shared operator =(byval rectangle1 rect, byval rectangle2 rect) boolean return rectangle1.equals(rectangle2) end operator public shared operator <>(byval rectangle1 rect, byval rectangle2 rect) boolean return not rectangle1.equals(rectangle2) end operator public overrides function tostring() string return "{left: " & _left & "; " & "top: " & _top & "; right: " & _right & "; bottom: " & _bottom & "}" end function public overloads function equals(byval rectangle rect) boolean return rectangle.left = _left andalso rectangle.top = _top andalso rectangle.right = _right andalso rectangle.bottom = _bottom end function public overloads overrides function equals(byval [object] object) boolean if typeof [object] rect return equals(directcast([object], rect)) elseif typeof [object] rectangle return equals(new rect(directcast([object], rectangle))) end if return false end function end structure <dllimport("user32.dll", setlasterror:=true)> _ private shared function setwindowpos(byval hwnd intptr, byval hwndinsertafter intptr, byval x integer, byval y integer, byval cx integer, byval cy integer, byval uflags setwindowposflags) boolean end function <flags> _ private enum setwindowposflags uinteger ''' <summary>if calling thread , thread owns window attached different input queues, ''' system posts request thread owns window. prevents calling thread ''' blocking execution while other threads process request.</summary> ''' <remarks>swp_asyncwindowpos</remarks> asynchronouswindowposition = &h4000 ''' <summary>prevents generation of wm_syncpaint message.</summary> ''' <remarks>swp_defererase</remarks> defererase = &h2000 ''' <summary>draws frame (defined in window's class description) around window.</summary> ''' <remarks>swp_drawframe</remarks> drawframe = &h20 ''' <summary>applies new frame styles set using setwindowlong function. sends wm_nccalcsize message ''' window, if window's size not being changed. if flag not specified, wm_nccalcsize ''' sent when window's size being changed.</summary> ''' <remarks>swp_framechanged</remarks> framechanged = &h20 ''' <summary>hides window.</summary> ''' <remarks>swp_hidewindow</remarks> hidewindow = &h80 ''' <summary>does not activate window. if flag not set, window activated , moved ''' top of either topmost or non-topmost group (depending on setting of hwndinsertafter ''' parameter).</summary> ''' <remarks>swp_noactivate</remarks> donotactivate = &h10 ''' <summary>discards entire contents of client area. if flag not specified, valid ''' contents of client area saved , copied client area after window sized or ''' repositioned.</summary> ''' <remarks>swp_nocopybits</remarks> donotcopybits = &h100 ''' <summary>retains current position (ignores x , y parameters).</summary> ''' <remarks>swp_nomove</remarks> ignoremove = &h2 ''' <summary>does not change owner window's position in z order.</summary> ''' <remarks>swp_noownerzorder</remarks> donotchangeownerzorder = &h200 ''' <summary>does not redraw changes. if flag set, no repainting of kind occurs. applies ''' client area, nonclient area (including title bar , scroll bars), , part of parent ''' window uncovered result of window being moved. when flag set, application must ''' explicitly invalidate or redraw parts of window , parent window need redrawing.</summary> ''' <remarks>swp_noredraw</remarks> donotredraw = &h8 ''' <summary>same swp_noownerzorder flag.</summary> ''' <remarks>swp_noreposition</remarks> donotreposition = &h200 ''' <summary>prevents window receiving wm_windowposchanging message.</summary> ''' <remarks>swp_nosendchanging</remarks> donotsendchangingevent = &h400 ''' <summary>retains current size (ignores cx , cy parameters).</summary> ''' <remarks>swp_nosize</remarks> ignoreresize = &h1 ''' <summary>retains current z order (ignores hwndinsertafter parameter).</summary> ''' <remarks>swp_nozorder</remarks> ignorezorder = &h4 ''' <summary>displays window.</summary> ''' <remarks>swp_showwindow</remarks> showwindow = &h40 end enum end class
please note in testing above code, winform application declared dpi aware having following in app.manifest file.
<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowssettings> <dpiaware xmlns="http://schemas.microsoft.com/smi/2005/windowssettings">true</dpiaware> </windowssettings> </application>
do not use
the application.height property , application.width property measured in points not pixels. can use window.pointstoscreenpixelsx method , window.pointstoscreenpixelsy methods compute width , height in pixels set form size.
width = exclapp.activewindow.pointstoscreenpixelsx(exclapp.width)
height = exclapp.activewindow.pointstoscreenpixelsy(exclapp.height)
i not know if have declare addin dpi aware avoid windows's scaling form.
note: base on testing in excel, activewindow yield value.
Comments
Post a Comment