日期:2012-12-16  浏览次数:20373 次

第一次使用VB,想做一个堆叠对话框的程序。
就是想标签控件那种,但是不想把每个页上的控件都放在一个主控件里。想把每个页都使用一个Form或PropertyPage来组织。
经过大量的实验和CSDN上同仁的友情帮助,略有小成,不敢独享,放在这里,抛砖引玉。希望大家还有更好的办法。

Step 1 声明如下说明
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) 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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Step 2 在主窗体上放置Frame控件(fraContainer), 以放置堆叠页面的容器
Step 3 建立函数InitializePage
Private Sub InitializePage(frmPage As Form, hWndParent As Long)
Dim dwStyle As Long

Load frmPage

dwStyle = GetWindowLong(frmPage.hwnd, -16)
dwStyle = dwStyle Or &H40000000
SetWindowLong frmPage.hwnd, -16, dwStyle

SetParent frmPage.hwnd, hWndParent
End Sub
Step 4 建立需要切换的页面Form1 和 Form2, 修改为无边框,并将相应的起始属性(StartUpPosition)改为Manual,Left Top 均为0
Step 5 建立页初始化函数
Public Sub InitializePages()
Set frm1 = New Form1
Set frm2 = New Form2

InitializePage frm1, frmMain.fraContainer.hwnd
InitializePage frm2, frmMain.fraContainer.hwnd
End Sub
Step 6 建立全局变量frmActive 保存当前活动的页面
Step 7 建立切换页面切换函数
Public Sub ChangePage(frmChange as Form)
If frmChange Is Nothing Then Exit Sub

If Not frmActive Is Nothing Then
If frmActive.hWnd <> frmChange.hWnd Then
frmActive.Hide
frmChange.Show 0, Me
Set frmActive = frmChange
End If
Else
frmChange.Show 0, Me
Set frmActive = frmChange
End If
End Sub

Step 8 在主页面的Unload 中加入如下代码
Unload frm1
Unload frm2

Set frm1 = Nothing
Set frm2 = Nothing

如此一个可以切换两个页面的代码完成,不过还有一些问题,
1. 在每个页面切入切出时没有相应的处理函数。
2. 页面没有焦点。

对于切入切出的问题,本来想用如下方式,从Form中派生一个PropertyForm类,然后加入OnSetActive 和 OnKillActive函数,然后所有页面从此类生成,但是不知道怎么做,呵呵。VB的功能还是弱了点。C++我可以为所欲为,VB不会就没有一点办法。:)
希望VB高手可以指教