Results 1 to 3 of 3

Thread: Why this doesn't work !!!???

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    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

    Can anyone spot where the problem is or if I am missing something ?


    VB Code:
    1. Private Declare Function PrintWindow Lib "user32" _
    2. (ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
    3.  
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    5. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6.  
    7. Private Declare Function ReleaseDC Lib "user32" _
    8. (ByVal hwnd As Long, ByVal hdc As Long) As Long
    9.  
    10. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    11.  
    12. Dim lhwnd, hdc, mWnd As Long
    13.  
    14.  
    15. Private Sub UserForm_Initialize()
    16.  
    17.     lhwnd = FindWindow(vbNullString, Me.Caption)
    18.     hdc = GetDC(lhwnd)
    19.  
    20.     'KPD-Team 2001
    21.     'URL: [url]http://www.allapi.net/[/url]
    22.     'E-Mail: [email][email protected][/email]
    23.     'launch notepad
    24.     Shell "notepad.exe", vbNormalNoFocus
    25.     DoEvents
    26.  
    27.     'search the handle of the notepad window
    28.     mWnd = FindWindow("Notepad", vbNullString)
    29.     If mWnd = 0 Then
    30.        MsgBox "NotePad window not found!"
    31.     Else
    32.         'draw the image of the notepad window on our form
    33.         PrintWindow mWnd, hdc, 0
    34.     End If
    35.  
    36.  
    37. End Sub
    38.  
    39. Private Sub UserForm_Terminate()
    40.  
    41.     ReleaseDC lhwnd, hdc
    42.  
    43. End Sub

    Any takers ?

    Regards.

  2. #2
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    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().
    "bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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:
    1. 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:
    1. Dim lhwnd As Long, [hl]myhdc[/hl] As Long, mWnd As Long
    (replace all hdc with mhdc in your code)

    Harsh Gupta
    Last edited by Harsh Gupta; Jan 30th, 2006 at 07:58 AM.
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width