我想在VBA 7.0中创建一个无模式弹出对话框。
到目前为止,最有希望的路线似乎是CreateDialog
。
首先我尝试CreateDialogW
并收到Entry point not found for CreateDialogW in DLL
打开DLL后,我验证了此功能未列出。上面链接的MSDN参考显示User32作为此函数的DLL并列出函数名CreateDialogW
和CreateDialogA
(分别为Unicode / ansi),但它们未在我的计算机上的此DLL中列出(Win 7专业版) ,64位)。
因此,查看DLL中 的函数列表,我看到CreateDialogParam
and CreateDialogIndirectParam
functions(每个的Ansi和Unicode版本)。
我一直在尝试关注MSDN并将C示例转换为VB,但是我在某处遗漏了某些内容而且因为我不知道自己做错了什么而陷入困境。代码编译并运行没有错误,但API调用没有任何反应 - 它执行但没有任何反应。
如果有人能给我一些正确方向的指示,我将非常感激。 我当前的解决方法很糟糕,我真的很想按下这个项目。
Option Explicit
'Reference conversion of C to VB type declarations here
'http://msdn.microsoft.com/en-us/library/aa261773(v=vs.60).aspx
'Declare function to Win API CreateDialog function
'http://msdn.microsoft.com/en-us/library/ms645434(v=vs.85).aspx
Private Declare PtrSafe Function CreateDialog Lib "User32.dll" Alias "CreateDialogParamW" _
(ByVal lpTemplateName As LongPtr, _
ByRef lpDialogFunc As DIALOGPROC, _
ByVal dwInitParam As Long, _
Optional ByVal hInstance As Long, _
Optional ByVal hWndParent As Long) _
As Long
'Windows Style Constants
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
Public Const WS_BORDER As Long = &H800000
Public Const WS_CAPTION As Long = &HC00000
Public Const WS_CHILD As Long = &H40000000
Public Const WS_CHILDWINDOW As Long = &H40000000
Public Const WS_CLIPCHILDREN As Long = &H2000000
Public Const WS_CLIPSIBLINGS As Long = &H4000000
Public Const WS_DISABLED As Long = &H8000000
Public Const WS_DLGFRAME As Long = &H400000
Public Const WS_GROUP As Long = &H20000
Public Const WS_HSCROLL As Long = &H100000
Public Const WS_ICONIC As Long = &H20000000
Public Const WS_MAXIMIZE As Long = &H1000000
Public Const WS_MAXIMIZEBOX As Long = &H10000
Public Const WS_MINIMIZE As Long = &H20000000
Public Const WS_MINIMIZEBOX As Long = &H20000
Public Const WS_OVERLAPPED As Long = &H0
Public Const WS_POPUP As Long = &H80000000
Public Const WS_SIZEBOX As Long = &H40000
Public Const WS_SYSMENU As Long = &H80000
Public Const WS_TABSTOP As Long = &H10000
Public Const WS_THICKFRAME As Long = &H40000
Public Const WS_TILED As Long = &H0
Public Const WS_VISIBLE As Long = &H10000000
Public Const WS_VSCROLL As Long = &H200000
Public Const WS_OVERLAPPEDWINDOW As Long = (WS_OVERLAPPED + WS_CAPTION + WS_SYSMENU + WS_THICKFRAME + WS_MINIMIZEBOX + WS_MAXIMIZEBOX)
Public Const WS_TILEDWINDOW As Long = (WS_OVERLAPPED + WS_CAPTION + WS_SYSMENU + WS_THICKFRAME + WS_MINIMIZEBOX + WS_MAXIMIZEBOX)
Public Const WS_POPUPWINDOW As Long = (WS_POPUP + WS_BORDER + WS_SYSMENU)
'Declare custom type for lpDialogFunc argument
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms645469(v=vs.85).aspx
Public Type DIALOGPROC
hwndDlg As Long
uMsg As LongPtr
wparam As Long
lparam As Long
End Type
'MAKEINTRESOURCE Macro emulation
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms648029(v=vs.85).aspx
'Bitwise function example found here: http://support.microsoft.com/kb/112651
'VB conversion found here: https://groups.google.com/forum/#!topic/microsoft.public.vb.winapi/UaK3S-bJaiQ _
modified with strong typing and to use string pointers for VB7
Private Function MAKEINTRESOURCE(ByVal lID As Long) As LongPtr
MAKEINTRESOURCE = StrPtr("#" & CStr(MAKELONG(lID, 0)))
End Function
Private Function MAKELONG(ByRef wLow As Long, ByRef wHi As Long)
'Declare variables
Dim LoLO As Long
Dim HiLO As Long
Dim LoHI As Long
Dim HiHI As Long
'Get the HIGH and LOW order words from the long integer value
GetHiLoWord wLow, LoLO, HiLO
GetHiLoWord wHi, LoHI, HiHI
If (wHi And &H8000&) Then
MAKELONG = (((wHi And &H7FFF&) * 65536) Or (wLow And &HFFFF&)) Or &H80000000
Else
MAKELONG = LoLO Or (&H10000 * LoHI)
'MAKELONG = ((wHi * 65535) + wLow)
End If
End Function
Private Function GetHiLoWord(lparam As Long, LOWORD As Long, HIWORD As Long)
'This is the LOWORD of the lParam:
LOWORD = lparam And &HFFFF&
'LOWORD now equals 65,535 or &HFFFF
'This is the HIWORD of the lParam:
HIWORD = lparam \ &H10000 And &HFFFF&
'HIWORD now equals 30,583 or &H7777
GetHiLoWord = 1
End Function
Public Function TstDialog()
Dim dpDialog As DIALOGPROC
dpDialog.hwndDlg = 0
dpDialog.uMsg = StrPtr("TEST")
dpDialog.lparam = 0
dpDialog.wparam = 0
CreateDialog hInstance:=0, lpTemplateName:=MAKEINTRESOURCE(WS_POPUPWINDOW + WS_VISIBLE), lpDialogFunc:=dpDialog, dwInitParam:=&H110
End Function
答案 0 :(得分:7)
我不想贬低深度和深入研究,但有可能的解决方法在VBA中动态创建无模式对话框。在提问者用CreateDialog
勇敢地潜入兔子洞之前,这是最初的问题。所以这个答案是针对在VBA中动态创建无模式对话框而不是如何使用CreateDialog
的原始问题。我无法帮助那里。
如前所述,可以使用UserForm创建无模式对话框,但我们不希望无用的表单乱丢项目。我已经实现的解决方法使用Microsoft VBA可扩展性库。简而言之,我们创建了一个类,它在构造时为项目添加通用用户表单,并在终止时删除用户表单。
另请注意,这是使用Excel VBA测试的。我没有SolidWorks,所以我无法在那里进行测试。
粗略地完成作为课程模块。
Option Explicit
Private pUserForm As VBIDE.VBComponent
Private Sub Class_Initialize()
' Add the userform when created '
Set pUserForm = ThisWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ct_MSForm)
End Sub
Private Sub Class_Terminate()
' remove the userform when instance is deleted '
ThisWorkbook.VBProject.VBComponenets.Remove pUserForm
End Sub
Public Property Get UserForm() As VBIDE.VBComponent
' allow crude access to modify the userform '
' ideally this will be replaced with more useful methods '
Set UserForm = pUserForm
End Property
Public Sub Show(ByVal mode As Integer)
VBA.UserForms.Add(pUserForm.Name).Show mode
End Sub
理想情况下,这个类会更好地开发,并且可以更容易地访问修改表单,但是现在它是一个解决方案。
Private Sub TestModelessLocal()
Dim localDialog As New Dialog
localDialog.UserForm.Properties("Caption") = "Hello World"
localDialog.Show vbModeless
End Sub
当localDialog
离开范围时,您应该看到一个窗口出现并消失。在您的VBProject中创建了UserForm1
并将其删除。
此测试将创建一个持久对话框。不幸的是,UserForm1
将保留在您的VBProject中,因为仍然定义了globalDialog
。重置项目不会删除用户表单。
Dim globalDialog As Dialog
Private Sub TestModeless()
Set globalDialog = New Dialog
globalDialog.UserForm.Properties("Caption") = "Hello World"
globalDialog.Show vbModeless
'Set globalDialog = Nothing closes window and removes the userform '
'Set gloablDialog = new Dialog should delete userform1 after added userform2'
End Sub
所以永远不要在模块范围内使用它。
总之,它是一个丑陋的解决方案,但它远不如Asker试图做的那么难看。
答案 1 :(得分:6)
这可以使用,但如果你应该尝试让它工作是另一个问题。我有一个显示空对话框的工作版本。今晚我没有时间再完成对话框的实际控制,但是我发帖希望它能让你开始。
首先,您需要忘记CreateDialog,因为它们需要对话框模板位于资源部分。您可以使用CreateDialogIndirectParam从内存中对话框模板创建对话框。你需要这个:
Private Type DLGTEMPLATE
style As Long
dwExtendedStyle As Long
cdit As Integer
x As Integer
y As Integer
cx As Integer
cy As Integer
End Type
Private Type DLGITEMTEMPLATE
style As Long
dwExtendedStyle As Long
x As Integer
y As Integer
cx As Integer
cy As Integer
id As Integer
End Type
Private Type DLG
dlgtemp As dlgtemplate
menu As Long
classname As String
title As String
End Type
Private Declare PtrSafe Function CreateDialogIndirectParam Lib "User32.dll" Alias "CreateDialogIndirectParamW" _
(ByVal hInstance As Long, _
ByRef lpTemplate As DLGTEMPLATE, _
ByVal hWndParent As Long, _
ByVal lpDialogFunc As LongPtr, _
ByVal lParamInit As Long) _
As LongPtr
Const WM_INITDIALOG As Long = &H110
Const DS_CENTER As Long = &H800&
Const DS_SETFONT As Long = &H40
Const DS_MODALFRAME As Long = &H80
Const WS_EX_APPWINDOW As Long = &H40000
然后这样称呼:
Dim d As DLG
d.dlgtemp.style = DS_MODALFRAME + WS_POPUP + WS_VISIBLE + WS_CAPTION + WS_SYSMENU
d.dlgtemp.dwExtendedStyle = WS_EX_APPWINDOW
d.dlgtemp.cdit = 0
d.dlgtemp.x = 100
d.dlgtemp.y = 100
d.dlgtemp.cx = 200
d.dlgtemp.cy = 200
d.menu = 0
d.title = "Test"
d.classname = "Test"
CreateDialogIndirectParam 0, d.dlgtemp, 0, AddressOf DlgFunc, 0
与DlgFunc看起来像这样:
Public Function DlgFunc(ByVal hwndDlg As LongPtr, ByVal uMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
If uMsg = h110 Then ' = WM_INITDIALOG - you should make a const for the various window messages you'll need...
DlgFunc = True
Else
DlgFunc = False
End If
End Function
自从我上次做这些事以来已经十多年了。但是,如果您决定采用这种方式,我认为这种方法最有前途 - 下一步是调整DLG结构以添加一些DLGITEMTEMPLATE成员,将d.dlgtemp.cdit设置为您的控件数量对话框,并开始处理DlgFunc中的控制消息。
答案 2 :(得分:4)
你在这个项目上的开局很差。你完全扰乱了CreateDialogParam的参数顺序,注意hInstance
参数是第一个,dwInitParam
参数是最后一个。
你完全弄错了DIALOGPROC声明,它是一个函数指针。这需要声明中的LongPtr
和拨打电话时AddressOf
运算符。
这只是让它发挥作用的前1%。接下来的问题是您必须编写一个功能对话框程序(AddressOf
的目标)来处理对话框生成的通知。基本的东西,比如识别用户点击了确定按钮。当你对WinAPI编程知之甚少时很难写,在运行时几乎没有错误是很大的不可诊断的问题。
这只是小事,还有很多更大的问题。 lpTemplateName
论证是一个非常严重的障碍。这需要是一个资源标识符,由" rc.exe"生成的类型。并由链接器添加到可执行文件中。您无法重新链接SolidWorks。无模式对话需要消息循环的帮助,它必须调用IsDialogMessage()
。您无法说服SolidWorks为您拨打此电话。如果没有它,对话就会在难以诊断的方式上行为不端,就像标签不起作用一样。
你必须知道什么时候你绝对没有机会让它发挥作用。你不能让它发挥作用。
答案 3 :(得分:3)
此答案与Cheezsteak's一样,并未直接处理您与CreateDialog
所遇到的问题。它解决了创建无模式对话框的最终目标。
我的建议是使用UserForm来完成此任务。它是Show Method一个可选参数,用于确定用户表单是否显示为模态或无模式。
来自MSDN文档:
modal可选。确定UserForm是否为模态的布尔值 或无模式。
在创建UserForm实例的代码中,只需将vbModeless
常量传递给它。
Option Explicit
Private frm As UserForm1
Sub test2()
Set frm = New UserForm1
frm.Show vbModeless
End Sub
如果您担心使用表单混乱您的项目,请不要这样做。只需create the form on the fly。