Contents:

Main
Tutorials
Examples
Source Code
FAQ
Files

Microsoft

 

Frequently Asked Questions

If you are stuck on something, this is the place to come. First check the previous questions because the likelihood is that someone's already had your problem. If your question/problem is not covered simply submit a question and I'll do my best to answer it or find someone who can.

ALL THE ANSWERS USING API CALLS ARE FOR THE WIN32 ENVIRONMENTS, UNLESS OTHERWISE STATED.

Also, all Answers are by me, unless oterwise stated. If you would like to answer questions, drop me a line, as I always appreciate some help.


Submit a Question by Email.

Contents of Questions:

  1. How do I play wave files from my program?
  2. How do I make a form always on top?
  3. What's the best way to center a form?
  4. How can I make an INI file for my program?
  5. Is it possible to use the Windows Registry, and if so how?

Answers:

1. How to play a wave file from with in a program Goto top

Firstly, to do this it is necessary to make a call from the windows API, so we must declare the function and related constants.

Place this code in the declarations section of a module.

Global Const SND_SYNC = &H0
Global
Const SND_ASYNC = &H1
Global
Const SND_NODEFAULT = &H2
Global
Const SND_LOOP = &H8
Global
Const SND_NOSTOP = &H10

Public Declare Function sndPlaySound% Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long)

This should all be on one line. Now, to make the programming neater add a Sub to the module which will play the sound:

Public Sub PlaySound(SoundName As String)
On Error Resume Next
'If can't find wav don't hang
Dim
wFlags%, X%
SoundName = App.Path & "\" & SoundName
wFlags% = SND_ASYNC Or SND_NODEFAULT
X% = sndPlaySound(SoundName, wFlags%)
End Sub

You will notice that the sound name is changed to the full path, by adding the application's path to the beginning. This means that when you call the wave file you need only include the actual file name.

Now to play a wave file simply put this code somewhere in your program

Call PlaySound ("mywav.wav")

Where mywav.wav is in the application's directory.


2. How to make a form, always on top. Goto top

Again, to do this we must call an API function. This is the declaration and should be placed in a module.

Global Const SWP_NOMOVE = 2
Global Const
SWP_NOSIZE = 1
Global Const
HWND_TOPMOST = -1
Global Const
HWND_NOTOPMOST = -2
Global Const
FLOAT = 1, SINK = 0

Public Declare Sub
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)

And again, this declaration should be on one line.

This Sub should be placed in the module, and is what is called by your program.

Sub FloatWindow(X As Integer, action As Integer)

' When called by a form:
'
' If action <> 0 makes the form float (always on top)
' If action = 0 "unfloats" the window.


Dim
wFlags As Integer, result As Integer
wFlags = SWP_NOMOVE Or SWP_NOSIZE

If
action <> 0 Then ' Float
Call
SetWindowPos(X, HWND_TOPMOST, 0, 0, 0, 0, wFlags)
Else
' Sink
Call
SetWindowPos(X, HWND_NOTOPMOST, 0, 0, 0, 0, wFlags)
End If

End Sub

Then to call the procedure there are to options; the first to "float" the form, this makes it Always on Top, the second "sink", this makes it not Always on top.

To Float a form :

Dim f As Integer
f = Me.hWnd
Call
FloatWindow(f, FLOAT)

To Sink a form :

Dim f As Integer
f = Me.hWnd
Call
FloatWindow(f, FLOAT)


3. What's the best way to center a form? Goto top

I'm not saying this is the best way, but it's definitely one of the easiest.

Simply add this Sub in a module or the declaration's section of the form.

Public Sub CenterForm(MeForm As Form)
MeForm.Left = (Screen.Width / 2) - (MeForm.Width / 2)
MeForm.Top = (Screen.Height / 2) - (MeForm.Height / 2)

End Sub

Then to center the current form use this code:

Centerform Me

or to center an MDIs active form use

Centerform MDIForm1.ActiveForm 'Where MDIForm1 is the form's name


4. How to use an INI file with your program Goto top

This is another example that uses the Windows API. Put these functions in the Declarations section of a form or Module.

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnSring As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Long

Again, make sure it's all on one line.

Now to "GET" information from an INI use the following code:

Dim FileName As String, lpAppName As String, lpKeyName As String * 20
Dim
lpDefault As String, Temp As String * 30, X As Long

FileName = "MY_INI.INI" 'INI's File Name
lpAppName = "START-UP" 'Section of INI File

lpKeyName = "FILE1" ' Attribute name
lpDefault = "unknown" ' Default value if not present
X = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, Temp, Len(Temp), FileName)
LastFileName1 = Temp

So, this will get data from a file called MY_INI.INI, the section START_UP and Key Name FILE. If there is no entry under this name it will return a value of "unknown". The value is storred as LastFileName.

The INI file might look like this:

[START-UP]
FILE1=c:\mytext.txt
FILE2=c:\win.ini
FILE3=a:\bootlog.log

[OPTIONS]
COLOR=blue
FONT=Times New Roman
SIZE=12

etc. etc.

As, you can probably guess it is possible to "get" values from the WIN.INI and the SYSTEM.INI.

Heres, how to "SAVE" a value to an INI.

Dim FileName As String, lpAppName As String, lpKeyName As String * 20
Dim
lpDefault As String, Temp As String * 30, X As Long

FileName = "MYINI.INI" ' File name
lpAppName = "START-UP"
' Section

lpKeyName = "FILE1"
' Attriubute
Temp = LastFileName1
' Value
X = WritePrivateProfileString(lpAppName, lpKeyName, Temp, FileName)

Easy, eh?

Oh, I should say. Unless a specific path is specified the INI file will be located in the windows directory.


5. How to access the Windows Registry (only works with Win32s) Goto top

This is actually an easier way to save data than using an INI file.

To Get a setting:

Dim X As String
X = GetSetting("appname", "section", "key", "default")

To Set a setting:

SaveSetting "appname", "section", "key", value

Like the INI file with the GetSetting, if no value is there then it returns the default.