Tips and Triks Visual Basics (VB)

  • These tips & Tricks are donated by Microsoft.nl They provides examples of how you can solve a particulair problem with Visual Basic. They are free to use and free to re-publish.
Tip 63: Preventing List Box from Redrawing (Refreshing)
Tip 64: Changing the Case of Text Entered in a Text Box Control
Tip 65: Separating a Path into Individual Fields
Tip 66: Disabling Task-Switching with the SetSysModalWindow Function
Tip 67: Returning Focus to a Specific Control After Executing WinHelp
Tip 68: Removing Duplicate Items from List Box Controls
Tip 69: Forcing a Picture Control to Use Only a Specific Font
Tip 70: Creating Temporary Files
Tip 71: Dragging Controls at Run Time
Tip 72: Positioning the Cursor over a Control That Receives Focus
Tip 73: Creating Nested Directories
Tip 74: Scrolling Through Two List Box Controls Simultaneously
Tip 75: Invoking Menu Items in Other Applications with SendMessage
Tip 76: Detecting Right Mouse Button Clicks on List Box Controls
Tip 77: Determining the Amount of RAM Installed in a Computer
Tip 78: Retrieving the Names of All Printers
Tip 79: Closing All MDI Child Windows at One Time
Tip 80: Drawing Borders Around Controls
Tip 81: Repairing and Compressing a Microsoft Access Database from
Tip 82: Retrieving Multiple Filenames from the Common Dialog Control
Tip 83: Listing Fields and Associated Properties for an Attached
Tip 84: Creating a Scrolling "Credits" Control
Tip 85: Hiding MDI Child Forms at Run Time
Tip 86: Allowing a Visual Basic Application to Accept Drag-and-Drop
Tip 87: Sending Output to the Printer in Any Order
Tip 88: Shrinking Icons Down to Size
Tip 89: Invoking the Microsoft Word Page Setup Dialog Box
Tip 90: Adding ToolTips to Visual Basic applications
Tip 91: Determining If a Form Is Loaded
Tip 92: Using the Shell Statement to Execute MS-DOS Programs
Tip 93: Retrieving the Names of Installed Printers
Tip 94: Using Drag-and-Drop on Multiple items in a List Box Control
Tip 95: Deleting All Records from Every Table in a Microsoft Access Database
Tip 96: Centering a Form over Another Form
Tip 97: Creating a Task List
Tip 98: Saving a Window's Client Area in Bitmap Format
Tip 99: Adding Three-Dimensional Effects to Visual Basic Forms
Tip 100: Printing a Form Multiple Times on One Page
Tip 101: Using the Built-In Windows Icons
Tip 102: Modifying a Child Window's Caption
Tip 103: Preventing the CTRL+TAB and CTRL+F6 Key Combinations from Activating
Tip 104: Creating a Form with a Thin Title Bar
Tip 105: Removing a Form's Title Bar
Tip 106: Centering Text Vertically in a Text Box Control
Tip 107: Detecting Double-Click Events in Combo Box Controls
Tip 108: Flashing Controls to Get the User's Attention
Tip 109: Modifying an MDI Form's Border Style
Tip 110: Sending a Click Event to a Command Button Control
Tip 111: Using Different Fonts in List Boxes That Have Tab Stops
Tip 112: Preventing ListIndex Property from Triggering a Click Event
Tip 113: Creating Transparent Forms
Tip 114: Preventing a Portion of a Text Box from Scrolling
Tip 115: Performing Smart Searches in Combo Box Controls
Tip 116: Sending Data to the Printer in Landscape or Portrait Mode
Tip 117: Changing a Menu's Shortcut Key at Run Time
Tip 118: Converting a Word for Windows Document to RTF Format
Tip 127: Stamping Files with the Current Date and Time
Tip 128: Calculating the Beginning and Ending Date of a Month
Tip 130: Using the Undo Feature with a Text Box Control
Tip 131: Determining Whether a DLL File Is 16-Bit or 32-Bit
Tip 132: Preventing Duplicate Items from Being Added to List Box Controls
Tip 133: Using the SendMessage Function to Scroll Contents of Text Box =
Tip 134: Creating Temporary Files
Tip 135: Preventing a User from Editing the Contents of a Text Box =
Tip 136: Using Keyboard and Mouse Shortcuts
Tip 137: Removing Items from a Drive List Box Control
Tip 138: Enabling and Disabling Scroll Bars in a List Box
Tip 139: Retrieving a Disk's Volume Label
Tip 140: Identifying CD-ROM Drives
Tip 141: Searching a List Box Control for a Partial Match
Tip 142: Determining Whether the Operating System Supports Long File
Tip 143: Determining Whether the Windows 95 Taskbar Is Visible or=20
Tip 144: Using Accelerator Keys with the TabStrip Control
Tip 145: Inserting Tab Characters in the Rich-Text Box Control
Tip 146: Retrieving the Printer Name from the Windows 95 Registry
Tip 147: Retrieving Multiple File Names from the Common Dialog Control
Tip 148: Using OLE Automation to Check Spelling
Tip 149: Enumerating Disk Drives in Visual Basic 4.0
Tip 150: Determining How Many Items Can Be Displayed in the List Box
Tip 151: Finding Whole Words in a Text Box Control
Tip 152: Determining the Status of Virtual Keys on the Keyboard
Tip 155: Scrolling Text Horizontally in a Picture Box Control
Tip 156: Adding New Icons to the Windows 95 Taskbar
Tip 153: Searching PATH for Specific Files
Tip 154: Terminating Windows 95 in Visual Basic
Tip 159: Setting the Minimum Size of a Window
Tip 160: Retrieving the Versions of MS-DOS and Windows
Tip 161: Forcing the Common Dialog Control to Save Files to a Specific Disk Drive
Tip 162: Determining Whether a File Exists
Tip 163: Emulating a Double-Click Event in the Directory List Box Control
Tip 164: Copying Files from One Directory to Another
Tip 165: Locating CD-ROM Drives Installed on a Computer System
Tip 166: Playing .WAV Files in Visual Basic
Tip 167: Controlling the State of Virtual Keys on the Keyboard
Tip 168: Using the ShellExecute Function to Print Files
Tip 169: Setting the Position and Size of the Windows Help File
Tip 170: Positioning the Mouse Pointer over a Specific Control
Tip 171: Determining RGB Color Values
Tip 172: Extracting the Directory Name and the Filename from the Path
Tip 173: Launching Applications in Visual Basic
Tip 174: Using the GetKeyState Function to Determine the State of
Tip 175: Determining the Current Screen Resolution
Tip 176: Sending Files to the Recycle Bin in Visual Basic 4.0
Tip 177: Adding Drag-and-Drop Functionality to Your Application
Tip 178: Enabling or Disabling Fast Task Switching in Windows
Tip 179: Retrieving the Computer Name
Tip 180: Performing Searches in a Combo Box Control with SendMessage
Tip 182: Temporarily Enabling or Disabling Tabs on the TabStrip Control
Tip 183: Changing the Color of the Grid Control's Cells
Tip 184: Creating a List of Directories and Files in a List Box Control
Tip 185: Determining the Number of Printable Lines Per Page on the Printer
Tip 186: Formatting Text in a Rich Text Box Control in Visual Basic 4.0
Tip 187: Determining Whether an Application Was Closed from the Control Menu
Tip 188: Adding New Commands to the Control Menu
Tip 189: Determining Which Screen and Printer Fonts Are Available
Tip 190: Adding Hot Key Access to Your Visual Basic Application
Tip 191: Shelling to Other Applications
Tip 192: Selecting a New Desktop Wallpaper
Tip 193: Launching Windows 95 Control Panel Applets in Visual Basic
Tip 195: Modifying a File's Date and Time Stamp in Visual Basic 4.0
Tip 196: Using the Common Dialog Control to Invoke Context-Sensitive Help
Tip 197: Preventing a Right-Click from Displaying a Context Menu
Tip 198: Retrieving File Information in Visual Basic 4.0
Tip 199: Saving the Contents of the List Box Control to Disk in Visual
Tip 200: Determining the Amount of Free Disk Space
Tip 201: Retrieving the Task List in Windows 95
Tip 202: Forcing a Combo Box Control to Drop Down with the ENTER Key
Tip 203: Searching a Disk for Files Based on Filename and Path
Tip 204: Changing the Screen Resolution at Run Time in Visual Basic 4.0
Tip 205: Drawing a Gradient Background Pattern on Forms in Visual Basic 4.0
Tip 206: Populating the Outline Control with a List of Directories and
Tip 208: Changing the Displayed Icon in the About Dialog Box in
Tip 209: Changing the Color of a Label Control When the Mouse Pointer Is

