只是为了好玩,我想从 excel-vba 调用 GDI (win32) 绘图函数。以下是我的 dll 函数声明。这些都是从win32导入的。
Public Declare Function GetDC _
Lib "user32.dll" _
(ByVal handle As Long) As Long
Public Declare Function MoveToEx _
Lib "gdi32.dll" _
(ByVal handle As Long, ByVal x As Integer, ByVal y As Integer, ByVal lppoint As Long) As Integer
Public Declare Function LineTo _
Lib "gdi32.dll" _
(ByVal handle As Long, ByVal x As Integer, ByVal y As Integer) As Integer
Public Declare Function ReleaseDC _
Lib "user32.dll" _
(ByVal hwnd As Long, ByVal hdc As Long) As Integer
Public Declare Function GetSystemMetrics _
Lib "user32.dll" _
(ByVal i As Integer) As Integer
我希望得到的结果是从屏幕左上角到右下角的一条线。以下代码给出了所需的结果。
Private Sub CommandButton1_Click()
Dim dc As Long
dc = GetDC(0)
screenX = GetSystemMetrics(0)
screenY = GetSystemMetrics(1)
MoveToEx dc, 0, 0, 0
LineTo dc, screenX, screenY
ReleaseDC 0, dc
End Sub
但问题是下面的代码没有做任何事情。为什么?
Private Sub CommandButton1_Click()
Dim dc As Long
Dim screenX, screenY As Integer
dc = GetDC(0)
screenX = GetSystemMetrics(0)
screenY = GetSystemMetrics(1)
MoveToEx dc, 0, 0, 0
LineTo dc, screenX, screenY
ReleaseDC 0, dc
End Sub
唯一的区别是在第二个代码中,第三行声明了变量screenX
,screenY
而在第一个代码中,它们没有被声明。谁能解释发生了什么?