Results 1 to 8 of 8

Thread: API equivalent of .Cls?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    API equivalent of .Cls?

    Hi,

    I'm trying to figure out the equivalent of the .Cls function in VB which clears the dc for which it is called but I'm not having any luck.

    I am subclassing a form and thought I could just do a PostMessage to the form's hWnd with "WM_PAINT" as the message parameter, but that doesn't seem to be enough.

    Just to confirm, I created a form with a command button on it and put the code: Me.Cls and then looked at it with Spy++ and it seems that the only message getting processed was WM_PAINT.

    So why is posting the "WM_PAINT" message not enough?

    Any help would be appreciated..

    Dan

    Visual Studio 2010

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    (oops - thought I'd already replied to this...)

    When a window recieves a WM_PAINT message it only repaints the part of the window which has been marked as needing updating. This means that screen refresh is much faster when only a small part needs refreshing.
    To force the WM_PAINT message to update the entire window you need to mark the window as needing repainting using the InvalidateRect API call.

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks! Your explanation really cleared things up. I will try it out and let you know..

    Dan

    Visual Studio 2010

  4. #4
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    Was this post timely.

    Can someone explain what's going on in the API in relationship
    to VB.

    IN VB:

    VB has a Paint Event attached to each object. Paint cannot be called directly from another form or module unless it it made
    Public. Hence the need for the API to trigger paint.


    IN API:

    From my understanding you need the following in order to call the VB Paint Event:

    Structures:

    RECT
    PAINT


    API Calls:
    SendMessage
    SetRect
    InvalidateRect
    ValidateRect
    LockWindowUpdate
    ReDrawWindow


    IS THIS THE SUEDOCODE NEEDED TO TRIGGER A CALL TO THE
    VB PAINT EVENT?

    1. SetRect to identify the area of the object to be repainted
    2. InvalidateRect to tell the OS the area is in need of repaint
    3. LockWindowUpdate to make sure Window is not accessed
    other than by me
    4. ReDrawWindow to cause a call to the Paint Event
    5. LocKWindowUpdate to make Window available to others

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    No need for SetRect - calling InvalidateRect and passing vbNull for the RECT parameter invalidates the whole window....in fact passing True for the fuRedraw parameter should force the window to repaint.

    From EventVB.ApiWindow:
    VB Code:
    1. Private Declare Function InvalidateRectByPointerApi Lib "user32" Alias "InvalidateRect" (ByVal hwnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
    2.  
    3. Public Sub Refresh()
    4.  
    5. Dim lret As Long
    6.  
    7. lret = InvalidateRectByPointerApi(m_hwnd, 0, True)
    8.  
    9. End Sub

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  6. #6
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    Duncan: Thanks for input and link.

    Passing vbNull as param to InvalidateRect generated an error but got the following code to work. BTW, passing ByVal 0& for the
    rectangle param in RedrawWindow also generated an error.
    Both calls seem to want the pointer to the RECT structure.

    NOTE: RedrawWindow also works.

    'Redraw this window (invoke a Paint-event)
    LockWindowUpdate Me.hwnd

    With Me
    SetRect r, .left, .top, .left + .Width, .top + .Height
    End With

    InvalidateRect Me.hwnd, r, True
    'RedrawWindow Me.hwnd, r, ByVal 0&, RDW_INVALIDATE
    LockWindowUpdate 0


    -------------
    Saw you had a dll (believe EventDll.dll on your web site.).

    Is it a tutorial add-in or just a VB wrap around for the API?

    Am looking for a good tutorial on dealing with Windows.
    That is, when to use SendMessage vs other API calls such as InvalidateRect, ReDrawWindows with the Rectangle etc.

    ------------

    Thanks
    David

  7. #7
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    1) Passing a NULL pointer to an API call:
    As you can see from the above example, I have aliased the declaration of InvalidateRect and instead of defining the parameter lpRect As Rect I have defined it Byval lpRect As Long.

    To pass a valid RECT to this api call use:
    Code:
    Dim rcThis As RECT
    
    lret = InvalidateRectByPointerApi(m_hwnd, VarPtr(rcThis), True)
    and to pass NULL tro it use:
    Code:
    lret = InvalidateRectByPointerApi(m_hwnd, vbNull, True)
    Notes:
    VarPtr() is a special undocumented VB function that returns the memory address of any given variable.


    2: About EventVB.dll
    The EventVB.dll is a wrapper for the Win32 APi written entirely in VB. As such it's source code useful as an example on how to use the various API calls - hoiwever to reduce complexity (and support request) the source code is included in the compiuled help. Suggest you download EventVB.chm and save it to your HD. It is searcheable so you can find API calls declarations and uses and also the classification by different classes groups related API calls.

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  8. #8
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    Duncan:

    1. Thanks for the input.
    2. Thanks for referring me to you *.chm file. I downloaded it and took a look see.

    Looks good.

    --------------------

    A SUGGESTION:

    Where my API issues are (and I ASSUME others) is not so much using each API call -- as SELECTING the correct API call(s) and putting them in the correct sequence.

    A list some of the API call as well as an explanation as why they are being used by the ACTION which needs to be accomplished would go a long way to assisting myself and others.

    Our present discussion being the case in point.

    This would be a great addition to your database and in my opinion make it OUTSTANDING.

    David.

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