Tip 110: Sending a Click Event to a Command Button Control

Abstract
In a Microsoft(r) Visual Basic(r) application, you can simulate a Click =
event to
a Command Button control. This article explains how to send a BN_CLICKED
notification message to a control.

Executing a BN_CLICKED Message
A user who wants to carry out a command in your Microsoft(r) Visual =
Basic(r)
application usually clicks a Command Button control. The code in the =
Command
Button's Click event is then executed.

There may be times, however, when you will want to initiate a Click =
event from
within your Visual Basic program. You can use the Microsoft Windows(r) =
application
programming interface (API) PostMessage function to send a BN_CLICKED =
notification
message to the parent of the Command Button control. This will call the =
button's
Click event.
As you can see from the example program below, the GetDlgCtrlID function
retrieves the Command Button's handle. Next, a call is made to the =
GetParent
function, which retrieves the handle of the window that the Command =
Button resides
on. (In other words, we must retrieve the parent window's handle.)

The last step is to execute a PostMessage function. PostMessage sends a =
BN_CLICKED
notification message to the parent window, which then processes the =
Click event
for the Command Button.
When you run the example program below, the second Command Button's =
Click event
is executed. However, the second Command Button does not receive the =
focusonly its
code is executed.

Example Program
This program shows how to send a Command Button click to the Windows =
operating
system.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following Constant and Declare statements to the General =
Declarations
section of Form1 (note that each Declare statement must be typed as =
a single
line of text):

Const BN_CLICKED =3D 0
Const WM_COMMAND =3D &H111
Private Declare Function GetDlgCtrlID Lib "User" (ByVal hWnd As Integer) =
As
Integer
Private Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As =
Integer
Private Declare Function PostMessage Lib "User" (ByVal hWnd As Integer, =
ByVal
wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As =
Integer

3. Add a Command Button control to Form1. Command1 is created by =
default. Set
its Caption property to "Send".
4. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
ClickButton Command2.hWnd
End Sub

5. Add a second Command Button control to Form1. Command2 is created by =
default.
Set its Caption property to "Receive".
6. Add the following code to the Click event for Command2:

Private Sub Command2_Click()
MsgBox "Command2 was CLICKED!"
End Sub

7. Create a new function called ClickButton. Add the following code to =
this
function:

Sub ClickButton(ByVal hWnd As Integer)
Dim Button As Integer
Dim ParentHwnd As Integer
Dim X As Integer
=20
Button =3D GetDlgCtrlID(hWnd)
ParentHwnd =3D GetParent(hWnd)

X =3D PostMessage(ParentHwnd, WM_COMMAND, Button, BN_CLICKED * =
&H10000 + hWnd)
End Sub

Run the example program by pressing F5. Click the Send Command Button. =
The Click
event for the second Command Button control is immediately executed (the =
message
box is displayed).

Additional References
"BN_CLICKED." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference Volume 3: Messages, =
Structures)
"GetDlgCtrl." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)
PostMessage." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)

