Location Earth, (I Think)
Home
About Us
Contact Us
Links
Home
Basic Window Manipulation, (User32.dll) - UNDER CONSTRUCTION 
DarkBasic Step By Step Guide.

 

This Guide is Designed as a N00b's Guide to User32.dll, and by Reading through this, and running all the Examples, You should get a Better Idea of the Basics, And how to use Listed Functions on WWW.MSDN.COM , If you already understand the use of Windows DLL's and the MSDN Database, then you may Simply want to scan through the Examples posted here, for any useful functions.

 

I always place my DLL Calls in Functions as it makes them easier to remember, and it means you can Minimize the Paramiters Required, Also If you have them Saved all in one File, This makes it a Good #Include File for DarkBasic.

 

Here is a Quick Index of Whats included on this page

  • Step 1 - Getting a Windows Handle
  • Step 2 - Getting and Changing Size and Position
  • Step 3 - More Window Controls
  • Step 4 - Combine With BlueGui

 

Once youve read this Tutural, Then Feel free to Browse other Examples on this site, (All listed on the HomePage)

 


 

Step 1 - Getting a Windows Handle

 

A Handle to me is like a Windows Pointer To a Windows or other Gadget/Object, So by gaining the Handle to a Window, You can then Manipulate the Window it to Suite your Applications Needs.

 

Example1.1 below shows whats proberley the 2 most used Functions to gain The DarkBasic Windows Handle, they are GetActiveWindow And GetForegroundWindow, Both of these proform the same task and get the Handle to the Window That is Currently OnTop, (Top in zOrder).

 

Example 1.1 - Copy this to DarkBasic, Then press F5 to Run the Examples.

GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

 

DBWin   = GetActiveWindow()


DO

LOOP

 

`        ---------------------------------------------------------------------------------------------------------------

FUNCTION GetActiveWindow()                                    : ` Get the Handle for the Active Window
         hwnd = CALL DLL(DLL_User32,"GetActiveWindow")


ENDFUNCTION hwnd
FUNCTION GetForegroundWindow()                                : ` Get the Handle for the Active Window
         hwnd = CALL DLL(DLL_User32,"GetForegroundWindow")


ENDFUNCTION hwnd

`        ---------------------------------------------------------------------------------------------------------------

 

Example1.2 below shows several other ways to get a Handle.
  • FindWindowA Is a Very useful one if you know what the Title of the Window is.
  • GetNextWindow has been used Below to make the GetNextWndow(Handle) and GetPreviousWindow(Handle) Functions, These allow you to Search all Windows that Exist.
  • WindowFromPoint and ChildWindowFromPoint can both be Used to Get a Window at the X and Y Screen Coordinates
  • GetDesktopWindow Returns a Handle to the Desktop Window
  •  
Because this Example includes 2 Functions Based on Screen Coordinates, I have also Included 2 other Function to get CursorX() and CursorY(), These use GetCursorPos, to get the Coordinates into a Memblock for you to Access, ( See Example 1.2 )
Example 1.2 - Copy this to DarkBasic, Then press F5 to Run the Examples.

GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

SET WINDOW ON

 

DBWin   = GetActiveWindow()
Desktop = DesktopHandle()


DO : CLS
TEXT 10,10,"Desktop Handle     = "+STR$(Desktop)
TEXT 10,30,"DarkBasic Handle   = "+STR$(DBWin)
TEXT 10,50,"Active Window      = "+STR$(GetActiveWindow())
TEXT 10,70,"Handle Under Mouse = "+STR$(WindowFromPoint(CursorX(),CursorY()))
SYNC : LOOP

 

`        ---------------------------------------------------------------------------------------------------------------

FUNCTION GetActiveWindow()                                    : ` Get the Handle for the Active Window
         hwnd = CALL DLL(DLL_User32,"GetActiveWindow")


ENDFUNCTION hwnd
FUNCTION GetForegroundWindow()                                : ` Get the Handle for the Active Window
         hwnd = CALL DLL(DLL_User32,"GetForegroundWindow")


ENDFUNCTION hwnd

FUNCTION FindWindow(Title$)                                   : ` Get a Handle for a Window Searching by Title
         hwnd = CALL DLL(DLL_User32,"FindWindowA",0x0,Title$)


ENDFUNCTION hwnd
FUNCTION GetNextWindow(Handle)                                : ` Get the Next Window in zOrder
         Result = CALL DLL(DLL_User32,"GetNextWindow",Handle,2)

 

ENDFUNCTION Result
FUNCTION GetPreviousWindow(Handle)                            : ` Get the Previous Window in zOrder
         Result = CALL DLL(DLL_User32,"GetNextWindow",Handle,3)

 

