Window class for external windows-VBForums
Results 1 to 15 of 15

Thread: Window class for external windows

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Lightbulb Window class for external windows

    Inside Windows, applications use "Windows". You can only access these Windows using API's and lots and lots of documentation.
    I implemented all useful and working Window API's into a single class. You can do the following with it:
    - Return all active Windows on the OS
    - Get the process of a Window (handle)
    - Set/Get text, location, size, enabled, visible, exstyles, font, window order
    - Perform commands: cut, copy, close, destroy, bringtofront,sendtoback etc.
    - Capture the Window into a bitmap
    - Set transperancy
    - Get window parent and children using a built-in Enumerator

    Code is in the attachment. It consists of 2 files:
    - API.vb: contains direct SendMessage, DefWindowProc, SendInput and key API's
    - Window.vb: the main class to use for windows. Contains API's that are used internally

    The Window class only holds a pointer (hwnd), it is not storing any background information about Children or states.
    When using the same Property more than once at the same time, it is wise to store it in a new Variable:
    Code:
    Dim w As New Window("Form1")
    
    'Good:
    Dim s As Size = w.Size
    w.Location = New Point(s.Width, s.Height)
    
    'Bad:
    w.Location = New Point(w.Size.Width, w.Size.Height)
    Note: a window is not a form, a window is everything you see. This includes textboxes, buttons, pictureboxes, labels, everything.
    It is pretty cool to enable a disabled button and move on in an app while you are actually not allowed to.

    Simple usage examples:

    Getting a window
    vb Code:
    1. 'Get the Foreground window
    2. Dim w As Window = Window.GetForeGround
    3.  
    4. 'Find a window or child:
    5. Dim w As Window = Window.Find("notepad")
    6. Dim c As Window = w.FindChild("edit")
    7.  
    8. 'Window children
    9. Dim children() As Window = w.Children()
    10. Dim childbyname As Window = w.Children("name")
    11. Dim childbypoint As Window = w.Children(Cursor.Position)
    12. Dim childbyindex As Window = w.Children(2)
    13.  
    14. 'Get all windows on the current Desktop (OS):
    15. Dim windows() As Window = Window.All
    16.  
    17. 'Get all windows on the current Desktop (OS) using the running Processes:
    18. Dim windows() As Window = Window.AllFromProcessList()
    19.  
    20. 'Get all windows belonging to a certain process:
    21. Dim p As Process = Process.GetProcessesByName("notepad").First
    22. Dim windows() As Window = Window.All(p)
    23.  
    24. 'Get the Window of one of your own Controls or Forms:
    25. Dim MyWindow As Window = Window.FromControl(Me)
    26.  
    27. 'Get the Next sibling Window above this Window:
    28. Dim nextwindow As Window = w.NextSibling
    29.  
    30. 'Get the Desktop Window on which all other Windows are drawn:
    31. Dim desktop As Window = Window.GetDesktop()
    32.  
    33. 'Get the currently focused window control:
    34. Dim focusedwindow As Window = Window.GetFocused()
    35.  
    36. 'Get the currently focused window inside another Window:
    37. 'Note that it uses the Thread ID, only one control is focused in each program.
    38. Dim focusedwindow As Window = w.FocusedWindow

    Window manipulation
    vb Code:
    1. 'Change the text:
    2. w.Text &= vbNewLine & "New line"
    3.  
    4. 'Kill the process owner:
    5. Dim p As Process = w.Process
    6. If Not IsNothing(p) Then p.Kill()
    7.  
    8. 'Toggle the enabled state:
    9. w.Enabled = w.Enabled = False
    10.  
    11. 'Move it around and resize
    12. w.Location = New Point(100, 100)
    13. w.Size = New Size(300, 200)
    14. w.Bounds = New Rectangle(100, 100, 300, 200)
    15.  
    16. 'Set the Window outside of the screen:
    17. w.Location = New Point(-w.Size.Width - 50, -w.Size.Height - 50)
    18.  
    19. 'Change the WindowState:
    20. w.WindowState = Window.State.Minimized
    21. w.WindowState = Window.State.Maximized
    22. w.WindowState = Window.State.Normal
    23.  
    24. 'Perform commands:
    25. w.Command(Window.CMD.BringToFront)
    26. w.Command(Window.CMD.SendToBack)
    27. w.Command(Window.CMD.Focus)
    28. w.Command(Window.CMD.Close)
    29.  
    30. 'Set transparency(key):
    31. w.Opacity = 128 '50%
    32. w.TransparencyKey = Color.FromKnownColor(KnownColor.Control)
    33.  
    34. 'Change (Ex)Styles:
    35. w.ExStyle(Window.ExStyles.TopMost) = True
    36. w.Style(Window.Styles.MaximizeBox) = False
    37.  
    38. 'Update (redraw) a Window after you performed a Layout change:
    39. w.Invalidate()
    40. w.Redraw(Window.RedrawFlags.UpdateNow)
    41.  
    42. 'Capturing:
    43. Dim imgw As Image = w.CaptureWindow()
    44. Dim imgc As Image = w.CaptureClient()
    45. Dim screenshot As Image = Window.GetDesktop().CaptureClient()
    46.  
    47. 'Device Context operations (using the HDC of a window):
    48. Dim DC As API.DC = w.GetDC()
    49. Dim c1 As Color = DC.GetPixel(10, 10)
    50. Dim c2 As Color = DC.GetPixel(11, 11)
    51. Dim screenshot As Bitmap = DC.ToBitmap()
    52. DC.SetPixel(5, 5, Color.Black)
    53. DC.Pixel(5, 5) = DC.Pixel(10, 10)
    54. DC.Graphics.FillRectangle(Brushes.Red, 0, 0, 100, 100)
    55. DC.Dispose()
    56.  
    57. 'Drawing on other Windows using CreateGraphics:
    58. Dim g As Graphics = w.CreateGraphics()
    59. g.FillRectangle(Brushes.Red, 10, 10, 100, 100)
    60. g.Dispose()
    61.  
    62. 'Sending keys (no focus required):
    63. w.SendKey(Keys.Control And Keys.A, Window.KeyCommand.KeyEvent.Down)
    64. w.SendKey(Keys.Control And Keys.A, Window.KeyCommand.KeyEvent.Up)
    65. w.SendChars("All text is now this")
    66. w.SendChar(".")

    Changelog:
    <15th March 2011>
    Had to update Window.vb, ExStyle enum was set on Integer while it should've been Long. Also, made sure the handle was passed as HandleRef to either 32 or 64 version of GetWindowLongPtr.
    Note: This class has been in development and is still under development, so it *could* contain some bugs here and there.

    <19th March 2011>
    Got "TransparencyKey" and "Opacity" with internal "AWL" style setting working. Also added "Name" property.
    Updated the files, no additional functions are used. They are replaced by shared functions inside the Window class. (GetWindows => Window.All)
    Might get rid of the API class as well, just keeping it since it holds non-window related messages.
    Good news: I managed to send keys, characters and Control + commands to an external minimized window using PostMessage. Only two arguments will be needed: the key code and the key state (down/up).

    <23th March 2011>
    Added "SendKey", "SendChar" and "SendString" functions to send input to other windows. Also added "Find" functions for children and all windows on the OS. API now contains an INPUT structure as well that uses SendInput.

    <28th March 2011>
    Added GetPixel, Client capture and Client bounds, plus the functions "Next/Previous/Top/BottomChild" for returning Window siblings in the Z-order.

    <3th April 2011>
    Added Input attaching, Focus functions, a DC class for handling Device Context operations, both a Window and Client DC attribute and simply added a lot of classes/functions inside the API class.
    Attached Files Attached Files
    Last edited by bergerkiller; Apr 6th, 2011 at 11:43 AM.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Window class for external windows

    Nice I made a similar thing a while ago, a Windows API library for .NET, though I didn't have quite as many Window specific functions as you so I'm sure your class will be useful to a lot of people. You can check my API library out here if you are interested: http://cjwdev.wordpress.com/2010/07/...2-1-available/
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Window class for external windows

    My initial goal was having all Windows-related functions in a single class; but there are too many.

    So I changed by goal a bit to only add those functions that are harder to make; such as those that require bit setting and custom structures.

    Quite some interesting functions in there, such as emptying the recycle bin.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Window class for external windows

    haha yeah well I went for the same strategy as you originally - just including things that are not very straight forward or that there are not already lots of examples of on the web. But then I started to just add anything I found that looked like it could be vaguely useful and wasn't already in the .NET Framework :P There's still loads more I want to add to it but just haven't had chance
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Re: Window class for external windows

    HELP!
    Right off the bat, I got numerous errors when adding Window.vb and API.vb to my project.
    It says that "Count" is not a member of System.Array.
    I'm using VB 2005 if that make a difference.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  6. #6
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,250

    Re: Window class for external windows

    Count on an Array is a LINQ method, which means you need 2008 or greater. I'm sure this can be converted to 2005 and below, but that is a LINQ statement.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Window class for external windows

    Quote Originally Posted by formlesstree4 View Post
    Count on an Array is a LINQ method, which means you need 2008 or greater. I'm sure this can be converted to 2005 and below, but that is a LINQ statement.
    Just to be technically correct, I think it is an Extension method not a LINQ method (although I guess it may use LINQ internally)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,250

    Re: Window class for external windows

    Quote Originally Posted by chris128 View Post
    Just to be technically correct, I think it is an Extension method not a LINQ method (although I guess it may use LINQ internally)
    I can't use Count() without importing System.Linq in C# 3.0 and above... o.o

    EDIT: i'll test this when I get home from work, can't right now as work uses Framework 2.0

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Window class for external windows

    It is a method in the System.Linq namespace.

    If you're calling it as an instance member of an IEnumerable then it's an extension method.

    Here's a small example showing both ways to call it.

    vb.net Code:
    1. Dim theArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    2.         Dim c1 = Enumerable.Count(theArray)
    3.         Dim c2 = theArray.Count()
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Window class for external windows

    Maybe we are talking about two different things... This is what I'm talking about (see screenshot)



    and I'm pretty sure its what the guy who mentioned the error was talking about too, as that is the exact error you get if I change that project to target .NET 2.0

    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,250

    Re: Window class for external windows

    Quote Originally Posted by chris128 View Post
    Maybe we are talking about two different things... This is what I'm talking about (see screenshot)



    and I'm pretty sure its what the guy who mentioned the error was talking about too, as that is the exact error you get if I change that project to target .NET 2.0

    No, that's what I'm talking about. I guess it is an extension.

  12. #12

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Window class for external windows

    Yeah that will do it, sorry I thought that was obvious but I should have mentioned it before I went on the technical rampage
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: Window class for external windows

    Hey can someone help me with this.

    W.Location works with all apps expect with the one I need it to work It says Object reference not set to an instance of an object. .. ONLY for that 1 app? How can I fix it.

  15. #15
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Window class for external windows

    This is just brilliant! How do you set the border-style on an external window to none though?

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width