Tip 109: Modifying an MDI Form's Border Style

Abstract
This article explains how you can create a multiple-document interface =
(MDI) form
that has a fixed border in a Microsoft(r) Visual Basic(r) application.

Retrieving and Setting a Form's BorderStyle
Every form you create when designing a Microsoft(r) Visual Basic(r) =
application
can have one of four border styles. Just set the form's BorderStyle =
property to
one of the following styles:
0 - None
1 - Fixed Single
2 - Sizeable
3 - Fixed Double

An MDI child form, however, does not have a BorderStyle property. But by =
using the
Microsoft Windows(r) GetWindowLong and SetWindowLong application =
programming
interface (API) functions , you can change an MDI form's border style to =
a fixed
border style.
The GetWindowLong function retrieves information about the specified =
window's
style attributes and the SetWindowLong function modifies the specified =
window's
style attributes.

GetWindowLong requires only two arguments. The first argument is the =
target
window's handle. The second argument specifies the type of information =
you want
to retrieve, which is the style settings for the window.
After retrieving the window's current style settings, use the bitwise =
And Not
function to remove the WS_THICKFRAME attribute from the style settings =
value.
Next, issue a call to the SetWindowLong function to set the new style =
settings
for the specified window. This creates an MDI form that has a fixed =
border style.


