|
-
Jun 28th, 2002, 12:31 PM
#1
Thread Starter
Addicted Member
Opening files with your program
If i made a text editor, for example, how would i make it so the user can click on my special .abc files and they would open up with MY editor? Possible in VB?
-
Jun 28th, 2002, 01:03 PM
#2
Hyperactive Member
Are your .abc files text files?
-
Jun 28th, 2002, 08:22 PM
#3
Hyperactive Member
You'd have to create a registry key under HKEY_CLASSES_ROOT called ".abc" and then make a new folder called "ShellEx" and do something there, I've forgotten, sorry. Try searching, it could be somewhere.
[vbcode]
' comment
Rem remark
[/vbcode]
-
Jun 28th, 2002, 10:15 PM
#4
Thread Starter
Addicted Member
AttnSue- its just an example, but yes (if you want, they can be txt files, i just want to know how to open them it my program)
Oafo- Thanks , thats a big help. Ill ask around about that.
Anyone else know how?
-
Jun 28th, 2002, 10:28 PM
#5
PowerPoster
Well
File Association 101:
VB Code:
'Make a new project. Add a module. To the form add a command button.
'Code:
'Add this code to the module:
Option Explicit
Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_INVALID_PARAMETER = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259
Public Const KEY_ALL_ACCESS = &H3F
Public Const REG_OPTION_NON_VOLATILE = 0
Public Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Public Declare Function RegCreateKeyEx _
Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal Reserved As Long, _
ByVal lpClass As String, _
ByVal dwOptions As Long, _
ByVal samDesired As Long, _
ByVal lpSecurityAttributes As Long, _
phkResult As Long, _
lpdwDisposition As Long) As Long
Public Declare Function RegOpenKeyEx _
Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long
Public Declare Function RegSetValueExString _
Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
ByVal lpValue As String, _
ByVal cbData As Long) As Long
Public Declare Function RegSetValueExLong _
Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
lpValue As Long, _
ByVal cbData As Long) As Long
Public Sub CreateAssociation()
Dim sPath As String
'File Associations begin with a listing
'of the default extension under HKEY_CLASSES_ROOT.
'So the first step is to create that
'root extension item
CreateNewKey ".xxx", HKEY_CLASSES_ROOT
'To the extension just added, add a
'subitem where the registry will look for
'commands relating to the .xxx extension
'("MyApp.Document"). Its type is String (REG_SZ)
SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
'Create the 'MyApp.Document' item under
'HKEY_CLASSES_ROOT. This is where you'll put
'the command line to execute or other shell
'statements necessary.
CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
'Set its default item to "MyApp Document".
'This is what is displayed in Explorer against
'for files with a xxx extension. Its type is
'String (REG_SZ)
SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
'Finally, add the path to myapp.exe
'Remember to add %1 as the final command
'parameter to assure the app opens the passed
'command line item.
'(results in '"c:\LongPathname\Myapp.exe %1")
'Again, its type is string.
sPath = "c:\LongPathname\Myapp.exe %1"
SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
'All done
MsgBox "The file association has been made!"
End Sub
Public Function SetValueEx(ByVal hKey As Long, _
sValueName As String, _
lType As Long, _
vValue As Variant) As Long
Dim nValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hKey, _
sValueName, _
0&, _
lType, _
sValue, _
Len(sValue))
Case REG_DWORD
nValue = vValue
SetValueEx = RegSetValueExLong(hKey, _
sValueName, _
0&, _
lType, _
nValue, _
4)
End Select
End Function
Public Sub CreateNewKey(sNewKeyName As String, _
lPredefinedKey As Long)
'handle to the new key
Dim hKey As Long
'result of the RegCreateKeyEx function
Dim r As Long
r = RegCreateKeyEx(lPredefinedKey, _
sNewKeyName, 0&, _
vbNullString, _
REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, 0&, hKey, r)
Call RegCloseKey(hKey)
End Sub
Public Sub SetKeyValue(sKeyName As String, _
sValueName As String, _
vValueSetting As Variant, _
lValueType As Long)
'result of the SetValueEx function
Dim r As Long
'handle of opened key
Dim hKey As Long
'open the specified key
r = RegOpenKeyEx(HKEY_CLASSES_ROOT, _
sKeyName, 0, _
KEY_ALL_ACCESS, hKey)
r = SetValueEx(hKey, _
sValueName, _
lValueType, _
vValueSetting)
Call RegCloseKey(hKey)
End Sub
'Add this code to the command button:
Private Sub Command1_Click()
CreateAssociation
End Sub
To get list of associations on your system :
VB Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const MAX_PATH As Long = 260
Private Const HKEY_CLASSES_ROOT As Long = &H80000000
Private Const ERROR_SUCCESS As Long = 0
Private Const vbDot As Long = 46
Private Const SHGFI_USEFILEATTRIBUTES As Long = &H10
Private Const SHGFI_TYPENAME As Long = &H400
Private Const LB_SETTABSTOPS As Long = &H192
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub GetAssociatedFileListing()
Dim dwIndex As Long
Dim sTypeName As String
Dim sSubkey As String * MAX_PATH
Dim sClass As String * MAX_PATH
Dim ft As FILETIME
Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, _
dwIndex, _
sSubkey, _
MAX_PATH, _
0, sClass, _
MAX_PATH, ft) = ERROR_SUCCESS
If Asc(sSubkey) = vbDot Then
'Pass the returned string to get the file type
sTypeName = GetFileType(sSubkey)
If Len(sTypeName) > 0 Then
List1.AddItem TrimNull(sSubkey) & vbTab & sTypeName
End If
End If
dwIndex = dwIndex + 1
Loop
End Sub
Private Function GetFileType(sFile As String) As String
'If successful returns the specified file's
'typename, returns an empty string otherwise.
'sFile does not have to exist and can be
'just a file extension.
Dim sfi As SHFILEINFO
If SHGetFileInfo(sFile, 0&, _
sfi, Len(sfi), _
SHGFI_TYPENAME Or SHGFI_USEFILEATTRIBUTES) Then
GetFileType = TrimNull(sfi.szTypeName)
End If
End Function
Private Function TrimNull(startstr As String) As String
'returns the string up to the first
'null, if present, or the passed string
Dim pos As Integer
pos = InStr(startstr, Chr$(0))
If pos Then
TrimNull = Left$(startstr, pos - 1)
Exit Function
End If
TrimNull = startstr
End Function
Private Sub Command1_Click()
List1.Clear
Screen.MousePointer = vbHourglass
'fill the listbox box with the file types and their extensions
Call GetAssociatedFileListing
Screen.MousePointer = vbDefault
End Sub
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 29th, 2002, 06:08 PM
#6
Thread Starter
Addicted Member
Thanks James! Some day ill be smart like you...
-
Jun 29th, 2002, 06:11 PM
#7
PowerPoster
Well
Not smart I just know where to look (and how to store snippets of code)...
Glad I could help...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|