ENDFUNCTION Result

FUNCTION WindowFromPoint(x,y)                                 : ` Returns Handle to Window at x,y
         Result = CALL DLL(DLL_USER32,"WindowFromPoint",x,y)

 

ENDFUNCTION Result
FUNCTION ChildWindowFromPoint(Handle,x,y)                     :
` Determines which, if any, of the child windows belonging to a parent window contains the specified point. The search is restricted to immediate child windows. Grandchildren, and deeper descendant windows are not searched
         Result = CALL DLL(DLL_USER32,"ChildWindowFromPoint",Handle,x,y)

 

ENDFUNCTION Result
`        ---------------------------------------------------------------------------------------------------------------

FUNCTION DesktopHandle()                                      : ` Returns the Handle to the Desktop
         Result = CALL DLL(DLL_User32,"GetDesktopWindow")

ENDFUNCTION Result

`        ---------------------------------------------------------------------------------------------------------------

FUNCTION CursorX()                                            : ` Get the X Position of the Cursor
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,0)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

FUNCTION CursorY()                                            : ` Get the Y Position of the Cursor
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetCursorPos",GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,4)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result

`        ---------------------------------------------------------------------------------------------------------------

FUNCTION FindFreeMemblock()                                   : ` INTERNAL USE ONLY...
   Local
Result As Integer
         Result = 1
         Repeat
         Inc
Result,1
         Until MemBlock Exist(Result) =
0

 

ENDFUNCTION Result
`        ---------------------------------------------------------------------------------------------------------------

 

 
 
Step 2 - Getting and Changing Size and Position
 
Example 2.1 below Runs MSPaint, Gets the Handle to its Window, Then Repositions and Resizes it using MoveWindow, This Section also uses GetWindowRect to Retreive Size and Position Data, I have also Included several other Functions Below which use the same DLL Function to proform Simular Tasks, So copy'n'Paste to DarkBasic, Press F5 to see the code working, Then try changing to get use to the Commands.
 
GetSystemMetrics Is also shown in Use at the Bottom of this Example to Retreave The Desktop Width and Height, Usually you may use External or Addon DLL's to get this Information in DarkBasic, So this simply shows you how to Get it Yourself Through the Windows DLL's, I would Recommend reading the msdn entry as you can also gain lots of other values from this.
 
This Example also used a MemBlock to Send/Recceive Information from the DLL by Passing a Pointer to the Memblock, This is Handy to know as there are Many other Commands that use this Ability, Such as Getting the Cursor Position.

Example 2.1 - Copy this to DarkBasic, Then press F5 to Run the Examples.

GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

 

Set Window On


Execute File "MSPaint","",""             : ` Run MSPaint
Wait 5000                                : ` Give it time for the Programme to Load
MSPaint = FindWindow("untitled - Paint") : ` Get the Handle to MSPaint
Set(MSPaint,100,100,300,500)             : ` Reposition and Resize the Window

 

` MSPaint Data

  PRINT "Details for MSPaint Window"

  PRINT

  PRINT "Window Handle = "+STR$(MSPaint)

  PRINT "Window X      = "+STR$(     GetX(MSPaint))

  PRINT "Window Y      = "+STR$(     GetY(MSPaint))

  PRINT "Window Width  = "+STR$( GetWidth(MSPaint))

  PRINT "Window Height = "+STR$(GetHeight(MSPaint))

  PRINT

 

` Desktop Data

  PRINT "Details for the Desktop"

  PRINT

  PRINT "Desktop Handle = "+STR$(DesktopHandle())

  PRINT "Desktop Width  = "+STR$(DesktopWidth())

  PRINT "Desktop Height = "+STR$(DesktopHeight())

 

 

Do

Loop

 

`        --------------------------------------------------------------------------------------------------------------------
FUNCTION FindWindow(Title$)                                   : ` Get a Handle for a Window Searching by Title
            hwnd = CALL DLL(DLL_User32,"FindWindowA",0x0,Title$)
ENDFUNCTION hwnd

`        --------------------------------------------------------------------------------------------------------------------

FUNCTION GetX(Handle)                                         : ` Get the Items X Position
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,0)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result
FUNCTION GetY(Handle)                                         : ` Get the Items Y Position
        
Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,4)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result
FUNCTION GetWidth(Handle)                                     : ` Get the Items Width
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,8)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result
FUNCTION GetHeight(Handle)                                    : ` Get the Items Height
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Result = MEMBLOCK DWORD(Memblock,12)
         DELETE MEMBLOCK Memblock

 

ENDFUNCTION Result
`        --------------------------------------------------------------------------------------------------------------------

