VB入门技巧N例
vb吧
全部回复
仅看楼主
level 1
iewanna 楼主
VB入门技巧N例 1. 如何消除textbox中按下回车时的beep声? Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0 End If End Sub 2.Textbox获得焦点时自动选中。 Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End Sub 3.屏蔽textbox控件自身的右键菜单,并显示自己的菜单。 方法一: Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _ As Single) If Button = 2 Then Text1.Enabled = False Text1.Enabled = True PopupMenu mymenu End If End Sub 方法二:回调函数 module: Option Explicit Public OldWindowProc As Long ' 保存默认的窗口函数的地址 Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,产生这条消息 Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal _ lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Function SubClass_WndMessage(ByVal hWnd As Long, ByVal Msg As Long, ByVal wp _ As Long, ByVal lp As Long) As Long ' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理 If Msg <> WM_CONTEXTMENU Then SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp) Exit Function End If SubClass_WndMessage = True End Function 窗体中: Private Const GWL_WNDPROC = (-4) Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _ As Single) If Button = 1 Then Exit Sub oldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) ' 取得窗口函数的地址 ' 用SubClass_WndMessage代替窗口函数处理消息 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage) End Sub Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub ' 恢复窗口的默认函数 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc) PopupMenu mymenu End Sub 4. 设置TEXTBOX为只读属性 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd _ As Long, ByVal nIndex As Long) As Long Private Const GWL_STYLE = (-16) Private Const EM_SETREADONLY = &HCF Private Sub Command1_Click() Dim l As Long If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &H800) Then Text1.Text = "This is a read/write text box." '文本窗口是只读窗口,设置为可读写窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull) Text1.BackColor = RGB(255, 255, 255) '将背景设置为白色 Command1.Caption = "Read&Write" 
2008年05月03日 11点05分 1
level 1
iewanna 楼主
Else Text1.Text = "This is a readonly text box." '文本窗口是可读写窗口,设置为只读窗口 2 VB入门技巧N例 l = SendMessage(Text1.hwnd, EM_SETREADONLY, True, vbNull) Text1.BackColor = vbInactiveBorder '将背景设置为灰色 Command1.Caption = "&ReadOnly" End If End Sub 5. 利用API函数MessageBox代替MSGBOX函数可以使得Timer控件正常工作 Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As _ Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long Private Sub Command1_Click() MsgBox "时钟变的无效了" End Sub Private Sub Command2_Click() MessageBox Me.hwnd, "时钟正常运行", "hehe", 0 End Sub Private Sub Timer1_Timer() Static i As Integer i = i + 1 Text1.Text = i End Sub 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 Public Sub SetOnTop(ByVal IsOnTop As Integer) Dim rtn As Long If IsOnTop = 1 Then rtn = SetWindowPos(Form1.hwnd, -1, 0, 0, 0, 0, 3) Else rtn = SetWindowPos(Form1.hwnd, -2, 0, 0, 0, 0, 3) End If End Sub Private Sub Command1_Click() SetOnTop 1 '将窗口置于最上面 End Sub Private Sub Command2_Click() SetOnTop 0 End Sub 6.只容许运行一个程序实例(利用互斥体) 选择启动对象为sub main() module: Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" _ (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName _ As String) As Long Public Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Public Const ERROR_ALREADY_EXISTS = 183& Private Sub Main() Dim sa As SECURITY_ATTRIBUTES sa.bInheritHandle = 1 sa.lpSecurityDescriptor = 0 sa.nLength = Len(sa) Debug.Print CreateMutex(sa, 1, App.Title) '这一行可千万不能删除啊 Debug.Print Err.LastDllError If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then MsgBox "More than one instance" Else Form1.Show End If End Sub 7.窗体标题栏闪烁 Option Explicit Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert _ As Long) As Long Private Sub tmrFlash_Timer() Static mFlash As Boolean FlashWindow hwnd, Not mFlash End Sub 8. 拷屏 方法一:利用模拟键盘 Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _ ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Const theScreen = 1 Const theForm = 0 Private Sub Command1_Click() Call keybd_event(vbKeySnapshot, theForm, 0, 0) '若theForm改成theScreen则Copy整个Screen DoEvents Picture1.Picture = Clipboard.GetData(vbCFBitmap) End Sub 9. 为程序注册热键 方法一:修改注册表 Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id _ 
2008年05月03日 11点05分 2
level 1
iewanna 楼主
As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id _ As Long) As Long Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, _ ByVal hWnd As Long, ByVal 3 VB入门技巧N例 wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal _ wRemoveMsg As Long) As Long Private Declare Function WaitMessage Lib "user32" () As Long Private Type POINTAPI x As Long y As Long End Type Private Type Msg hWnd As Long Message As Long wParam As Long lParam As Long time As Long pt As POINTAPI End Type ' 声明常数 Private Const MOD_ALT = &H1 Private Const MOD_CONTROL = &H2 Private Const MOD_SHIFT = &H4 Private Const PM_REMOVE = &H1 Private Const WM_HOTKEY = &H312 Private HotKey_Fg As Boolean Private Sub Form_Load() Dim Message As Msg '注册 Ctrl+Y 为热键 RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyY 'RegisterHotKey Me.hWnd, &HBFF2&, MOD_CONTROL, vbKeyU Me.Show Form1.Hide '等待处理消息 HotKey_Fg = False Do While Not HotKey_Fg '等待消息 WaitMessage '检查是否热键被按下 If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then Form1.Show 1 End If '转让控制权,允许操作系统处理其他事件 DoEvents Loop End Sub Private Sub Form_Unload(Cancel As Integer) HotKey_Fg = True '撤销热键的注册 Call UnregisterHotKey(Me.hWnd, &HBFFF&) End Sub 方法二:SendMessage 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 Const WM_SETHOTKEY = &H32 Private Const HOTKEYF_SHIFT = &H1 Private Const HOTKEYF_ALT = &H4 Private Sub Form_Load() Dim l As Long Dim wHotkey As Long wHotkey = (HOTKEYF_ALT) * (2 ^ 8) + 65 '定义ALT+A为热键 l = SendMessage(Me.hwnd, WM_SETHOTKEY, wHotkey, 0) End Sub 10.在状态栏显示无边框窗体图标。 Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long) As Long Const GWL_STYLE = (-16&) Const WS_SYSMENU = &H80000 Private Sub Form_Load() 'Make Form's Icon visible in the taskbar SetWindowLong Me.hWnd, GWL_STYLE, GetWindowLong(Me.hWnd, GWL_STYLE) Or WS_SYSMENU End Sub 11. 记录窗体的大小及位置和程序中的一些设置 Private Sub Form_Load() Me.Width = GetSetting(App.Title, Me.Name, "Width", 7200) Me.Height = GetSetting(App.Title, Me.Name, "Height", 6300) Me.Top = GetSetting(App.Title, Me.Name, "Top", 100) Me.Left = GetSetting(App.Title, Me.Name, "Left", 100) Check1.Value = GetSetting(App.Title, Me.Name, "check1", 0) 
2008年05月03日 11点05分 3
level 1
iewanna 楼主
End Sub Private Sub Form_Unload(Cancel As Integer) Call SaveSetting(App.Title, Me.Name, "Width", Me.Width) Call SaveSetting(App.Title, Me.Name, "Height", Me.Height) Call SaveSetting(App.Title, Me.Name, "Top", Me.Top) Call SaveSetting(App.Title, Me.Name, "Left", Me.Left) Call SaveSetting(App.Title, Me.Name, "check1", Check1.Value) End Sub 12. 解决mschart控件数据更改时的闪动现象 4 VB入门技巧N例 1、在有MSChart控件的窗体中另外加入一个PictureBox控件,如MSChart1和Picture1。 2、使Picture1和MSChart1大小一致,位置相同(通过左对齐和顶端对齐)。 3、使Picture1在MSChart1前端,设置Picture1的Visible为False,即不可见。只有刷新数据时Picture1才显示。 '刷新数据过程 Private Sub Refresh() Dim V_newchar() 'n维数组 …… Picture1.Visible = True MSChart1.ChartData = V_newchar '给MSChart1重新赋值,即刷新数据 MSChart1.EditCopy '将当前图表的图片复制到剪贴板中 Picture1.Picture = Clipboard.GetData() '给Picture1赋值剪贴板中的图片 End Sub 这样每一次刷新数据时Picture1显示的图片都不会产生闪烁现象 13. 无边框窗体的右键菜单 设计无边框窗体时,如果使用菜单编辑器,就会自动改变成有边框的窗体,此时,可以在另外一个窗体中(一般情况下你的程序应该不止一个 窗体的吧,如果真的只有一个,可以利用其他人写的类,添加右键)编辑菜单(VISIBLE属性设为FALSE),然后在本窗体中调用。调用形式如 下: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu Form2.mymenu End If End Sub 14.创建圆角无边框窗体 Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Integer, ByVal Y1 _ As Integer, ByVal X2 As Integer, ByVal Y2 As Integer, ByVal x3 As Integer, ByVal y3 As _ Integer) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hrgn As _ Long, ByVal bRedraw As Boolean) As Long Private Sub Form_Load() hround = CreateRoundRectRgn(0, 0, ScaleX(Form1.ScaleWidth, vbTwips, vbPixels), _ ScaleY(Form1.ScaleHeight, vbTwips, vbPixels), 20, 20) SetWindowRgn Me.hwnd, hround, True DeleteObject hround End Sub 15.拖动没有标题栏的窗体 方法一: Private Declare Function ReleaseCapture Lib "user32" () 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 Const HTCAPTION = 2 Private Const WM_NCLBUTTONDOWN = &HA1 Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim ncl As Long Dim rel As Long If Button = 1 Then i = ReleaseCapture() ncl = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) End If End Sub 方法二:回调函数 module: Public Const GWL_WNDPROC = (-4) Public Const WM_NCHITTEST = &H84 Public Const HTCLIENT = 1 Public Const HTCAPTION = 2 Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal _ lpPrevWndFunc As Long, ByVal hWnd As Long, 
2008年05月03日 11点05分 4
level 1
iewanna 楼主
ByVal Msg As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As _ Long, ByVal nIndex As Long) As Long Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As _ Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public prevWndProc As Long Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal _Param As Long) As Long 5 VB入门技巧N例 WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam) If Msg = WM_NCHITTEST And WndProc = HTCLIENT Then WndProc = HTCAPTION End If End Function 窗体中: Private Sub Form_Load() prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC) SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf WndProc End Sub Private Sub Form_Unload(Cancel As Integer) SetWindowLong Me.hWnd, GWL_WNDPROC, prevWndProc End Sub 16. 半透明窗体 Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, _ ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long Private Const WS_EX_LAYERED = &H80000 Private Const LWA_ALPHA = &H2 Private Const GWL_EXSTYLE = (-20) Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal _ hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal _ hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Sub Form_Load() Dim rtn As Long rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE) '取的窗口原先的样式 rtn = rtn Or WS_EX_LAYERED ' 使窗体添加上新的样式WS_EX_LAYERED SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn ' 把新的样式赋给窗体 SetLayeredWindowAttributes Me.hwnd, 0, 200, LWA_ALPHA End Sub 17.开机启动(函数及常数声明略) Private Sub Form_Load() Dim hKey As Long, SubKey As String, Exe As String SubKey = "Software\Microsoft\Windows\CurrentVersion\Run" Exe = "可执行文件的路径" RegCreateKey HKEY_CURRENT_USER, SubKey, hKey RegSetvalueEx hKey, "autorun", 0, REG_SZ, ByVal Exe,LenB(StrConv(Exe, vbFromUnicode)) + 1 RegCloseKey hKey End Sub 18.关闭显示器 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 Const WM_SYSCOMMAND = &H112& Const SC_MONITORPOWER = &HF170& Private Sub Command1_Click() SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 2& '关闭显示器 End Sub Private Sub Command2_Click() SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal -1& '打开显示器 End Sub 19. 在程序结束时自动关闭由SHELL打开的程序。 Private Const PROCESS_QUERY_INFORMATION = &H400 '关闭由SHELL函数打开的文件 Private Const PROCESS_TERMINATE = &H1 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal 
2008年05月03日 11点05分 5
level 1
iewanna 楼主
dwProcessId As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Dim ProcessId As Long Private Sub Command1_Click() ProcessId = Shell("notepad.exe.", vbNormalFocus) End Sub Private Sub Form_Unload(Cancel As Integer) Dim hProcess As Long hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, _ ProcessId) Call TerminateProcess(hProcess, 3838) End Sub 20. 关闭、重启计算机 Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal _ 6 VB入门技巧N例 uFlags As Long, ByVal dwReserved As Long) As Long ExitWindowsEx 1,0 关机 ExitWindowsEx 0,1 重新启动 21.显示关机提示框 Private Declare Function SHRestartSystemMB Lib "shell32" Alias "#59" (ByVal hOwner _ As Long, ByVal sExtraPrompt As String, ByVal uFlags As Long) As Long Const EWX_LOGOFF = 0 Const EWX_SHUTDOWN = 1 Const EWX_REBOOT = 2 Const EWX_FORCE = 4 Const EWX_POWEROFF = 8 Private Sub Command1_Click() SHRestartSystemMB Me.hWnd, PROMPT, EWX_LOGOFF End Sub 22.右键托盘图标后必须电击他才可以消失,怎么办? Case WM_RBUTTONUP '鼠标在图标上右击时弹出菜单 SetForegroundWindow Me.hwnd Me.PopupMenu mnuTray 加一句 SetForegroundWindow Me.hwnd 23. 将progressbar嵌入statusbar中 Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal _ hWndNewParent As Long) As Long Private Sub Command1_Click() With ProgressBar1 .Max = 1000 Dim i As Integer For i = 1 To 1000 .Value = i Next i End With End Sub Private Sub Form_Load() ProgressBar1.Appearance = ccFlat SetParent ProgressBar1.hWnd, StatusBar1.hWnd ProgressBar1.Left = StatusBar1.Panels(1).Left ProgressBar1.Top = 100 ProgressBar1.Width = StatusBar1.Panels(1).Width - 50 ProgressBar1.Height = StatusBar1.Height - 150 End Sub '相对位置你可以自己再调一下 25.如何打印PictureBox中的所有控件 添加另外一个PictureBox,然后: Private Const WM_PAINT = &HF Private Const WM_PRINT = &H317 Private Const PRF_CLIENT = &H4& Private Const PRF_CHILDREN = &H10& Private Const PRF_OWNED = &H20& Private Const PHYSICALOFFSETX As Long = 112 Private Const PHYSICALOFFSETY As Long = 113 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _ As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nindex _ As Long) As Long private Sub Form_Load() Picture1.AutoRedraw = True Picture2.AutoRedraw = True Picture2.BorderStyle = 0 Picture2.Visible = False End Sub Private Sub Command2_Click() Dim retval As Long, xmargin As Single, ymargin As Single Dim x As Single, y As Single x = 1: y = 1 With Printer .ScaleMode = vbInches xmargin = GetDeviceCaps(.hdc, PHYSICALOFFSETX) xmargin = (xmargin * .TwipsPerPixelX) / 1440 ymargin = GetDeviceCaps(.hdc, PHYSICALOFFSETY) 
2008年05月03日 11点05分 6
level 1
iewanna 楼主
ymargin = (ymargin * .TwipsPerPixelY) / 1440 Picture2.Width = Picture1.Width Picture2.Height = Picture1.Height DoEvents Picture1.SetFocus retval = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hdc, 0) retval = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hdc, _ PRF_CHILDREN + PRF_CLIENT + PRF_OWNED) DoEvents Printer.Print "" .PaintPicture Picture2.Image, x - xmargin, y - ymargin .EndDoc End With End Sub 26.冒泡排序如下: Sub BubbleSort(List() As Double) Dim First As Double, Last As Double Dim i As Integer, j As Integer Dim Temp As Double First = LBound(List) Last = UBound(List) For i = First To Last - 1 For j = i + 1 To Last If List(i) > List(j) Then 7 VB入门技巧N例 Temp = List(j) List(j) = List(i) List(i) = Temp End If Next j Next i End Sub 27.清空回收站 Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias _ "SHEmptyRecycleBinA" (ByVal hwnd As Long, ByVal pszRootPath As String, _ ByVal dwFlags As Long) As Long Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long Private Const SHERB_NOCONFIRMATION = &H1 Private Const SHERB_NOPROGRESSUI = &H2 Private Const SHERB_NOSOUND = &H4 Private Sub Command1_Click() Dim retval As Long ' return value retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOPROGRESSUI) ' 清空回收站, 确认 ' 若有错误出现,则返回回收站图示 If retval <> 0 Then ' error retval = SHUpdateRecycleBinIcon() End If End Sub Private Sub Command2_Click() Dim retval As Long ' return value ' 清空回收站, 不确认 retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOCONFIRMATION) ' 若有错误出现,则返回回收站图示 If retval <> 0 Then ' error retval = SHUpdateRecycleBinIcon() End If Command1_Click End 28.获得系统文件夹的路径 Private Declare Function GetSystemDirectory Lib "kernel32" Alias _ "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub Command1_Click() Dim syspath As String Dim len5 As Long syspath = String(255, 0) len5 = GetSystemDirectory(syspath, 256) syspath = Left(syspath, InStr(1, syspath, Chr(0)) - 1) Debug.Print "System Path : "; syspath End Sub 29.动态增加控件并响应事件 Option Explicit '通过使用WithEvents关键字声明一个对象变量为新的命令按钮 Private WithEvents NewButton As CommandButton '增加控件 Private Sub Command1_Click() If NewButton Is Nothing Then '增加新的按钮cmdNew Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me) '确定新增按钮cmdNew的位置 NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top NewButton.Caption = "新增的按钮" NewButton.Visible = True End If End Sub '删除控件(注:只能删除动态增加的控件) Private Sub Command2_Click() If NewButton Is Nothing Then Else Controls.Remove NewButton Set NewButton = Nothing End If End Sub '新增控件的单击事件 Private Sub NewButton_Click() MsgBox "您选中的是动态增加的按钮!" End Sub 30.得到磁盘序列号 Function GetSerialNumber(strDrive As String) As Long Dim SerialNum As Long Dim Res As Long Dim Temp1 As String Dim Temp2 As String Temp1 = String$(255, Chr$(0)) Temp2 = String$(255, Chr$(0)) Res = GetVolumeInformation(strDrive, Temp1, Len(Temp1), SerialNum, 0, 0, Temp2, _ Len(Temp2)) GetSerialNumber = SerialNum End Function 调用形式 Label1.Caption = GetSerialNumber("c:\") 31.打开屏幕保护 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 '我们将要调用的那个消息,在MSDN中搜索WM_SYSCOMMAND就可以找到具体说明 Const WM_SYSCOMMAND = &H112 '这个参数指明了我们让系统启动屏幕保护 Const SC_SCREENSAVE = &HF140& Private Sub Command1_Click() SendMessage Me.hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0 End Sub 36.如何打开光驱 Public Declare Function CDdoor Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Call CDdoor("set CDAudio door open", 0, 0, 0) '打开光驱 Call CDdoor("set CDAudio door closed", 0, 0, 0) '关闭光驱 
2008年05月03日 11点05分 7
level 1
iewanna 楼主
我是转的~~无聊耍耍酷的!呵呵!!
2008年05月03日 13点05分 8
level 0
好东西
2009年05月14日 10点05分 9
level 1
真的很谢谢,学习中………………
2009年05月15日 03点05分 10
level 2
虽然已是小case了,楼主好人
2009年05月15日 11点05分 12
1