If you can think of something to do to a window, there’s probably an API function to do it. Visual Basic provides corresponding methods for many of these operations. In some cases, properties perform operations rather than simply providing access to attributes. Table 6-2 compares the Windows Way and the Basic Way of getting things done.
Attribute | Windows Way | Basic Way |
Active window | GetActiveWindow, possibly combined with GetParent | ActiveForm and Active-Control properties |
Style bits | GetWindowLong with GWL_STYLE or GWL_EXSTYLE | BorderStyle, Enabled, Visible, WindowState, ControlBox, MaxButton,and MinButton properties |
Window title | GetWindowText and SetWindowText | Caption property |
Enabled, visible | WS_VISIBLE, WS_DISABLED style bits; IsWindowEnabled, IsWindowVisible, Enable Window, and ShowWindow | Enabled and Visible properties |
Tab position | WS_TABSTOP style bit; GetNextDlgTabItem | TabStop and TabIndex properties |
Window dimensions | GetWindowRect and SetWindowPos | Left, Top, Width, and Height properties; indirectly, Align-ment and AutoSize properties |
Client dimensions | GetClientRect | ScaleLeft, ScaleTop, ScaleWidth and ScaleHeight properties |
Property list | GetProp, SetProp, and RemoveProp | Tag property (a minimal version) |
Device context | GetDC and ReleaseDC | hDC property |
Relative windows | GetWindow with GW_ constants, GetNextWindow, and GetParent | Parent property for controls but otherwise no way |
Instance handle | GetWindowLong with GWL_HINSTANCE | App.hInstance property |
Process ID and handle | GetWindowThreadProcessId and OpenProcess | No way |
Class name | GetClassName | No way |
Table 6-1. Window properties.
As Tables 6-1 and 6-2 (on the following page) show, it’s a mixed-up world. You won’t always find an obvious comparison between the Basic Way and the Windows Way. But if you are like most hardcore programmers, your eye keeps wandering to those items that show up in the Windows Way column but not in the Basic Way column. That’s the challenge the rest of this chapter attempts to meet.
Operation | Windows Way | Basic Way |
Show in different states | ShowWindow, OpenIcon, CloseWindow, IsIconic, and IsZoomed | Show and Hide methods; WindowState property |
Change position, size, or z-order | SetWindowPos, DeferWindow- | Move and ZOrder methods; |
Pos, BeginDeferWindowPos, | Left, Top, Width, and Height | |
EndDeferWindowPos, | properties | |
BringWindowToTop, and | ||
MoveWindow | ||
Set active window | SetForegroundWindow, Set-ActiveWindow, and SetFocus | SetFocus method; AppActivate statement |
Create | CreateWindow, RegisterClass, and so on | Mostly automatic, but Load statement starts the process |
Change window placement | GetWindowPlacement and SetWindowPlacement | Left, Top, Width, and Height properties |
Capture mouse | SetCapture, GetCapture, and ReleaseCapture | No way |
Destroy | DestroyWindow | Unload statement |
Update window | UpdateWindow, Invalidate-Rect, BeginPaint, and a slew of others | Don’t worry about it (or turn off AutoRedraw property and handle in Paint event) |
Iterate through window hierarchy | EnumChildWindows, Enum-Windows, and GetWindow | No way |
Find window | FindWindow, GetActiveWindow, and WindowFromPoint | No way |
Text output | TextOut, ExtTextOut, GrayString, and others | Print method |
Text sizing | GetTextExtentPoint32 | TextWidth and TextHeight methods |
Text alignment | SetTextAlign, SetText-Justification, and SetTextCharacterExtra functions | No direct comparison |
Table 6-2. Window methods.