FUNCTION Set(Handle,x,y,Width,Height)                          : ` Position and Resize the Item
         Temp = CALL DLL(DLL_User32,"MoveWindow",Handle,x,y,Width,Height,1)

 

ENDFUNCTION
FUNCTION Move(Handle,x,y)                                     : ` Move the Item
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Width  = MEMBLOCK DWORD(Memblock, 8)
         Height = MEMBLOCK DWORD(Memblock,12)
         DELETE MEMBLOCK Memblock
         Temp   = CALL DLL(DLL_User32,"MoveWindow",Handle,x,y,Width,Height,1)

 

ENDFUNCTION
FUNCTION Position(Handle,x,y)                                 : ` Move the Item
         Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         Width  = MEMBLOCK DWORD(Memblock, 8)
         Height = MEMBLOCK DWORD(Memblock,12)
         DELETE MEMBLOCK Memblock
         Temp   = CALL DLL(DLL_User32,"MoveWindow",Handle,x,y,Width,Height,1)

 

ENDFUNCTION
FUNCTION
Resize(Handle,Width,Height)                          : ` Resize the Item
         
Memblock = FindFreeMemblock()
         MAKE MEMBLOCK Memblock,20
         Temp   = CALL DLL(DLL_User32,"GetWindowRect",Handle,GET MEMBLOCK PTR(Memblock))
         x      = MEMBLOCK DWORD(Memblock,0)
         y      = MEMBLOCK DWORD(Memblock,4)
         DELETE MEMBLOCK Memblock
         Temp   = CALL DLL(DLL_User32,"MoveWindow",Handle,x,y,Width,Height,1)

 

ENDFUNCTION
`        --------------------------------------------------------------------------------------------------------------------

FUNCTION DesktopHandle()                                      : ` Returns the Handle to the Desktop
         Result = CALL DLL(DLL_User32,"GetDesktopWindow")

ENDFUNCTION Result
FUNCTION DesktopWidth()                                       : ` Returns Desktop Width
         Result = CALL DLL(DLL_User32,"GetSystemMetrics",0)

 

ENDFUNCTION Result
FUNCTION DesktopHeight()                                      : ` Returns Desktop Height
         Result = CALL DLL(DLL_User32,"GetSystemMetrics",1)

 

ENDFUNCTION Result
`        --------------------------------------------------------------------------------------------------------------------

 

Step 3 - More Window Controls

 

There are Many Many Functions Listed Below to enable you to Manipulate a Window as you see fit, Theres no Example for this one, only the Functions Ready to Use, But if you read through this whole document, Then you should now have a Basic Understanding of how they work, So Copy This to DarkBasic, Then PlayAround as thats the Best Way to Learn.

 

You may also want to Copy the Functions Used Above into the Same Document, Then you can use this as an #Include File to gain Instant Access to These Functions.

Example 3.1 - Copy this to DarkBasic, Then Play Around.

GLOBAL DLL_User32 AS INTEGER : DLL_User32 = 1 : LOAD DLL "User32.dll",DLL_User32

 

DBWin = GetActiveWindow()

 

Do

Loop

 

`        --------------------------------------------------------------------------------------------------------------------

FUNCTION GetActiveWindow()                                    : ` Get the Handle for the Active Window
         hwnd = CALL DLL(DLL_User32,"GetActiveWindow")


ENDFUNCTION hwnd

`        --------------------------------------------------------------------------------------------------------------------

FUNCTION GetParent(Handle)                                    : ` Get the Parent of an Item like a Window from the Handle
         Result = CALL DLL(DLL_USER32,"GetParent",Handle)

 

ENDFUNCTION Result
FUNCTION SetParent(Handle,Parent)                             : ` Change the Items Parent
        
Temp = CALL DLL(DLL_User32,"SetParent",Handle,Parent)

 

ENDFUNCTION
FUNCTION SetFont(Handle,FontHandle)                           : ` Set an Items Font
        
Result = CALL DLL(DLL_User32,"SendMessageA",Handle,0x0030,FontHandle,1)

 

ENDFUNCTION
FUNCTION Delete(Handle)                                       : ` Delete an Item
         CALL DLL DLL_User32,"DestroyWindow",Handle

 

ENDFUNCTION
`        --------------------------------------------------------------------------------------------------------------------

FUNCTION BringWindowToFront(Handle)                           : ` Move Window to Foreground and Give Focus
        
Result = CALL DLL(DLL_User32,"BringWindowToTop",Handle)

 

ENDFUNCTION
FUNCTION
AlwaysOnTop(Handle)                                  : ` Set Window Always on Top
         Result = CALL DLL(DLL_User32,"BringWindowToTop",Handle)
         Result = CALL DLL(DLL_User32,"LockSetForegroundWindow",1)

 

ENDFUNCTION
FUNCTION
RemoveAlwaysOnTop()                                  : ` Undo AlwaysOnTop
        