Example Program
This program shows how to create an MDI form that has a fixed border. =
Run the
example program by pressing F5. The MDI form will be displayed with a =
fixed
border.

1. Create a new project in Visual Basic. Form1 is created by default.
2. From the Insert menu, select MDI Form to create an MDI form. =
MDIForm1 is
created by default.
3. Set Form1's MDIChild property to True. Modify the size of this form =
so that
it is smaller than the MDIForm1 form.
4. Add the following Constant and Declare statements to the General =
Declarations
section of Form1 (note that each Declare statement must be typed as =
a single
line of text):

Private Declare Function GetWindowLong Lib "User" (ByVal hWnd As =
Integer, ByVal
nIndex As Integer) As Long
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd As =
Integer, ByVal
nIndex As Integer, ByVal dwNewLong As Long) As Long
Const GWL_STYLE =3D (-16)

Const WS_THICKFRAME =3D &H40000

5. Add the following code to the Load event for MDIForm1 (note that the =
NewStyle
line must be typed as a single line of code):

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long
CurStyle =3D GetWindowLong(MDIForm1.hWnd, GWL_STYLE)
NewStyle =3D SetWindowLong(MDIForm1.hWnd, GWL_STYLE, CurStyle And
Not (WS_THICKFRAME))
End Sub

Additional References

"GetSystemMenu." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference, Volume 2: Functions)
Knowledge Base Q118376. "How to Lock a Form So It Cannot Be Moved."
Knowledge Base Q110393. "How to Remove Menu Items from a Form's =
Control-Menu Box."
Knowledge Base Q77930. "Modifying the System Menu of an MDI Child =
Window."
Knowledge Base Q71669. "Preventing an MDI Child Window from Changing =
Size."

Tip 108: Flashing Controls to Get the User's Attention

Abstract
When developing an application in Microsoft(r) Visual Basic(r), you can =
use the
BackColor property to change the background color of a control. This =
article
explains how you can temporarily flash a control's BackColor property to =
draw the
user's attention to a specific control.

Changing a Control's BackColor Property
When designing a Microsoft(r) Visual Basic(r) application , you place =
controls
such as List Boxes and Text Boxes on a form. At run time, you can move =
the focus
to one of these objects by using Visual Basic's SetFocus method. Users =
can then
see that that particular control needs to be addressed in some way. For =
example,
if a Text Box receives the focus, users know they must type some text =
into that
control.

However, users may not actually notice that the focus has been set to a =
specific
control because the "rubberband" (highlighting) around the inside of the =
control
is not that obvious. To alert the user, you could change the background =
color of
the control from white to, say, red, to draw the user's attention to =
that control.
When the control loses the focus, you could reset the control's =
background color
to white. This procedure, however, means that the control would be a =
different
color as long as that control retained the focus. In some situations, =
this would
not be appropriate.

A far better solution would be to change the control's background color =
for just
a few seconds. The example program below "flashes" a control by quickly =
changing
the control's background color three times in succession. The Timer =
function is
used to cause a short time delay in the program. Each time a 2-second =
interval
elapses, the control's color is changed from white to red, then back to =
white.
The For-Next loop dictates how many times the control is flashed. In =
this case,
a value of 3 was used to flash the color three times. This creates a =
very visual
clue to draw the user's attention to that specific control.

Example Program
This program shows how to highlight the control that has the focus. Run =
the
example program by pressing F5 . Then click the Flash Command Button. =
Note that
the background color of the List Box control is changed to red and =
flashed three
times.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
List1.AddItem "Item #1"
List1.AddItem "Item #2"
List1.AddItem "Item #3"
End Sub

3. Add a Command Button control to Form1. Command1 is created by =
default. Set
its Caption property to "Flash".
4. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
FlashControl List1
End Sub

5. Add a List Box control to Form1. List1 is created by default.
6. Create a new function called FlashControl. Add the following code to =
this
function:

