Results 1 to 21 of 21

Thread: [VB] CWindows+CWindow - classes wrapping windows-related API functions

Threaded View

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow [VB] CWindows+CWindow - classes wrapping windows-related API functions

    This arose from this thread where WiseJack asked:
    Quote Originally Posted by WiseJack
    Isn't there a nice class I could get from somewhere that encapsulates all the dirty work and provides a humanisitic collection of all windows on the screen just like, for example, the controls collection of a VB form?
    Well, I couldn't think of one (Merrion later provided a link to EventVB.dll) so I decided to write this. It has two classes and a module and wraps a lot of common windows-related API functions.


    CWindows

    This is a collection class which contains all the top-level windows returned through the EnumWindows() API. Just call .GetWindows() to populate the collection. You can enumerate all the windows by using a For Each syntax (thanks brucevde).
    VB Code:
    1. Dim objWindows   As CWindows
    2. Dim objWindow    As CWindow
    3.  
    4. objWindows.GetWindows
    5.  
    6. For Each objWindow In objWindows
    7.     ' do something
    8. Next objWindow

    It contains some other functions also:
    • WindowFromHWND - Returns a CWindow object for the hWnd provided, from the collection (so you need to call .GetWindows() first).
    • WindowFromCaption - Returns a CWindow object for the window caption provided, from a call to FindWindow() (no need to call .GetWindows() first).
    • WindowFromClass - Same as WindowFromCaption, but with class name instead.
    • WindowFromClassAndCaption - Both (duh).
    • HWNDs - Returns an array of hWnds for all top-level windows.
    • DesktopWindow - Returns the hWnd of the desktop window.
    • ForegroundWindow - Returns the hWnd of the current foreground window.
    • ShellWindow - Returns the hWnd of the "shell" window, from GetShellWindow() - I don't actually know what this is.



    CWindow

    This class contains all the window-related properties etc. A CWindow object returned by a function in CWindows will have its information populated already. One you create yourself will need to be "attached" to a window by calling .GetInfo(hWnd), e.g.:
    VB Code:
    1. ' From within a form
    2. Dim objWindow    As CWindow
    3. objWindow.GetInfo Me.hWnd
    4.  
    5. ' Now you can use objWindow to access extended properties/methods of the form, described below.
    6.  
    7. ' Don't forget to Set it to Nothing when you've finished.
    8. Set objWindow = Nothing

    It contains the following properties (read/write unless marked otherwise):
    • AlphaEnabled - Boolean, whether alpha-blending is enabled.
    • AlphaLevel - Byte, level of opacity from 0 to 255 where 255 is opaque and 0 is transparent.
    • AlwaysOnTop - Boolean, whether the window is currently set as always on top.
    • Ancestor - Returns a CWindow object of the window's root ancestor (from GetAncestor() which walks the chain of windows by calling GetParent() until it finds the root window).
    • Left, Top, Right, Bottom, Width, Height - Longs, window coordinates and size (in pixels).
    • Caption - String, window caption.
    • Children - A collection of CWindow objects for each of the window's child windows. Must be populated first by a call to .GetChildren().
    • ChildWindow - Returns a CWindow object from the collection of child windows, for an index supplied. Must call .GetChildren() first.
    • Classname - String, class name of the window.
    • CloseButton - Boolean (read-only), whether the window has a close button.
    • ColourKey - Long, RGB value for the alpha colour key of the window. This is a colour that is made transparent (if you use it).
    • ColourKeyEnabled - Boolean, whether colour key transparency is enabled or not.
    • hAppInstance - Handle to the application instance of the window. Read-only.
    • hWnd - Window handle, read-only.
    • Layered - Boolean, whether the window is a "layered" window or not (WS_EX_LAYERED). This must be True, if you want to use alpha blending (with or without a colour key).
    • MaxButton - Boolean (read-only), whether the window has a maximize button.
    • MinButton - Same, for the minimize button.
    • ModuleFileName - This is supposed to return a string containing the file name of the application the window belongs to. However, in practice, it doesn't work very often. Read-only.
    • Parent - Returns a CWindow object of the window's parent (hWnd from a call to GetParent()).
    • Subclassed - Boolean, whether the window has been subclassed or not by using .Subclass/.Unsubclass. Read-only.
    • TopMostChild - Returns a CWindow object of the window's top-most child window.


    It also contains the following methods:
    • Activate - Sets the window as the foreground window (gives it focus).
    • CloseWindow - Attempts to close the window, by sending a WM_CLOSE message.
    • FindChildByCaption - If succesful, returns a CWindow object for the child window with the caption passed.
    • FindChildByClassName - Same, but with class name instead.
    • GetChildren - Call this to populate the .Children collection of child CWindow objects.
    • GetClientDCHandle - Returns a handle to the DC of the window's client area. You must release this when you have finished by using the ReleaseDC() API.
    • GetWindowDCHandle - Returns a handle to the DC of the window (including the non-client area). You have to release this too.
    • Hide - Hides the window (would you believe).
    • PostWindowMessage - Posts a message to the window by using PostMessage().
    • QuitAppplication - Posts a WM_QUIT message to try to get the application to quit.
    • Refresh - Refreshes the client area of the window by calling UpdateWindow().
    • SendWindowMessage - Send a message to the window using SendMessage().
    • SetText - Sends a WM_SETTEXT message. Useful for textboxes.
    • Show - Show the window, you can optionally pass a show state as well.
    • Subclass - Subclass the window using a WndProc address you supply. For this to work, the window needs to be in the calling thread.
    • Unsubclass - Undoes the work of .Subclass().
    • ZOrder - Just like a VB control's .ZOrder method, you can vbBringToFront or you can vbSendToBack.



    MCallbacks

    Just a small module containing the EnumWindowsProc function (which incidentally contains a demo of how to create an collection object reference from a pointer).


    Attachment

    The attachment contains the two classes, the module, in a project Test.vbp, which doesn't do much, but has a form with a bit of code demonstrating how to use the classes. It also has some commented out code that you can try in the Immediate window, mainly working with a Notepad window.


    I hope this is useful for someone If you spot any bugs, or think of anything you'd like me to add to this, just post here, or PM me, and I will do my best

    Attached Files Attached Files
    Last edited by penagate; Aug 20th, 2005 at 03:16 AM.

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