|
Creating Transparent Forms
Abstract
When developing an application in Microsoft Visual Basic原 you may need
to design
a transparent form. This article explains how to create transparent forms.
Using Transparent Forms
A transparent form is a form that, when displayed, does not cover up the
underlying windows beneath it. The Microsoft Windows application programming
interface (API) function SetWindowLong can be used to change the style
settings
of a form or window. You can create a transparent window by setting the
WS_EX_TRANSPARENT style bit.
To use the SetWindowLong function within your Microsoft Visual Basic program,
include the following Declare statement in the General Declarations section
of a
form (note that it must be typed as a single line of code):
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd
As Integer, ByVal
nIndex As Integer, ByVal dwNewLong As Long) As Long
The SetWindowLong function takes the following arguments:
hWnd An integer value containing the window's handle.
nIndex An integer value that describes the type of information you want
to set. This value may be one of the following:
GWL_EXSTYLE Set the extended window style
GWL_STYLE Set the window style
GWL_WNDPROC The window function's address
dwNewLong A long value containing the new style bits to be given to the
window.
Because you want to make the specified form transparent, you call the
SetWindowLong function with the nIndex argument set to GWL_EXSTYLE, specifying
the transparency style bit.
When you run the example program shown below, the form will be displayed
in
transparent mode.
Example Program
This program shows how to create a transparent form.
1. Create a new project in Visual Basic. Form1 is created by default.
Set the
form's Picture property to the ARCHES.BMP bitmap file (usually found in
the
\WINDOWS directory).
2. Add a Command Button control to Form1. Command1 is created by default.
Set
its Caption property to "Show Form".
3. Add the following code to the Click event for Command1:
Private Sub Command1_Click()
Form2.Show
End Sub
4. From the Visual Basic Insert menu, select Form to create a new form.
Form2
is created by default. Change the size of Form2 so that it is the same
size
as Form1. Position Form2 so that it is on top of Form1.
5. Add the following Constant and Declare statements to the General Declarations
section of Form2 (note that the Declare statement must be typed as one
single
line of code):
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd
As Integer, ByVal
nIndex As Integer, ByVal dwNewLong As Long) As Long
Const WS_EX_TRANSPARENT = &H20&
Const GWL_EXSTYLE = (-20)
6. Add the following code to the Form_Load event for Form2:
Private Sub Form_Load()
Dim Ret As Long
Ret = SetWindowLong(Form2.hWnd, GWL_EXSTYLE, WS_EX_TRANSPARENT)
End Sub
Run the example program by pressing F5. Click the "Show Form"
command button.
Form2 is displayed directly over Form1. It appears as if only Form1 is
displayed
because Form2 has the transparent attribute.
返回
Changing a Menu's
Shortcut Key at Run Time
Abstract
When you use the Menu Editor in Microsoft Visual Basic原 you can assign
a
shortcut or accelerator key to each individual menu entry. This article
shows
how you can change this shortcut key at run time within your Visual Basic
application.
Setting Shortcut Keys in Visual Basic Menus
The Microsoft Visual Basic Menu Editor lets you easily design a menu system
for your application. When your program is run, the user can click a menu
entry
to perform some kind of operation. As an alternative to using the mouse
to select
a menu entry, the user can type a keystroke combination such as ALT-S
to invoke
the menu selection. These keystroke combinations are called access or
accelerator
keys.
After you have designed your menu structure in the Visual Basic Menu Editor,
you
may decide to change the access key for a specific menu entry at run time.
The
example program below changes the ALT-F access key for the File menu to
ALT-E.
To change a menu entry's access key at run time, you need to process the
keystrokes at the form-level. That is, the KeyDown event for the underlying
form
will need to be monitored.
The KeyDown event in Visual Basic is triggered each time a user presses
a key on
the keyboard. The event is triggered for the control that has the focus.
In this
case, the control is the form that the menu is attached to.
The KeyDown event tells you which key or combination of keys was just
pressed on
the keyboard. The KeyCode argument gives you a unique number that identifies
each
individual key on the keyboard. For example, if the KeyCode value returned
is 9,
you know that the TAB key was just pressed.
In the example program below, you use the KeyDown event to determine if
the user
pressed the CTRL-E keystroke combination. If CTRL-E was pressed, the program
displays a message box telling you that the File menu item was selected.
In all
other cases, the KeyDown event simply ignores the incoming keystrokes.
Example Program
This program shows how to change a menu item's shortcut key from within
a Visual
Basic application.
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the General Declarations section of Form1:
Option Explicit
Dim ShortCut As String * 1
3. From the Visual Basic Tools menu, click Menu Editor to create a single
menu
item. In the Caption field, type "&File", and in the Name
field, type
"mnuFile". Click OK to create the menu structure and to return
to the design
mode in Visual Basic.
4. Add the following code to the Form_Load event for Form1:
Private Sub Form_Load()
Command1.Caption = "Change ShortCut"
KeyPreview = True
End Sub
5. Add the following code to the KeyDown event for Form1:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift And 2 <> 2 Then Exit Sub
If KeyCode = Asc(ShortCut) Then
mnuFile_Click
End If
End Sub
6. Add the following code to the Click event for mnuFile:
Private Sub mnuFile_Click()
MsgBox "Menu was selected"
End Sub
7. Add a Command Button control to Form1. Command1 is created by default.
Set
its Caption property to "Change Item".
8. Add the following code to the Click event for Command1:
Private Sub Command1_Click()
ShortCut = "E"
mnuFile.Caption = "Fil" & "&" & LCase$(ShortCut)
End Sub
Run the example program by pressing F5. Notice the menu at the top of
the form.
The menu entry says "File" with the letter F underlined. The
letter F is the menu
entry's access key. Click the command button. The menu changes to "File"
with the
letter e designated as the access key. Press CTRL-E on the keyboard; a
message
box pops up saying that that menu item was just clicked.
返回
如何关闭程序
如果你知道窗口的标题,就可以使用FindWindow 和函数PostMessage 来关闭窗口
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "The Calculator is not open."
End If
For this code to work, you
must have declared the API functions in a module in your project. You
must put the following in the declarations section of the module.
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H1
返回
Changing
the Displayed Icon in the About Dialog Box
Abstract
In Microsoft Visual Basic version 4.0, the Microsoft Windows俺
application programming interface (API) ShellAbout function lets you
display the standard Microsoft Windows About dialog box seen in
applications such as Notepad and Explorer. This article explains how to
use an icon other than the default Microsoft Windows icon that appears
in the About dialog box of your Visual Basic 4.0 application.
Selecting Icons for the ShellAbout Function
Many Microsoft Windows applications, such as Notepad and Explorer,
display a standard About dialog box. This About dialog box displays
information such as the name of the application, a copyright notice,
system information, or anything else the developer wants to include in
the window. Usually, the About dialog box is accessed from the
application's menu system. You can use the Microsoft Windows
application programming interface (API) ShellAbout function to
display an About dialog box in your Microsoft Visual Basic version
4.0 application.
To use the ShellAbout function, you must include the following Declare
statement in your Visual Basic project:
Private Declare Function ShellAbout Lib "shell32.dll" Alias
"ShellAboutA"
(ByVal hwnd As Long, ByVal szApp As String, ByVal szOtherStuff As String,
ByVal hIcon As Long) As Long
As you can see, the ShellAbout function requires four arguments, as
follows:
hWnd A long value containing the window handle.
szApp A string containing the text that appears in the title bar
of the About dialog box.
szOtherStuff A string containing the text that appears after the version
and copyright information.
hIcon A long value containing the handle of an icon resource. When
set to NULL, the ShellAbout function displays the default
Microsoft Windows icon.
You can tell the ShellAbout function to display your About dialog box
with
an icon of your choice rather than having the function use the Microsoft
Windows icon. You can do this by using the icon that is assigned to your
Visual Basic 4.0 application, for example. Then, when you call the
ShellAbout function, you specify the handle of that icon (Me.Icon) as
the hIcon argument to the function.
Example Program
This program shows how to change the icon that appears in the About dialog
box when using the ShellAbout function to create the dialog box.
1. Create a new project in Visual Basic. Form1 is created by default.
Set
the Icon property of Form1 to an icon of your choice.
2. Add the following Declare statement to the General Declarations
section of Form1 (note that this Declare statement must be typed as
a single line of code):
Private Declare Function ShellAbout Lib "shell32.dll" Alias
"ShellAboutA"
(ByVal hwnd As Long, ByVal szApp As String, ByVal szOtherStuff As String,
ByVal hIcon As Long) As Long
3. Add the following code to the Click event for Form1:
Private Sub Form_Click()
Call ShellAbout(Me.hwnd, App.Title, "My App's Details.", Me.Icon)
End Sub
Run the example program by pressing F5. Click the form to display the
standard About dialog box. Notice that the icon appearing in the About
dialog box is the icon you assigned to Form1 at design time.
返回
Closing All MDI
Child Windows at One Time
Abstract
This article explains how you can simultaneously close all child
windows of a running Visual Basic application.
Using the Count Property of MDI Forms
The multiple document interface (MDI) feature of Visual Basic allows
you to create applications that have multiple forms within a single
parent form. This allows you to use the multitasking functions of the
Windows operating system in your programs.
The Windows Notepad is an example of an MDI application. You can open
several text files at one time and move between each document with a
click of the mouse.
When you create a child form while your program is executing, you must
also remember to close all the open child windows before your
application terminates. Otherwise, you could cause some unforeseen
problems with other applications.
The count property of a control, such as a form, can be used to
determine how many members of that particular collection exist. In
this case, the collection refers to the child forms of the parent
form. We can, therefore, determine how many child forms exist in our
application program by executing a statement such as:
X = Forms.Count
After this statement executes, the variable X will contain the number
of child forms that we have created. It is important to decrement this
value by one because the count starts with the value of one, not zero.
Once we know how many child forms we have created within our
application program, we can use the TypeOf statement in a loop to
close each child form that exists. The TypeOf statement is used to
determine the type of object you are dealing with. In this case, we
want to find out if the object is a form (Form1, the name of the
child form).
The final step to removing the child forms from the parent form is to
use the Unload statement. Therefore, to remove all child forms from
our program while it is running, we simply check each object in the
form, making sure that it is indeed a child form of the MDI form, and
execute an Unload statement to close the form.
Example Program
The following program shows how to close all child forms at one time.
Run the program by pressing the F5 function key. The MDIForm1 form is
displayed. Double-click the client area of MDIForm1 to create a child
form (Form1). Do this until you have several child forms visible on
the screen. Click the "Close Children" menu option to close
all child
windows.
1. Create a new project in Visual Basic. Form1 is created by default.
Set the MDIChild property to True.
2. From Visual Basic's Insert menu, click "MDI Form" to create
a
Multiple Document Interface form. MDIForm1 is created by default.
3. Add the following code to the DblClick event for MDIForm1:
Private Sub MDIForm_DblClick()
Dim X As New Form1
X.Show
End Sub
4. From Visual Basic's Tools menu, click Menu Editor. Set the Caption
field to "&Close Children" and the Name field to "mnuClose".
5. Add the following code to the mnuClose_Click event:
Private Sub mnuClose_Click()
Dim X As Integer
For X = (Forms.Count - 1) To 0 Step -1
If TypeOf Forms(X) Is Form1 Then
Unload Forms(X)
End If
Next X
End Sub
6. From Visual Basic's Tools menu, select Project Options. Set the
StartUp Form to MDIForm1.
返回
建立可滚动的视区
下面的范例演示如何建立可滚动的视区以滚动显示比视区大的图象
Private Sub HScroll1_Change()
Picture2.Left = -HScroll1.Value
End Sub
Private Sub VScroll1_Change()
Picture2.Top = -VScroll1.Value
End Sub
Private Sub Form_Resize()
With Picture1
.Height = Form1.Height
.Width = Form1.Width
End With
Picture1.Move 0, 0, ScaleWidth - VScroll1.Width, _
ScaleHeight - HScroll1.Height
Picture2.Move 0, 0
With HScroll1
.Top = Picture1.Height
.Left = 0
.Width = Picture1.Width
.Max = Picture2.Width - Picture1.Width
End With
With VScroll1
.Top = 0
.Left = Picture1.Width
.Height = Picture1.Height
.Max = Picture2.Height - Picture1.Height
End With
VScroll1.Visible = (Picture1.Height < Picture2.Height)
HScroll1.Visible = (Picture1.Width < Picture2.Width)
End Sub
Private Sub Form_Load()
Picture1.Move 0, 0, ScaleWidth - VScroll1.Width, _
ScaleHeight - HScroll1.Height
With Picture2
.AutoSize = True
.Picture = LoadPicture("d:\vbcode\examples\demos\banner.jpg")
.Move 0, 0
End With
With HScroll1
.Top = Picture1.Height
.Left = 0
.Width = Picture1.Width
End With
With VScroll1
.Top = 0
.Left = Picture1.Width
.Height = Picture1.Height
End With
HScroll1.Max = Picture2.Width - Picture1.Width
VScroll1.Max = Picture2.Height - Picture1.Height
' HScroll1.LargeChange = HScroll1.Max / 10
' VScroll1.LargeChange = VScroll1.Max / 10
VScroll1.Visible = (Picture1.Height < Picture2.Height)
HScroll1.Visible = (Picture1.Width < Picture2.Width)
End Sub
返回
使指定窗口总处于其他窗口之上
将以下代码加入到Form中,这个Form就成为一个在其他所有窗口之上的窗口了.
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd
As Long, ByVal hWndInsertAfter
As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy
As Long, ByVal wFlags
As Long) As Long
Const HWND_TOPMOST = -1
Private Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, Me.Left / Screen.TwipsPerPixelX _
, Me.Top \ Screen.TwipsPerPixelY, Me.Width \ Screen.TwipsPerPixelX, _
Me.Height \ Screen.TwipsPerPixelY, 0
End Sub
返回
怎样使程序的标题条闪烁?
建立新的项目文件,添加模块文件,并填写如下代码:
Public Declare Function FlashWindow _
Lib "user32" (ByVal hwnd As Long, _
ByVal bInvert As Long) As Long
在窗体中添加两个按钮和一个计时器,并用设置以下属性:
command1.caption="开始"
command2.caption="停止"
timer1.interval=500 '每0.5秒闪烁一次
timer1.enabled=false
Private Sub Timer1_Timer()
a& = FlashWindow(Me.hwnd, 1)
End Sub
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
按F5运行程序。
返回
Cover the
Taskbar with a Window in Visual Basic
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:4.0,5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:kbVBp400 kbVBp500 kbVBp600 kbAPI kbUserGrp
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Standard, Professional, and Enterprise Editions,
32-bit only, for Windows, version 4.0
---------------------------------------------------------------------
SUMMARY
=======
The Windows taskbar has an AutoHide property that allows it to hide at
the
very edge of the screen, taking up very little space, until the mouse
is
positioned directly over it, which causes it reappear at it's normal size.
When you set the AutoHide property, a maximized window covers the entire
screen. If you do not set the AutoHide property, a maximized window covers
the entire screen except for the area occupied by the taskbar.
MORE INFORMATION
================
There is no programmatic way to change the AutoHide property in Microsoft
Windows. To size a Visual Basic form so that it fills the entire screen,
including covering the taskbar if it is showing, you must explicitly set
the height and width of the window.
In the following sample code, the SetWindowPos API enables the specified
window to cover the entire screen, including covering all taskbars. This
or
any similar approach only works if the window is not already maximized.
If
the window is currently maximized, you must first set the window to a
restored/normal state prior to using the SetWindowPos API.
Step-by-Step Procedures
-----------------------
1. Create a new Standard EXE project. Form1 is created by default.
2. Add a CommandButton to Form1.
3. Paste the following code into Form1's code window:
Private Sub Command1_Click()
Dim cx As Long
Dim cy As Long
Dim RetVal As Long
' Determine if screen is already maximized.
If Me.WindowState = vbMaximized Then
' Set window to normal size
Me.WindowState = vbNormal
End If
' Get full screen width.
cx = GetSystemMetrics(SM_CXSCREEN)
' Get full screen height.
cy = GetSystemMetrics(SM_CYSCREEN)
' Call API to set new size of window.
RetVal = SetWindowPos(Me.hwnd, HWND_TOP, 0, 0, cx, cy, _
SWP_SHOWWINDOW)
End Sub
4. Add a standard module to the Project menu.
5. Paste the following code into the module:
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long,
_
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Public Const HWND_TOP = 0
Public Const SWP_SHOWWINDOW = &H40
6. Run the project.
7. Click the CommandButton.
RESULT: The form will cover the entire screen, including any taskbars.
This works for all taskbars on the primary monitor, including the shell's
taskbar and any other taskbars (such as the Microsoft Office taskbar)
that
are written to use the standard Desktop Application Toolbar interface.
返回
如何使窗口不出现在Windows TaskBar上
在VB中建立一个窗口的话,在运行时窗口就会出现在TaskBar上,如图所示:

而在设计状态时将Form的ShowInTaskBar设置为False就可以使窗口不出现在TaskBar上,不过
该属性只能在设计状态时改变而不能在运行时改变(估计是改变了窗口的风格)
返回
阻止所有窗口已经关闭但程序没有退出的问题
有的时候即使你编写的程序在运行时所有的窗口都已经关闭但是程序还是没有退出,这时只要在你的窗口的ON_Unload事件中加入End语句就可以了。
另外你也可以使用下面的语句:
Dim objForm As Form
For Each objForm in Forms
objForm.Unload
Set objForm = Nothing
Next objForm
返回
使你的弹出菜单中的菜单项加重显示
我们知道使用PopupMenu函数可以在当前鼠标位置弹出菜单,但是如何让弹出菜单的某一项加重显示呢?下面的语句可以实现加重显示:
PopupMenu mnu, 0, , , mnuOpen
其中mnu为菜单名称,mnuOpen为需要加重显示的子菜单名称。
返回
Back to top
|