Sub FlashControl(C As Control)
Dim OldColor As Double
Dim Delay As Double
Dim X As Integer
=20
OldColor =3D C.BackColor
For X =3D 1 To 3
C.BackColor =3D QBColor(12)
Delay =3D Timer
While Timer - Delay < 0.2
DoEvents
Wend
C.BackColor =3D OldColor
Delay =3D Timer
While Timer - Delay < 0.2
DoEvents
Wend
Next X
C.SetFocus
End Sub

Tip 107: Detecting Double-Click Events in Combo Box Controls

Abstract
When developing an application in Microsoft(r) Visual Basic(r), you may =
want to
let the user double-click the edit portion of a Combo Box control. This =
article
explains how to process this double-click event by using the Message =
Blaster
custom control.

Using Message Blaster to Detect Double-Clicks on Combo Boxes
In a Microsoft(r) Visual Basic(r) application, you can use a Combo Box =
control to
allow your user to easily select an item. Unfortunately, the Combo Box =
control
only responds to single-click events, not to double-click events in the =
box's
edit portion.

You can, however, use a subclassing control such as Message Blaster to =
detect
when a user has double-clicked the combo box. Before you can do this, =
you need
to use two Microsoft Windows(r) application programming interface (API)
functions GetWindow and GetClassName.
The GetWindow function retrieves the handle of a window that has a =
specific
relationship to the source window. In other words, we need to determine =
the
Combo Box's handle. The Combo Box window is actually a sibling window of =
our
Visual Basic application's main form. Next, we need to call the =
GetClassName
function to make sure that the edit portion of the Combo Box is the =
window we are
dealing with.

If you are using a combo box with its Style property set to 0 - Drop =
Down Combo,
detecting a double-click message is relatively straightforward. Just =
retrieve the
handle of the Combo Box's edit window, and then tell Message Blaster to =
intercept
the Windows WM_LBUTTONDBLCLK message.
On the other hand, if you are using a Combo Box with its Style property =
set to
1 - Simple Combo, the procedure is a little different. Because the edit =
portion
of the Combo Box is already displayed, you need to call GetWindow to =
retrieve
the handle of the edit window.

After you have retrieved the edit window's handle, you must call the =
GetClassName
function. This function is called so that we can be certain we are =
processing a
double-click message for only the edit portion of the Combo Box.

Example Program
This program shows how your Visual Basic application can respond to a =
double-click event when such an event is detected in the edit portion of =
a Combo Box control.
1. Create a new project in Visual Basic. Form1 is created by default.
2. From the Insert menu, select Custom Control. Add the Message Blaster =
custom
control to this project.
3. Add a Message Blaster control to Form1. MsgBlast1 is created by =
default.
4. Add the following code to the Message event for MsgBlaster1 (note =
that the
first two lines of code must be typed as a single line of text):

Private Sub MsgBlaster1_Message(MsgVal As Integer, wParam As Integer,
lParam As Long, ReturnVal As Long)
MsgBox "Combo1 box double-clicked"
End Sub

5. Add a Combo Box control to Form1. Combo1 is created by default. Set =
its Style
property to 1-Simple Combo.
6. Add the following Constant and Declare statements to the General =
Declarations
section of Form1 (note that each Declare statement must be typed as =
a single
line of text):

Private Declare Function GetWindow Lib "User" (ByVal hWnd As Integer,
ByVal wCmd As Integer) As Integer
Private Declare Function GetClassName Lib "User" (ByVal hWnd As Integer,

ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Const WM_LBUTTONDBLCLK =3D &H203
Const GW_CHILD =3D 5
Const GW_HWNDNEXT =3D 2

7. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
Dim hWndList As Integer
Dim hWndEdit As Integer
Dim Buf As String * 10
Dim X As Integer
=20
Combo1.AddItem "Item #1"
Combo1.AddItem "Item #2"
Combo1.AddItem "Item #3"
=20
hWndList =3D GetWindow(Combo1.hWnd, GW_CHILD)
Select Case Combo1.Style
Case 0
MsgBlaster1.hWndTarget =3D hWndList
MsgBlaster1.MsgList(0) =3D WM_LBUTTONDBLCLK
Case 1
hWndEdit =3D GetWindow(hWndList, GW_HWNDNEXT)
Buf =3D ""
X =3D GetClassName(hWndEdit, Buf, Len(Buf))
=20
If StrComp(Trim(Buf), "Edit" & Chr$(0)) =3D 0 Then

MsgBlaster1.hWndTarget =3D hWndEdit
MsgBlaster1.MsgList(0) =3D WM_LBUTTONDBLCLK
End If
End Select
End Sub

Run the example program by pressing F5. When you double-click the edit =
portion of
the Combo Box control, a message box will confirm this action. You can =
also change
the Style property of the Combo Box to 0 - Drop Down Combo to get the =
same effect.

Additional References

"GetClassName." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)
"GetWindow." (Development Library, Product Documentation, SDKs,
Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)
Knowledge Base Q110104. "Using MSGBLAST.VBX Control to Process Windows =
Messages
from VB."
"Processing Messages in Visual Basic." (Development Library, Technical =
Articles,
Visual Basic Articles)

