|
'共6个文件
'工程文件 ClassWindow.vbp
Type=Exe
Module=ModuleMain; ModuleMain.bas
Class=Class_Form; Class_Form.cls
Class=Class_Screen; Class_Screen.cls
Module=ModuleTrusteeship; ModuleTrusteeship.bas
Class=Class_Main; Class_Main.cls
Startup="Sub Main"
HelpFile=""
Title="ClassWindow"
ExeName32="ClassWindow.exe"
Command32=""
Name="ClassWindow"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="FREE"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=-1
CodeViewDebugInfo=0
NoAliasing=-1
BoundsCheck=-1
OverflowCheck=-1
FlPointCheck=-1
FDIVCheck=-1
UnroundedFP=-1
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
'标准模块 ModuleTrusteeship.bas
Attribute VB_Name = "ModuleTrusteeship"
'托管模块
Option Explicit
'结构体
Private Type WNDCLASS '窗体结构
style As Long
lpfnwndproc As Long
cbClsextra As Long
cbWndExtra2 As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
End Type
Private Type POINTAPI '坐标结构
x As Long
y As Long
End Type
Private Type Msg '消息结构
hWnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
'API函数
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" () As String
Private Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function GetMessage Lib "user32.dll" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Private Declare Function TranslateMessage Lib "user32.dll" (lpMsg As Msg) As Long
Private Declare Function DispatchMessage Lib "user32.dll" Alias "DispatchMessageA" (lpMsg As Msg) As Long
Private Declare Function DefWindowProc Lib "user32.dll" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
'属性
Public CommandLine As String '命令行
Public hInstance As Long '实例
Public ErrDescription As String '错误描述
'事件托管窗体
Private IForm As Class_Form
'托管函数
Public Function Trusteeship(ByRef EventForm As Class_Form) As Boolean
'类实例化
Set IForm = EventForm
hInstance = GetModuleHandle(vbNull) '获取模块句柄
CommandLine = GetCommandLine() '获取命令行参数
Const WinClassName = "MyWinClass" '定义窗口类名
Dim WC As WNDCLASS '设置窗体参数
With WC
.hIcon = 0 '窗体图标 使用 LoadIcon(hInstance, ID) 加载RES图标
.hCursor = 0 '窗体光标 使用 LoadCursor(hInstance, ID) 加载RES光标
.lpszMenuName = vbNullString '窗体菜单 使用 LoadMenu(hInstance,ID) 加载RES菜单
.hInstance = hInstance '实例
.cbClsextra = 0
.cbWndExtra2 = 0
.style = 0
.hbrBackground = 16
.lpszClassName = WinClassName '类名
.lpfnwndproc = GetAddress(AddressOf WinProc) '消息函数地址
End With
'注册窗体类
If RegisterClass(WC) = 0 Then ErrDescription = "RegisterClass Faild.": Exit Function
'获取窗体句柄
With IForm
.hWnd = CreateWindowEx(0&, WinClassName, .Caption, .WindowStyle, .Left, .Top, .Width, .Height, 0, 0, hInstance, ByVal 0&)
If .hWnd = 0 Then ErrDescription = "CreateWindowEx Faild.": Exit Function
.hDC = GetDC(.hWnd) '获取窗体GDI句柄
.Visible = True '显示窗体
Dim WinMsg As Msg '消息结构
'消息循环
Do While GetMessage(WinMsg, .hWnd, 0, 0) > 0
TranslateMessage WinMsg
DispatchMessage WinMsg
DoEvents
Loop
End With
'返回值
Trusteeship = True
End Function
'窗体过程
Private Function WinProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_CREATE = &H1
Const WM_COMMAND = &H111
Const WM_CLOSE = &H10
Const WM_MOUSEMOVE = &H200
Const WM_SIZE = &H5
Dim bRet As Boolean '取返回值
With IForm
Select Case wMsg
Case WM_CREATE
Call .ICreate
Case WM_COMMAND
Call .ICommand(wParam, lParam)
Case WM_CLOSE
Call .IUnload(bRet)
If bRet = True Then Exit Function
DestroyWindow .hWnd '销毁窗体
Case WM_MOUSEMOVE
Call .IMouseMove(LoWord(lParam), HiWord(lParam))
Case WM_SIZE
Call .IResize
Case Else
WinProc = DefWindowProc(hWnd, wMsg, wParam, lParam)
End Select
End With
End Function
'取地址
Private Function GetAddress(Address) As Long
GetAddress = Address
End Function
'低字
Private Function LoWord(ByVal DWord As Long) As Integer
If DWord And &H8000& Then
LoWord = DWord Or &HFFFF0000
Else
LoWord = DWord And &HFFFF&
End If
End Function
'高字
Private Function HiWord(ByVal DWord As Long) As Integer
HiWord = (DWord And &HFFFF0000) \ 65536
End Function |
|