使VB .net userform的行为与VBA类似 [英] Make VB .net userform behave like VBA
问题描述
我正在尝试将我的VB .net用户表单附加到现有应用程序(CATIA),这样当父应用程序窗口最小化时,我的用户表单也是如此。当父应用程序处于活动状态时,我希望我的用户表单保持最佳状态。我已经通过使用SetParent Windows API实现了这一点,但我希望我的userform不会被限制在父应用程序窗口边框内。基本上我希望我的表单就像VBA表单一样。
我考虑创建一个out进程来监视父应用程序窗口并根据它控制我的用户表单激活状态,但这似乎过于复杂。
我很感激任何人都可以提供帮助。
不要这样做!通过使用VB.NET,您可以部分地进入应用程序的行为世界,并根据适当的UI样式。
在.NET中,表单之间的子父关系有意地被解除。表单
是一个Control
,对于Controls
,你可以使另一个控制
父母;这是控制层次结构的基础。但是,你会成为一个孩子控制
是一个表单
,会抛出异常,为了一个好处原因。
现在,您可以通过将属性Form.TopLevel
设置为true来覆盖此行为。它会使这种形式成为一个孩子,但是 - 不要这样做!。您将获得表单的丑陋结果,其所有非客户区域限制在父应用程序窗口边框内。
请参阅:
http://msdn.microsoft.com/en-us /library/system.windows.forms.form.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.toplevel.aspx [ ^ ]。
要实现正确的UI外观和行为,请执行以下操作:使您拥有的孩子的候选人表单
非表格控制
,通常为Panel
。如果可以托管您表单
的所有内容。然后,让这个Panel
一个Form
的孩子或另一个Panel
。你明白了吗?理想情况下,您的所有应用程序只能是一个主要表单,其余所有应用程序都是此表单的子项。另一个好但很简单的可能性是使用类System.Windows.Forms.TabPage
这是System.Windows.Forms.TabControl $的一部分c $ c>:
http:// msdn .microsoft.com / zh-cn / library / system.windows.forms.tabpage.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.aspx [ ^ ]。
在TabControl
方法中,每个TabPage
将扮演什么角色你把它描绘成一个孩子的形式。
最后,一个 大 警告 !另一个解决方案是MDI,MDI父母和MDI儿童。不要这样做,避免麻烦,避免吓跑客户。
这里的一些动机:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],
如何在WPF中创建MDI父窗口? [ ^ ]。
此外,我过去答案的一些动机:
如何在WPF中创建MDI父窗口? [ Solution 2 ],
关于在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer给出错误 [ ^ ],
如何设置子窗体最大化,最后的子窗体最小化 [ ^ ]。< br $> b $ b
祝你好运,留下ou麻烦,
-SA
问题比我想象的要容易。我只需编写一个类来将IntPtr转换为IWin32Window
公共类WindowWrapper
实现System.Windows.Forms.IWin32Window
Private _hwnd As IntPtr
Public Sub New(ByVal handle As IntPtr)
_hwnd = handle
End Sub
Public ReadOnly Property Handle()作为IntPtr Implements System.Windows.Forms.IWin32Window.Handle
获取
返回_hwnd
结束获取
结束物业
结束等级
然后在我显示用户表单时实现它。我将此行添加到表单类中New子的末尾:
Me.Show(New WindowWrapper(Application.Hwnd)
I am trying to attach my VB .net userform to an existing application (CATIA) such that when the parent application window is minimized, so is my userform. And when the parent application is active, I want my userform to stay on top. I have achieved this by using the SetParent windows API but would prefer that my userform not be restricted inside the parent application window border. Basically I would like my form to behave just like a VBA form.
I have considered creating an out process to monitor the parent application window and control my userform based on its activation state, but this seems overly complicated.
I would appreciate any help anyone can offer.
Don''t do it! By using VB.NET, you are partially entering the world of applications behaving consistently and according to proper UI style.
In .NET, child-parent relationships between forms are intentionally rendered defunct. AForm
is aControl
, and forControls
, you can make another aControl
a parent; this is a basis of control hierarchy. However, it you would-be-a-childControl
is aForm
, exception will be thrown, for a good reason.
Now, you can override this behavior by setting the propertyForm.TopLevel
to true. It will make this form able to become a child, but — don''t do it!. You will getg an ugly result of the form, with all its non-client areas, "restricted inside the parent application window border".
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.toplevel.aspx[^].
To achieve proper UI look and behavior, do the following: make what you has as a candidate for a childForm
a non-FormControl
, typicallyPanel
. If can host all the content youForm
had. And then, make thisPanel
a child of someForm
or anotherPanel
. Are you getting the point? Ideally, all your application can be just one main form, and all the rest would be the children of this form. Another good but simple possibility is using the classSystem.Windows.Forms.TabPage
which is a part ofSystem.Windows.Forms.TabControl
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.aspx[^].
In theTabControl
approach, eachTabPage
will play the role of what you pictured as a child form.
And finally, one big warning! One other solution is MDI, MDI parents and MDI children.Don''t go this way, stay out of trouble and avoid scaring off your customers.
Some motivation here:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].
Also, some motivation in my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].
Good luck, stay out of trouble,
—SA
The problem was easier than I thought. I just needed to write a class to convert an IntPtr to IWin32Window
Public Class WindowWrapper Implements System.Windows.Forms.IWin32Window Private _hwnd As IntPtr Public Sub New(ByVal handle As IntPtr) _hwnd = handle End Sub Public ReadOnly Property Handle() As IntPtr Implements System.Windows.Forms.IWin32Window.Handle Get Return _hwnd End Get End Property End Class
Then implement it when I show my userform. I added this line to the end of my "New" sub in the form class:
Me.Show(New WindowWrapper(Application.Hwnd)
这篇关于使VB .net userform的行为与VBA类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!