Tip 106: Centering Text Vertically in a Text Box Control

Abstract
The Microsoft® Visual Basic® Text Box control lets your user enter text that can
later be used by your application. This article explains how you can center the
text that the user types vertically within the Text Box control.

Vertically Centering Text in Visual Basic
In your application, you may need to display the text typed by the user, centered
vertically within the Text Box control. The only way to accomplish this task is
to place the Text Box control within a larger Picture Box control. The Text Box
control allows you to type text that can later be used by your Microsoft® Visual
Basic® application. As the user types the text, the text wraps to the next line
(if the MultiLine property is set to True).

The example program below centers the text in the text box by first setting the
size of the Text Box control to the same as the size of the Picture Box control.
Whenever a Change event is detected by the Text Box control, the text is redrawn
in the control so that it appears vertically centered.

Example Program
This program shows how to center text vertically within a Text Box control.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Picture Box control to Form1. Picture1 is created by default. Set its
AutoRedraw property to True.
3. Add a Text Box control to Form1 over top of the Picture Box control. Text1 is
created by default. Set its MultiLine property to True.
4. Add the following Constant and Declare statements to the General Declarations
section of Form1 (note that the Declare statement must be typed as a single
line of text):

Private Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
Const WM_USER = &H400
Const EM_GETLINECOUNT = WM_USER + 10
Dim NumLines As Integer

5. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
Dim HT As Integer
Text1.Left = 0
Text1.Width = Picture1.Width
Text1.Height = Picture1.TextHeight("A")
Text1.Top = (Picture1.Height = Text1.Height) / 2
Text1.Visible = True
NumLines = 1
End Sub

6. Add the following code to the Change event for Text1:

Private Sub Text1_Change()
Dim Ret As Long
Dim HT As Integer
Ret = SendMessage(Text1.hWnd, EM_GETLINECOUNT, 0, ByVal 0&)
If Ret <> NumLines Then
HT = Picture1.TextHeight("A")
Text1.Height = HT * Ret
Text1.Top = (Picture1.Height - Text1.Height) / 2
NumLines = Ret
SendKeys "{PGUP}", True
Text1.SelStart = Len(Text1)
End If
End Sub

Tip 105: Removing a Form's Title Bar

Abstract
This article explains how you can remove the title bar from a window or form from
within a Microsoft® Visual Basic® application.

Modifying the Style Attributes of a Window
You can use two Microsoft® Windows® application programming interface (API)
functions GetWindowLong and SetWindowLong to modify the appearance of a window
when your Visual Basic® application is running.

These functions allow you to programmatically change one or more style bits
associated with a specific window. For example, you can remove a window's
title bar by changing the following style bits at run time:

WS_SYSMENU The window has a control menu on the left side of its title bar
WS_MINIMIZEBOX The window has a minimize box on the right side of its title bar
WS_MAXMIZEBOX The window has a maximize box on the right side of its title bar
WS_DLGFRAME The window has a double border, but does not have a title bar

First, we must first call GetWindowLong. This function reports the window style
associated with a window, among other pieces of information.
To use GetWindowLong, you must include its Declare statement in your program as
follows (note that the Declare statement must be typed as a single line of text):

Private Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer) As Long

The GetWindowLong function requires two arguments. The first argument is the
window's handle; the second argument specifies the type of information you
want to retrieve. In this case, we want to retrieve the window's style settings.
The current window style is returned as a long value after GetWindowLong is
called.

