如何给VB.NET窗体添加子窗体?

如何给VB.NET窗体添加子窗体?,第1张

直接添加一个MID父窗体或在已有窗体的属性中找到IsMDIContainer属性,然后设置为True,然后创建第二个窗体 ,需要加载子窗体的时候:

Dim NewMDIChild As New Form2

NewMDIChild.MdiParent = Me

NewMDIChild.Show()

Public Shared Sub CheckMDIChildForm(ByVal MDIForm As Windows.Forms.Form, ByVal MDIChildForm As Windows.Forms.Form, ByVal MDIChildFormName As String)

        If MDIForm.MdiChildren.Length < 1 Then

            '如果没有任何一个MDI子窗体,则创该MDI子窗体的窗体实例

            Dim MDIChildFrm As Windows.Forms.Form = MDIChildForm ' 定义MDI子窗体

            MDIChildFrm.MdiParent = MDIForm '指定父窗体

            MDIChildFrm.Show() '打开窗体

            Exit Sub

        Else

            Dim x As Integer

            Dim frmyn As Boolean

            For x = 0 To (MDIForm.MdiChildren.Length) - 1

                Dim tempChild As Windows.Forms.Form = CType(MDIForm.MdiChildren(x), Windows.Forms.Form)

                If tempChild.Name = MDIChildFormName Then

                    '检测到有该MDI子窗体,设为激活 并退出循环

                    frmyn = True

                    tempChild.BringToFront()

                    Exit For

                Else

                    frmyn = False

                End If

            Next

            If Not frmyn Then

                '在打开的窗体中没检测到则新建

                Dim MDIChildFrm As Windows.Forms.Form = MDIChildForm ' 定义MDI子窗体

                MDIChildFrm.MdiParent = MDIForm '指定父窗体

                MDIChildFrm.Show() '打开窗体

            End If

        End If

    End Sub

我做了几行。

对无标题栏的窗体,或者不从标题栏移动的情况下可用。

有标题栏并且点住标题栏移动则不理想。

代码如下,仅供参考。

1、建立模块。

option

explicit

public

oldproc

as

long

public

declare

function

setwindowlong

lib

"user32"

alias

"setwindowlonga"

(byval

hwnd

as

long,

byval

nindex

as

long,

byval

dwnewlong

as

long)

as

long

public

declare

function

sendmessage

lib

"user32"

alias

"sendmessagea"

(byval

hwnd

as

long,

byval

wmsg

as

long,

byval

wparam

as

long,

byref

lparam

as

any)

as

long

public

declare

function

callwindowproc

lib

"user32"

alias

"callwindowproca"

(byval

lpprevwndfunc

as

long,

byval

hwnd

as

long,

byval

msg

as

any,

byval

wparam

as

any,

byval

lparam

as

any)

as

long

public

const

wm_move

=

&h3

public

const

wm_lbuttondown

=

&h201

public

const

wm_lbuttonup

=

&h202

public

const

gwl_wndproc

=

(-4)

public

bnhwnd

as

long

public

function

newproc(byval

hwnd

as

long,

byval

msg

as

long,

byval

wp

as

long,

byval

lp

as

long)

as

long

if

msg

=

wm_move

then

'捕获窗体移动事件

sendmessage

bnhwnd,

wm_lbuttondown,

1,

0

sendmessage

bnhwnd,

wm_lbuttonup,

1,

0

'将消息发送到按纽,使其发生click事件

end

if

newproc

=

callwindowproc(oldproc,

hwnd,

msg,

wp,

lp)

end

function

2、在窗体上有一个按钮(名称为eventbn)

dim

垂直

as

single,

水平

as

single

private

sub

eventbn_click()

msgbox

"移动了"

end

sub

private

sub

form_load()

form2.show

form3.show

bnhwnd

=

eventbn.hwnd

'获得按纽句丙

oldproc

=

setwindowlong(me.hwnd,

gwl_wndproc,

addressof

newproc)

eventbn.visible

=

false

end

sub

private

sub

form_mousedown(button

as

integer,

shift

as

integer,

x

as

single,

y

as

single)

if

button

=

1

then

原垂直

=

me.top

+

y

原水平

=

me.left

+

x

end

if

end

sub

private

sub

form_mouseup(button

as

integer,

shift

as

integer,

x

as

single,

y

as

single)

me.top

=

me.top

-

原垂直

+

y

me.left

=

me.left

-

原水平

+

x

form2.top

=

form2.top

-

原垂直

+

y

form2.left

=

form2.left

-

原水平

+

x

form3.top

=

form3.top

-

原垂直

+

y

form3.left

=

form3.left

-

原水平

+

x

end

sub

3、其他窗体基本都这样。

如果是VB.NET

那么可以直接从一个父窗体类继承,例如

Dim

NewFrm

as

New

Form1

NewFrm.Show

这样可以为一个窗体创建N个相同的子类,并且通过类的方法覆盖,可以与父类有所不同


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11906977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存