Result = CALL DLL(DLL_User32,"LockSetForegroundWindow",2)

 

ENDFUNCTION
FUNCTION
HideWindow(Handle)                                   : ` Minimize a Window
        
Result = CALL DLL(DLL_User32,"CloseWindow",Handle)

 

ENDFUNCTION
`        --------------------------------------------------------------------------------------------------------------------
FUNCTION IsWindow(Handle)                                     : ` Returns 1 if the Item is a Window, Else Returns 0
         Result = CALL DLL(DLL_User32,"IsWindow",Handle)

 

ENDFUNCTION Result
FUNCTION IsWindowVisible(Handle)                              : ` Returns 1 if the Item is Visible, Else Returns 0
         Result = CALL DLL(DLL_User32,"IsWindowVisible",Handle)

 

ENDFUNCTION Result
FUNCTION IsWindowMaximized(Handle)                            : ` Returns 1 if the Window is Maximized, Else Returns 0
         Result = CALL DLL(DLL_User32,"IsZoomed",Handle)

 

ENDFUNCTION Result
FUNCTION IsWindowMinimized(Handle)                            : ` Returns 1 if the Window is Minimised, Else Returns 0
         Result = CALL DLL(DLL_User32,"ismin",Handle)


 

ENDFUNCTION Result
FUNCTION IsChild(Handle,Parent)                               : ` Tests whether a window is a child window or descendant window of a specified parent window. A child window is the direct descendant of a specified parent window if that parent window is in the chain of parent windows; the chain of parent windows leads from the original overlapped or pop-up window to the child window.
         Result = CALL DLL(DLL_User32,"IsChild",Parent,Handle)

 

ENDFUNCTION Result
FUNCTION IsHung(Handle)                                       : ` Indicates if a Window/App is not responding
        
Result = CALL DLL(DLL_User32,"IsHungAppWindow",Handle)

 

ENDFUNCTION Result
`        --------------------------------------------------------------------------------------------------------------------
FUNCTION IsWindowEnabled(Handle)                              : ` Determines whether the specified window is enabled for mouse and keyboard input
         Result = CALL DLL(DLL_User32,"IsWindowEnabled",Handle)

 

ENDFUNCTION Result
FUNCTION EnableWindow(Handle,Bool)                            : ` The EnableWindow function enables or disables mouse and keyboard input to the specified window or control. When input is disabled, the window does not receive input such as mouse clicks and key presses. When input is enabled, the window receives all input.
         Result = CALL DLL(DLL_User32,"EnableWindow",Handle,Bool)

 

ENDFUNCTION
`        --------------------------------------------------------------------------------------------------------------------
FUNCTION SetWindowText(Handle,Text$)                          : ` The SetWindowText function changes the text of the specified window's title bar (if it has one). If the specified window is a control, the text of the control is changed. However, SetWindowText cannot change the text of a control in another application
         Result = CALL DLL(DLL_User32,"SetWindowText",Handle,Text$)

 

ENDFUNCTION Result
FUNCTION FlashWindow(Handle,Invert)                           : ` Flashes the specified window one time. It does not change the active state of the window.
         Result = CALL DLL(DLL_User32,"FlashWindow",Handle,Invert)

 

ENDFUNCTION Result
`        --------------------------------------------------------------------------------------------------------------------

 
Step 4 - Combine With BlueGui
 
If you have Purchised the BlueGui DLL, You can also gain the Handle to a Window by Calling the Following Command
 

Handle = GadgetHandle(GadgetNumber)

 
This is Handy as while you read through this Tutural, You will see Functions and Capabilities that Blue does not have, So this Tutural can still be Useful to you, Or if you Make / Gain access to a Window, You can also gain access to the BlueGui Coimmands for it by Calling.
 

Gadget = CreateGadgetFromWindow(Handle)

 


  This page was last modified on Tuesday, 29 April 2008 15:12

 

This Example shows how to retreive information about a window such as Position, Size, Parent, Ect, There are also a number of Other useful Functions included in this Example which access the User32.dll so so play around as thats how to learn.

Function SetParent(Handle)

            Result = CALL DLL(DLL_User32,"SetParent",Handle)

EndFunction Result

Function SetWindowText(Handle)

            Result = CALL DLL(DLL_User32,"SetWindowText",Handle)

EndFunction Result

 

This Example shows how to Ceate new Windows and Buttons, This is Pritty useful, Although if you have BlueGui or WinGui, It would be better making your UI in That as its Built for Darkbasic.