After we have retrieved the current window style for the window, we need to save
the original style value that was just retrieved so that we can later restore the
window's title bar, if desired. This is done by testing for the individual title
bar attributes and saving each in turn to a new OriginalStyle variable. Next, we
need to remove the attributes associated with the window's title bar. These
attributes are the Minimize and Maximize buttons, the control menu, and the
dialog box frame. We can remove them from the original window style value that
was just retrieved by using the bitwise AND NOT function. Finally, we can call
SetWindowLong to send this information to Windows, which causes the title bar
to be removed from the window.

Example Program

This program shows how you can remove and later restore a window's title bar.
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Command Button control to Form1. Command1 is created by default. Set
its Caption property to "Remove Title Bar".
3. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
RemoveTitleBar Form2
Form2.Show
End Sub

4. Add a second Command Button control to Form1. Command2 is created by default.
Set its Caption property to "Restore Title Bar".
5. Add the following code to the Click event for Command2:

Private Sub Command2_Click()
RestoreTitleBar Form2
Form2.Show
End Sub

6. From the Insert menu, select Form. Form2 is created by default. Adjust the
size of this form so that it is approximately half the size of Form1. Set
its AutoRedraw property to True and its Caption property to an empty (NULL)
string.
7. Add a Command Button control to Form2. Command1 is created by default. Set
its Caption property to "OK".
8. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
Form1.Show
Unload Form2
End Sub

9. From the Insert menu, select Module. Module1.Bas is created by default.
10. Add the following Constant and Declare statements to Module1.Bas (note that
each Declare statement must be typed as a single line of text):

Option Explicit
Private Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer) As Long
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const WS_DLGFRAME = &H400000
Const WS_SYSMENU = &H80000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000

11. Create a new function called RemoveTitleBar to Module1.Bas. Add the following
code to this function:

Sub RemoveTitleBar(frm As Form)
Static OriginalStyle As Long
Dim CurrentStyle As Long
Dim X As Long
OriginalStyle = 0
CurrentStyle = GetWindowLong(frm.hWnd, GWL_STYLE)

OriginalStyle = OriginalStyle Or (CurrentStyle And WS_DLGFRAME)
OriginalStyle = OriginalStyle Or (CurrentStyle And WS_SYSMENU)
OriginalStyle = OriginalStyle Or (CurrentStyle And WS_MINIMIZEBOX)
OriginalStyle = OriginalStyle Or (CurrentStyle And WS_MAXIMIZEBOX)

CurrentStyle = CurrentStyle And Not WS_DLGFRAME
CurrentStyle = CurrentStyle And Not WS_SYSMENU
CurrentStyle = CurrentStyle And Not WS_MINIMIZEBOX
CurrentStyle = CurrentStyle And Not WS_MAXIMIZEBOX

X = SetWindowLong(frm.hWnd, GWL_STYLE, CurrentStyle)
frm.Refresh
End Sub

12. Create a new function called RestoreTitleBar to Module1.Bas. Add the
following code to this function:

Sub RestoreTitleBar(frm As Form)
Static OriginalStyle As Long
Dim CurrentStyle As Long
Dim X As Long

CurrentStyle = GetWindowLong(frm.hWnd, GWL_STYLE)
CurrentStyle = CurrentStyle Or OriginalStyle
X = SetWindowLong(frm.hWnd, GWL_STYLE, CurrentStyle)
frm.Refresh
End Sub

Run this example program by pressing F5. Click the "Remove Title Bar" command
button. Form2 is displayed. Notice that the title bar has been removed from
the form. Click the OK command button, then click the Restore Title Bar command
button. Form2 is displayed again, this time with its title bar intact.

Additional References
"GetWindowLong." (Development Library, Product Documentation, SDKs, Windows 3.1
SDK, Programmer's Reference, Volume 2: Functions)
Knowledge Base Q77316."How to Determine Display State of a VB Form, Modal or
Modeless."
Knowledge Base Q83915. "SAMPLE: Adding and Removing Caption of a Window."
"SetWindowLong." (Development Library, Product Documentation, SDKs, Windows 3.1
SDK, Programmer's Reference Volume 2: Functions)