|
VB 6 Helper Newsletter
|
Rod Stephens
|
Sep 29, 2008 10:15 PDT
|
My latest DevX article is now available. It turned out to be a pretty
good article and I strongly encourage you to take a look. It shows how
to use all of the CPUs on your system and almost doubled the performance
of several of my applications practically instantly.
Getting Started with the .NET Task Parallel Library
http://www.devx.com/dotnet/Article/39204
If you have a multi-core computer, chances are your first CPU does
all the work while the others spend most of their time twiddling their
electronic thumbs. Learn to unlock the idle power of your underused CPUs
to greatly improve the performance of your applications.
This article introduces the Task Parallel Library, which allows you
to use all of the CPUs available on your computer relatively easily.
Even if you don't have a multi-CPU system, now, you can use the library
with very little overhead to prepare for the day when you do!
A follow-up article that discusses my experiences and results while
converting several CPU-intensive applications to use TPL should post
soon, possibly later today.
----------
James Garner wrote DS Game Maker in Visual Basic 2008 with the .NET
Framework 3.5. It;s an impressive-looking toolkit for building Nintendo
DS games. Give it a try and let me know if you like it!
----------
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
----------
*** Now Available ***
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB6 Contents:
1. Updated HowTo: Randomly change the desktop background in Visual Basic
6
2. Updated HowTo: See if the computer is connected to the Internet in
Visual Basic 6
==========
++++++++++
<VB6>
++++++++++
==========
1. Updated HowTo: Randomly change the desktop background in Visual Basic
6
http://www.vb-helper.com/howto_backer.html
http://www.vb-helper.com/HowTo/howto_backer.zip
This example demonstrates many useful techniques including:
- Picking a file from a random list.
- Setting the desktop wallpaper.
- Setting the desktop wallpaper style (centered, tiled, or
stretched).
- Writing Registry entries.
- Moving a file into the wastebasket.
- Editing a file with the system's default editor.
When the program starts (and when you click the Apply button), the
program calls ReadFiles. That routine reads the names of the files in
the indicated directory and saves those that end in BMP, GIF, JPG, and
JPEG. After it loads all the file names, the routine calls
RandomizeNames to randomize the list.
Sub ReadFiles()
Dim file As String
Dim ext As String
' Create the new file name collection.
Set FileNames = New Collection
' Get the file names.
file = Dir(DirName & "\*.*")
Do While file <> ""
If LCase$(file) <> "temp.bmp" Then
ext = UCase$(Right$(file, 4))
If ext = ".BMP" Or ext = ".GIF" Or _
ext = ".JPG" Or ext = "JPEG" _
Then _
FileNames.Add file
End If
file = Dir()
Loop
NumNames = FileNames.Count
RandomizeNames
End Sub
Subroutine RandomizeNames makes an array of indexes with one entry for
each name in the FileNames collection. For i = 1 to NumNames - 1, the
routine selects a random index and swaps it into position i.
Private Sub RandomizeNames()
Dim idx As Integer
Dim tmp As Integer
Dim i As Integer
ReDim Indexes(1 To NumNames)
For i = 1 To NumNames
Indexes(i) = i
Next i
' Randomize them.
For i = 1 To NumNames - 1
idx = Int((NumNames - i + 1) * Rnd + i)
tmp = Indexes(i)
Indexes(i) = Indexes(idx)
Indexes(idx) = tmp
Next i
' Point to the index to display.
NextIndex = 1
End Sub
When a Timer fires, the program calls ShowFile to display the next file
in the randomized list.
Private Sub SwitchTimer_Timer()
Dim secs As Long
Dim pic As Integer
' See if it's time yet.
secs = DateDiff("s", Now, NextTime)
If secs <= 1 Then
If FileNames.Count > 1 Then
pic = Indexes(NextIndex)
NextIndex = NextIndex + 1
If NextIndex > NumNames Then RandomizeNames
ShowFile FileNames(pic)
End If
NextTime = DateAdd("s", Pause, Now)
secs = Pause
End If
If secs <= 60 Then
SwitchTimer.Interval = secs * 1000
Else
SwitchTimer.Interval = 60000
End If
SwitchTimer.Enabled = True
End Sub
Subroutine ShowFile checks the Style combo box and sets Registry entries
to make the desktop image centered, tiled, or stretched.
Next, if the file is a bitmap file, the program simply calls the
SystemParametersInfo API function to set the desktop background image.
If the file is not a bitmap file, the program loads it into a hidden
PictureBox and then saves the image as a bitmap file. Then it calls
SystemParametersInfo.
Private Sub ShowFile(ByVal file_name As String)
Const STYLE_CENTERED As String = "0"
Const STYLE_TILED As String = "1"
Const STYLE_STRETCHED As String = "2"
Const TILE_NO As String = "0"
Const TILE_YES As String = "1"
Dim had_error As Boolean
' Set the display style.
had_error = False
Select Case cboStyle.Text
Case "Centered"
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "TileWallpaper", TILE_NO) _
Then had_error = True
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "WallpaperStyle",
STYLE_CENTERED) _
Then had_error = True
Case "Tiled"
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "TileWallpaper", TILE_YES) _
Then had_error = True
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "WallpaperStyle", STYLE_TILED)
_
Then had_error = True
Case "Stretched"
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "TileWallpaper", TILE_NO) _
Then had_error = True
If SetRegistryValue(HKEY_CURRENT_USER, _
"Control Panel\Desktop", "WallpaperStyle",
STYLE_STRETCHED) _
Then had_error = True
End Select
If had_error Then
MsgBox "Error saving desktop style to registry.", _
vbOKOnly, "Registry Error"
End If
' Display the file.
FileLabel.Caption = file_name
m_CurrentFile = DirName & "\" & file_name
If UCase$(Right$(file_name, 4)) = ".BMP" Then
SystemParametersInfo SPI_SETDESKWALLPAPER, _
0, m_CurrentFile, SPIF_UPDATEINIFILE
Else
HiddenPict.Picture = LoadPicture(m_CurrentFile)
SavePicture HiddenPict.Picture, DirName & "\temp.bmp"
SystemParametersInfo SPI_SETDESKWALLPAPER, _
0, DirName & "\temp.bmp", _
SPIF_UPDATEINIFILE
End If
End Sub
When you click the Edit button, the program uses the ShellExecute API
function to edit the current picture file.
Private Sub cmdEdit_Click()
ShellExecute ByVal 0&, "edit", m_CurrentFile, _
vbNullString, vbNullString, SW_SHOWMAXIMIZED
End Sub
When you click the Delete button, the program calls subroutine
DeleteFile to move the file into the wastebasket. It then displays the
next picture.
Private Sub cmdDelete_Click()
' Delete the file.
DeleteFile m_CurrentFile, False
' Display the next file.
cmdNext_Click
End Sub
Subroutine DeleteFile uses the SHFileOperation API function to move a
file into the wastebasket, optionally asking the user to confirm.
Public Sub DeleteFile(ByVal file_name As String, ByVal user_confirm As
Boolean)
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_DELETE
.pFrom = file_name
If user_confirm Then
' Make the user confirm.
.fFlags = FOF_ALLOWUNDO
Else
' Do not make the user confirm.
.fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
End If
End With
SHFileOperation op
End Sub
==========
2. Updated HowTo: See if the computer is connected to the Internet in
Visual Basic 6
http://www.vb-helper.com/howto_detect_net_connection.html
http://www.vb-helper.com/HowTo/howto_detect_net_connection.zip
The InternetGetConnectedState function returns 1 if an Internet
connection is present or 0 if it is not. The program simply checks the
return value and sets a label accordingly.
Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef
dwflags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
If InternetGetConnectedState(0, 0) = 1 Then
lblResult.Caption = "Connected"
Else
lblResult.Caption = "Not Connected"
End If
End Sub
Joe Sova adds:
This is a complex topic as there are many ways of doing this but none
are
without side effects.
1. The registry value
"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\RemoteAccess" is
True if Online but only for modems.
2. InternetGetConnectedState works great if you're not on a Local Area
Network (LAN) but if you are on a LAN this will always return True
whether you are online or not.
3. A direct ping to a URL is very effective but is very, very slow if
there is no connection. Unable to get the "No Connection Timeout" to
activate after 5 seconds instead of the default 30-45 seconds!
4. Downloading a webpage is probably the most effective but it is quite
awkward.
Thanks Joe!
I think the problem is the disconnected philosophy of the Internet. The
assumption is that a network link may disappear at any time. It makes
the network robust but makes it hard to know whether the network is gone
or just slow.
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
http://www.vb-helper.com/cgi-bin/mojo/mojo.cgi?flavor=archive&list=VB_Helper_Newsletter
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|