编程打开我的电脑

类别:VC语言 点击:0 评论:0 推荐:

编程打开我的电脑

许多工具类软件都有一些方便用户操作电脑的功能,如点个按钮或按一下热键

就实现关机功能,或更改墙纸,或缩定机器,等等.

这类软件一般会有一个浮动工具条,象金山快译的那种,或停在系统托盘里,

大大方便了用户.

但是,有些功能虽然没有什么技术难度,却难以从一般的资料中找到,如编程打开我的电脑,下面我就给出实现该功能的代码.

Visual Basic的代码用了一种比较繁琐的过程,

不过如果你想执行更多的相关功能,有时就非次不可啦!

Const SW_SHOWNORMAL = 1

Public Const CSIDL_DESKTOP = &H0                 '{desktop}
Public Const CSIDL_INTERNET = &H1                'Internet Explorer (icon on desktop)
Public Const CSIDL_PROGRAMS = &H2                'Start Menu\Programs
Public Const CSIDL_CONTROLS = &H3                'My Computer\Control Panel
Public Const CSIDL_PRINTERS = &H4                'My Computer\Printers
Public Const CSIDL_PERSONAL = &H5                'My Documents
Public Const CSIDL_FAVORITES = &H6               '{user name}\Favorites
Public Const CSIDL_STARTUP = &H7                 'Start Menu\Programs\Startup
Public Const CSIDL_RECENT = &H8                  '{user name}\Recent
Public Const CSIDL_SENDTO = &H9                  '{user name}\SendTo
Public Const CSIDL_BITBUCKET = &HA               '{desktop}\Recycle Bin
Public Const CSIDL_STARTMENU = &HB               '{user name}\Start Menu
Public Const CSIDL_DESKTOPDIRECTORY = &H10       '{user name}\Desktop
Public Const CSIDL_DRIVES = &H11                 'My Computer
Public Const CSIDL_NETWORK = &H12                'Network Neighborhood
Public Const CSIDL_WINDOWS = &H24                 'GetWindowsDirectory()
Public Const CSIDL_SYSTEM = &H25                  'GetSystemDirectory()
Public Const CSIDL_PROGRAM_FILES = &H26           'C:\Program Files

Public Const MAX_PATH = 260
Public Const S_OK = 0

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal
lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String,
ByVal nShowCmd As Long) As Long

Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias
"SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal
hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long

Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)

--
' got from website:
Private Function GetSpecialFolderLocation(CSIDL As Long) As String

     Dim sPath As String
     Dim pidl As Long

    'fill the idl structure with the specified folder item
     If SHGetSpecialFolderLocation(0, CSIDL, pidl) = S_OK Then

       'if the pidl is returned, initialize
       'and get the path from the id list
        sPath = Space$(MAX_PATH)

        If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then

          'return the path
           GetSpecialFolderLocation = Left(sPath, InStr(sPath, Chr$(0)) - 1)

        End If

       'free the pidl
        Call CoTaskMemFree(pidl)

      End If

  End Function

Sub ShellExecuteDemo()
'
ShellExecute 0, "explore", "", "", GetSpecialFolderLocation(CSIDL_DRIVES),
SW_SHOWNORMAL

End Sub

本文地址:http://com.8s8s.com/it/it2539.htm