Why this doesn't work !!!???
Hi ,
I am trying to draw the image of the NotePad Window on an Excel UserForm DC via the PrintWindow API function But the code below doesn't work :mad:
Can anyone spot where the problem is or if I am missing something ?
VB Code:
Private Declare Function PrintWindow Lib "user32" _
(ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Dim lhwnd, hdc, mWnd As Long
Private Sub UserForm_Initialize()
lhwnd = FindWindow(vbNullString, Me.Caption)
hdc = GetDC(lhwnd)
'KPD-Team 2001
'URL: [url]http://www.allapi.net/[/url]
'launch notepad
Shell "notepad.exe", vbNormalNoFocus
DoEvents
'search the handle of the notepad window
mWnd = FindWindow("Notepad", vbNullString)
If mWnd = 0 Then
MsgBox "NotePad window not found!"
Else
'draw the image of the notepad window on our form
PrintWindow mWnd, hdc, 0
End If
End Sub
Private Sub UserForm_Terminate()
ReleaseDC lhwnd, hdc
End Sub
Any takers ?
Regards.
Re: Why this doesn't work !!!???
Dont use this code in UserForm_Initialize. This is before form shows up. Use it for example in UserForm_Click().
Re: Why this doesn't work !!!???
3 things to change, just give it a try:
firstly: cut the code from under Initialize event and paste it in either UserForm_Load event or Under _click event as suggested by bilm_ks.
second, in VB Dim lhwnd, hdc, mWnd As Long lhwmd and hdc are not of Long type. in VB, they are now Variant type. Use
VB Code:
Dim lhwnd As Long, hdc As Long, mWnd As Long
and Third, hdc is a native variable name, so you cannot use it. instead use
VB Code:
Dim lhwnd As Long, [hl]myhdc[/hl] As Long, mWnd As Long
(replace all hdc with mhdc in your code)
Harsh Gupta