vb编程68例
本帖最后由 作者 于 2005-1-6 12:02:34 编辑 <br /><br /> 1. 如何消除textbox中按下回车时的beep声?<BR>Private Sub Text1_KeyPress(KeyAscii As Integer)<BR> If KeyAscii = 13 Then<BR> KeyAscii = 0<BR> End If<BR>End Sub2.Textbox获得焦点时自动选中。<BR>Private Sub Text1_GotFocus()<BR> Text1.SelStart = 0<BR> Text1.SelLength = Len(Text1.Text)<BR>End Sub
3.屏蔽textbox控件自身的右键菜单,并显示自己的菜单。<BR>方法一:<BR>Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _<BR>As Single)<BR> If Button = 2 Then<BR> Text1.Enabled = False<BR> Text1.Enabled = True<BR> PopupMenu mymenu<BR> End If<BR>End Sub
方法二:回调函数<BR>module:<BR>Option Explicit<BR>Public OldWindowProc As Long ' 保存默认的窗口函数的地址<BR>Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,产生这条消息<BR>Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _ <BR>As Long, ByVal nIndex As Long) As Long<BR>Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>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<BR>Public Function SubClass_WndMessage(ByVal hWnd As Long, ByVal Msg As Long, ByVal wp _<BR> As Long, ByVal lp As Long) As Long<BR>' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理<BR> If Msg <> WM_CONTEXTMENU Then<BR> SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)<BR> Exit Function<BR> End If<BR> SubClass_WndMessage = True<BR>End Function<BR>窗体中:<BR>Private Const GWL_WNDPROC = (-4)<BR>Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _ <BR>As Single)<BR> If Button = 1 Then Exit Sub<BR> OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) ' 取得窗口函数的地址<BR> ' 用SubClass_WndMessage代替窗口函数处理消息<BR> Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage)<BR>End Sub<BR>Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR> If Button = 1 Then Exit Sub<BR> ' 恢复窗口的默认函数<BR> Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc)<BR> PopupMenu mymenu<BR>End Sub 4. 设置TEXTBOX为只读属性<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _<BR>As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long<BR>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd _ As Long, ByVal nIndex As Long) As Long<BR>Private Const GWL_STYLE = (-16)<BR>Private Const EM_SETREADONLY = &HCF<BR>Private Sub Command1_Click()<BR> Dim l As Long<BR> If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &H800) Then<BR> Text1.Text = "This is a read/write text box." '文本窗口是只读窗口,设置为可读写窗口<BR> l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull)<BR> Text1.BackColor = RGB(255, 255, 255) '将背景设置为白色<BR> Command1.Caption = "Read&Write"<BR> Else<BR> Text1.Text = "This is a readonly text box." '文本窗口是可读写窗口,设置为只读窗口<BR> l = SendMessage(Text1.hwnd, EM_SETREADONLY, True, vbNull)<BR> Text1.BackColor = vbInactiveBorder '将背景设置为灰色<BR> Command1.Caption = "&ReadOnly"<BR> End If<BR>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<BR>Private Sub Command1_Click()<BR> MsgBox "时钟变的无效了"<BR>End Sub<BR>Private Sub Command2_Click()<BR> MessageBox Me.hwnd, "时钟正常运行", "hehe", 0<BR>End Sub<BR>Private Sub Timer1_Timer()<BR> Static i As Integer<BR> i = i + 1<BR> Text1.Text = i<BR>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 _<BR>As Long, ByVal wFlags As Long) As Long<BR>Public Sub SetOnTop(ByVal IsOnTop As Integer)<BR>Dim rtn As Long<BR> If IsOnTop = 1 Then <BR> rtn = SetWindowPos(Form1.hwnd, -1, 0, 0, 0, 0, 3)<BR> Else<BR> rtn = SetWindowPos(Form1.hwnd, -2, 0, 0, 0, 0, 3)<BR> End If<BR>End Sub<BR>Private Sub Command1_Click()<BR> SetOnTop 1 '将窗口置于最上面<BR>End Sub<BR>Private Sub Command2_Click()<BR> SetOnTop 0<BR>End Sub
7.只容许运行一个程序实例(利用互斥体)
选择启动对象为sub main()<BR>module:<BR>Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" _ (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName _<BR> As String) As Long<BR>Public Type SECURITY_ATTRIBUTES<BR> nLength As Long<BR> lpSecurityDescriptor As Long<BR> bInheritHandle As Long<BR>End Type<BR>Public Const ERROR_ALREADY_EXISTS = 183&<BR>Private Sub Main()<BR> Dim sa As SECURITY_ATTRIBUTES<BR> sa.bInheritHandle = 1<BR> sa.lpSecurityDescriptor = 0<BR> sa.nLength = Len(sa)<BR> Debug.Print CreateMutex(sa, 1, App.Title) '这一行可千万不能删除啊<BR> Debug.Print Err.LastDllError<BR> If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then<BR> MsgBox "More than one instance"<BR> Else<BR> Form1.Show<BR> End If<BR>End Sub
8.窗体标题栏闪烁<BR>Option Explicit<BR>Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert _<BR> As Long) As Long<BR>Private Sub tmrFlash_Timer()<BR> Static mFlash As Boolean<BR> FlashWindow hwnd, Not mFlash<BR>End Sub<BR>8. 拷屏
方法一:利用模拟键盘<BR>Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _ ByVal dwFlags As Long, ByVal dwExtraInfo As Long)<BR>Const theScreen = 1<BR>Const theForm = 0<BR>Private Sub Command1_Click()<BR>Call keybd_event(vbKeySnapshot, theForm, 0, 0) '若theForm改成theScreen则Copy整个Screen<BR>DoEvents<BR>Picture1.Picture = Clipboard.GetData(vbCFBitmap)<BR>End Sub 9. 为程序注册热键
方法一:修改注册表<BR>Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id _<BR> As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long<BR>Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id _<BR> As Long) As Long<BR>Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, _ ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal _ wRemoveMsg As Long) As Long<BR>Private Declare Function WaitMessage Lib "user32" () As Long<BR>Private Type POINTAPI<BR> x As Long<BR> y As Long<BR>End Type<BR>Private Type Msg<BR> hWnd As Long<BR> Message As Long<BR> wParam As Long<BR> lParam As Long<BR> time As Long<BR> pt As POINTAPI<BR>End Type<BR>' 声明常数<BR>Private Const MOD_ALT = &H1<BR>Private Const MOD_CONTROL = &H2<BR>Private Const MOD_SHIFT = &H4<BR>Private Const PM_REMOVE = &H1<BR>Private Const WM_HOTKEY = &H312<BR>Private HotKey_Fg As Boolean<BR>Private Sub Form_Load()<BR> Dim Message As Msg<BR> '注册 Ctrl+Y 为热键<BR> RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyY<BR> 'RegisterHotKey Me.hWnd, &HBFF2&, MOD_CONTROL, vbKeyU<BR> Me.Show<BR> Form1.Hide<BR> '等待处理消息<BR> HotKey_Fg = False<BR> Do While Not HotKey_Fg<BR> '等待消息<BR> WaitMessage<BR> '检查是否热键被按下<BR> If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then<BR> Form1.Show 1<BR> End If<BR> '转让控制权,允许操作系统处理其他事件<BR> DoEvents<BR> Loop<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> HotKey_Fg = True<BR> '撤销热键的注册<BR> Call UnregisterHotKey(Me.hWnd, &HBFFF&)<BR>End Sub
方法二:SendMessage<BR>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<BR>Private Const WM_SETHOTKEY = &H32<BR>Private Const HOTKEYF_SHIFT = &H1<BR>Private Const HOTKEYF_ALT = &H4<BR>Private Sub Form_Load()<BR> Dim l As Long<BR> Dim wHotkey As Long<BR> wHotkey = (HOTKEYF_ALT) * (2 ^ 8) + 65 '定义ALT+A为热键<BR> l = SendMessage(Me.hwnd, WM_SETHOTKEY, wHotkey, 0)<BR>End Sub
10.在状态栏显示无边框窗体图标。<BR>Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long) As Long<BR>Const GWL_STYLE = (-16&)<BR>Const WS_SYSMENU = &H80000<BR>Private Sub Form_Load()<BR>'Make Form's Icon visible in the taskbar<BR>SetWindowLong Me.hWnd, GWL_STYLE, GetWindowLong(Me.hWnd, GWL_STYLE) Or WS_SYSMENU<BR>End Sub
11. 记录窗体的大小及位置和程序中的一些设置<BR>Private Sub Form_Load()<BR> Me.Width = GetSetting(App.Title, Me.Name, "Width", 7200)<BR> Me.Height = GetSetting(App.Title, Me.Name, "Height", 6300)<BR> Me.Top = GetSetting(App.Title, Me.Name, "Top", 100)<BR> Me.Left = GetSetting(App.Title, Me.Name, "Left", 100)<BR> Check1.Value = GetSetting(App.Title, Me.Name, "check1", 0)<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> Call SaveSetting(App.Title, Me.Name, "Width", Me.Width)<BR> Call SaveSetting(App.Title, Me.Name, "Height", Me.Height)<BR> Call SaveSetting(App.Title, Me.Name, "Top", Me.Top)<BR> Call SaveSetting(App.Title, Me.Name, "Left", Me.Left)<BR> Call SaveSetting(App.Title, Me.Name, "check1", Check1.Value)<BR>End Sub
12. 解决mschart控件数据更改时的闪动现象<BR>1、在有MSChart控件的窗体中另外加入一个PictureBox控件,如MSChart1和Picture1。 <BR>2、使Picture1和MSChart1大小一致,位置相同(通过左对齐和顶端对齐)。 <BR>3、使Picture1在MSChart1前端,设置Picture1的Visible为False,即不可见。只有刷新数据时Picture1才显示。 <BR>'刷新数据过程 <BR>Private Sub Refresh() <BR>Dim V_newchar() 'n维数组 <BR>…… <BR>Picture1.Visible = True <BR>MSChart1.ChartData = V_newchar '给MSChart1重新赋值,即刷新数据 <BR> MSChart1.EditCopy '将当前图表的图片复制到剪贴板中 <BR>Picture1.Picture = Clipboard.GetData() '给Picture1赋值剪贴板中的图片 <BR>End Sub <BR>这样每一次刷新数据时Picture1显示的图片都不会产生闪烁现象<BR> 13. 无边框窗体的右键菜单<BR>设计无边框窗体时,如果使用菜单编辑器,就会自动改变成有边框的窗体,此时,可以在另外一个窗体中(一般情况下你的程序应该不止一个窗体的吧,如果真的只有一个,可以利用其他人写的类,添加右键)编辑菜单(VISIBLE属性设为FALSE),然后在本窗体中调用。调用形式如下:<BR>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>If Button = 2 Then<BR>PopupMenu Form2.mymenu<BR>End If<BR>End Sub
14.创建圆角无边框窗体<BR>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<BR>Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long<BR>Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hrgn As _ Long, ByVal bRedraw As Boolean) As Long<BR>Private Sub Form_Load()<BR> hround = CreateRoundRectRgn(0, 0, ScaleX(Form1.ScaleWidth, vbTwips, vbPixels), _ ScaleY(Form1.ScaleHeight, vbTwips, vbPixels), 20, 20)<BR>SetWindowRgn Me.hwnd, hround, True<BR>DeleteObject hround<BR>End Sub
15.拖动没有标题栏的窗体<BR>方法一:<BR>Private Declare Function ReleaseCapture Lib "user32" () As Long<BR>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<BR>Private Const HTCAPTION = 2<BR>Private Const WM_NCLBUTTONDOWN = &HA1<BR>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR> Dim ncl As Long<BR> Dim rel As Long<BR> If Button = 1 Then<BR> i = ReleaseCapture()<BR> ncl = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)<BR> End If<BR>End Sub<BR>方法二:回调函数<BR>module:<BR>Public Const GWL_WNDPROC = (-4)<BR>Public Const WM_NCHITTEST = &H84<BR>Public Const HTCLIENT = 1<BR>Public Const HTCAPTION = 2<BR>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<BR>Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As _<BR>Long, ByVal nIndex As Long) As Long<BR>Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As _<BR> Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Public prevWndProc As Long<BR>Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal _Param As Long) As Long<BR> WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam)<BR> If Msg = WM_NCHITTEST And WndProc = HTCLIENT Then<BR> WndProc = HTCAPTION<BR> End If<BR>End Function<BR>窗体中:<BR>Private Sub Form_Load()<BR> prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)<BR> SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf WndProc<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> SetWindowLong Me.hWnd, GWL_WNDPROC, prevWndProc<BR>End Sub<BR>16. 半透明窗体<BR>Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, _ ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long<BR>Private Const WS_EX_LAYERED = &H80000<BR>Private Const LWA_ALPHA = &H2<BR>Private Const GWL_EXSTYLE = (-20)<BR>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal _<BR> hwnd As Long, ByVal nIndex As Long) As Long<BR>Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal _<BR> hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Private Sub Form_Load()<BR> Dim rtn As Long<BR> rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE) '取的窗口原先的样式<BR> rtn = rtn Or WS_EX_LAYERED ' 使窗体添加上新的样式WS_EX_LAYERED<BR> SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn ' 把新的样式赋给窗体<BR> SetLayeredWindowAttributes Me.hwnd, 0, 200, LWA_ALPHA<BR>End Sub 17.开机启动(函数及常数声明略)<BR>Private Sub Form_Load()<BR> Dim hKey As Long, SubKey As String, Exe As String<BR> SubKey = "Software\Microsoft\Windows\CurrentVersion\Run"<BR> Exe = "可执行文件的路径" <BR> RegCreateKey HKEY_CURRENT_USER, SubKey, hKey<BR> RegSetvalueEx hKey, "autorun", 0, REG_SZ, ByVal Exe,LenB(StrConv(Exe, vbFromUnicode)) + 1<BR> RegCloseKey hKey<BR>End Sub
18.关闭显示器<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _<BR> As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long<BR>Const WM_SYSCOMMAND = &H112&<BR>Const SC_MONITORPOWER = &HF170&<BR>Private Sub Command1_Click()<BR> SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 2& '关闭显示器<BR>End Sub<BR>Private Sub Command2_Click()<BR> SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal -1& '打开显示器<BR>End Sub
19. 在程序结束时自动关闭由SHELL打开的程序。<BR>Private Const PROCESS_QUERY_INFORMATION = &H400 '关闭由SHELL函数打开的文件<BR>Private Const PROCESS_TERMINATE = &H1<BR>Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long<BR>Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _<BR> ByVal uExitCode As Long) As Long<BR>Dim ProcessId As Long<BR>Private Sub Command1_Click()<BR> ProcessId = Shell("notepad.exe.", vbNormalFocus)<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> Dim hProcess As Long<BR> hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, _ ProcessId)<BR> Call TerminateProcess(hProcess, 3838)<BR>End Sub
20. 关闭、重启计算机<BR>Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal _<BR> uFlags As Long, ByVal dwReserved As Long) As Long<BR>ExitWindowsEx 1,0 关机<BR>ExitWindowsEx 0,1 重新启动<BR> 21.显示关机提示框<BR>Private Declare Function SHRestartSystemMB Lib "shell32" Alias "#59" (ByVal hOwner _<BR> As Long, ByVal sExtraPrompt As String,
ByVal uFlags As Long) As Long<BR>Const EWX_LOGOFF = 0<BR>Const EWX_SHUTDOWN = 1<BR>Const EWX_REBOOT = 2<BR>Const EWX_FORCE = 4<BR>Const EWX_POWEROFF = 8<BR>Private Sub Command1_Click()<BR>SHRestartSystemMB Me.hWnd, PROMPT, EWX_LOGOFF<BR>End Sub
22.右键托盘图标后必须电击他才可以消失,怎么办?<BR>Case WM_RBUTTONUP '鼠标在图标上右击时弹出菜单<BR> SetForegroundWindow Me.hwnd<BR> Me.PopupMenu mnuTray<BR>加一句 SetForegroundWindow Me.hwnd
23. 将progressbar嵌入statusbar中<BR>Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal _ hWndNewParent As Long) As Long<BR>Private Sub Command1_Click()<BR> With ProgressBar1<BR> .Max = 1000<BR> Dim i As Integer<BR> For i = 1 To 1000<BR> .Value = i<BR> Next i<BR> End With<BR>End Sub<BR>Private Sub Form_Load()<BR> ProgressBar1.Appearance = ccFlat<BR> SetParent ProgressBar1.hWnd, StatusBar1.hWnd<BR> ProgressBar1.Left = StatusBar1.Panels(1).Left<BR> ProgressBar1.Top = 100<BR> ProgressBar1.Width = StatusBar1.Panels(1).Width - 50<BR> ProgressBar1.Height = StatusBar1.Height - 150<BR>End Sub '相对位置你可以自己再调一下
24.使你的程序界面具有XP风格<BR>产生一个和你的可执行程序同名的后缀为exe.manifest的文件,并和可执行文件放在同一路径中。<BR>代码中加入:<BR>Private Declare Sub InitCommonControls Lib "comctl32.dll" ()<BR>Private Sub Form_Initialize()<BR> InitCommonControls<BR>End Sub<BR>注意:<BR>1 工具栏控件一定要用Microsoft Windows Common Controls 5.0,而不要用Microsoft Windows Common Controls 6.0。因为此
InitCommonControls API函数是位于comctl32.dll(Microsoft Windows Common Controls 5.0控件的动态链接库中)。<BR>2 放在FRAME控件中的单远按钮有些“麻烦”!为了解决此问题,可以将单选按钮放在PICTURE控件中(以PICTURE控件作为容器),再将
PICTURE控件放在FRAME控件中,就可以了。<BR>3 必须编译之后才能看到效果<BR>exe.manifest文件中的内容,可用notepad编辑。<BR><?xml version="1.0" encoding="UTF-8" standalone="yes"?><BR><assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"><BR><assemblyIdentity<BR>version="1.0.0.0"<BR>processorArchitecture="X86"<BR>name="CompanyName.ProductName.YourApp"<BR>type="win32"<BR>/><BR><description>Your application description here.</description><BR><dependency><BR><dependentAssembly><BR><assemblyIdentity<BR>type="win32"<BR>name="Microsoft.Windows.Common-Controls"<BR>version="6.0.0.0"<BR>processorArchitecture="X86"<BR>publicKeyToken="6595b64144ccf1df"<BR>language="*"<BR>/><BR></dependentAssembly><BR></dependency><BR></assembly> 25.如何打印PictureBox中的所有控件
添加另外一个PictureBox,然后:<BR>Private Const WM_PAINT = &HF<BR>Private Const WM_PRINT = &H317<BR>Private Const PRF_CLIENT = &H4&<BR>Private Const PRF_CHILDREN = &H10&<BR>Private Const PRF_OWNED = &H20&<BR>Private Const PHYSICALOFFSETX As Long = 112<BR>Private Const PHYSICALOFFSETY As Long = 113<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _<BR> As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long<BR>Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nindex _<BR> As Long) As Long<BR>private Sub Form_Load()<BR> Picture1.AutoRedraw = True<BR> Picture2.AutoRedraw = True<BR> Picture2.BorderStyle = 0<BR> Picture2.Visible = False<BR>End Sub<BR>Private Sub Command2_Click()<BR> Dim retval As Long, xmargin As Single, ymargin As Single<BR> Dim x As Single, y As Single<BR> x = 1: y = 1<BR> With Printer<BR> .ScaleMode = vbInches<BR> xmargin = GetDeviceCaps(.hdc, PHYSICALOFFSETX)<BR> xmargin = (xmargin * .TwipsPerPixelX) / 1440<BR> ymargin = GetDeviceCaps(.hdc, PHYSICALOFFSETY)<BR> ymargin = (ymargin * .TwipsPerPixelY) / 1440<BR> Picture2.Width = Picture1.Width<BR> Picture2.Height = Picture1.Height<BR> DoEvents<BR> Picture1.SetFocus<BR> retval = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hdc, 0)<BR> retval = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hdc, _<BR> PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)<BR> DoEvents<BR> Printer.Print ""<BR> .PaintPicture Picture2.Image, x - xmargin, y - ymargin<BR> .EndDoc<BR> End With<BR>End Sub
26.冒泡排序如下:<BR>Sub BubbleSort(List() As Double)<BR>Dim First As Double, Last As Double<BR>Dim i As Integer, j As Integer<BR>Dim Temp As Double<BR>First = LBound(List)<BR>Last = UBound(List)<BR>For i = First To Last - 1<BR>For j = i + 1 To Last<BR>If List(i) > List(j) Then<BR>Temp = List(j)<BR>List(j) = List(i)<BR>List(i) = Temp<BR>End If<BR>Next j<BR>Next i<BR>End Sub<BR>27.清空回收站
Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias _<BR> "SHEmptyRecycleBinA" (ByVal hwnd As Long, ByVal pszRootPath As String, _<BR> ByVal dwFlags As Long) As Long<BR>Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long<BR>Private Const SHERB_NOCONFIRMATION = &H1<BR>Private Const SHERB_NOPROGRESSUI = &H2<BR>Private Const SHERB_NOSOUND = &H4<BR>Private Sub Command1_Click()<BR> Dim retval As Long ' return value<BR> retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOPROGRESSUI) ' 清空回收站, 确认<BR> ' 若有错误出现,则返回回收站图示<BR> If retval <> 0 Then ' error<BR> retval = SHUpdateRecycleBinIcon()<BR> End If<BR>End Sub<BR>Private Sub Command2_Click()<BR> Dim retval As Long ' return value<BR> ' 清空回收站, 不确认<BR> retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOCONFIRMATION)<BR> ' 若有错误出现,则返回回收站图示<BR> If retval <> 0 Then ' error<BR> retval = SHUpdateRecycleBinIcon()<BR> End If<BR> Command1_Click<BR>End Sub
<BR>28.获得系统文件夹的路径<BR>Private Declare Function GetSystemDirectory Lib "kernel32" Alias _<BR> "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long<BR>Private Sub Command1_Click()<BR> Dim syspath As String<BR> Dim len5 As Long<BR> syspath = String(255, 0)<BR> len5 = GetSystemDirectory(syspath, 256)<BR> syspath = Left(syspath, InStr(1, syspath, Chr(0)) - 1)<BR> Debug.Print "System Path : "; syspath<BR>End Sub 29.动态增加控件并响应事件<BR>Option Explicit<BR> '通过使用WithEvents关键字声明一个对象变量为新的命令按钮<BR> Private WithEvents NewButton As CommandButton<BR> '增加控件<BR> Private Sub Command1_Click()<BR> If NewButton Is Nothing Then<BR> '增加新的按钮cmdNew<BR> Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)<BR> '确定新增按钮cmdNew的位置<BR> NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top<BR> NewButton.Caption = "新增的按钮"<BR> NewButton.Visible = True<BR> End If<BR> End Sub<BR> '删除控件(注:只能删除动态增加的控件)<BR> Private Sub Command2_Click()<BR> If NewButton Is Nothing Then<BR> Else<BR> Controls.Remove NewButton<BR> Set NewButton = Nothing<BR> End If<BR> End Sub<BR> '新增控件的单击事件<BR> Private Sub NewButton_Click()<BR> MsgBox "您选中的是动态增加的按钮!"<BR> End Sub<BR> <BR>30.得到磁盘序列号<BR>Function GetSerialNumber(strDrive As String) As Long<BR> Dim SerialNum As Long<BR> Dim Res As Long<BR> Dim Temp1 As String<BR> Dim Temp2 As String<BR> Temp1 = String$(255, Chr$(0))<BR> Temp2 = String$(255, Chr$(0))<BR> Res = GetVolumeInformation(strDrive, Temp1, Len(Temp1), SerialNum, 0, 0, Temp2, _<BR> Len(Temp2))<BR> GetSerialNumber = SerialNum<BR>End Function<BR>调用形式 Label1.Caption = GetSerialNumber("c:\")
31.打开屏幕保护<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _<BR> As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long<BR>'我们将要调用的那个消息,在MSDN中搜索WM_SYSCOMMAND就可以找到具体说明<BR>Const WM_SYSCOMMAND = &H112<BR>'这个参数指明了我们让系统启动屏幕保护<BR>Const SC_SCREENSAVE = &HF140&<BR>Private Sub Command1_Click()<BR>SendMessage Me.hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0<BR>End Sub
<BR>32.获得本机IP地址<BR>方法一:利用Winsock控件<BR>winsockip.localip<BR>方法二:<BR>Private Const MAX_IP = 255<BR> Private Type IPINFO<BR> dwAddr As Long<BR> dwIndex As Long<BR> dwMask As Long<BR> dwBCastAddr As Long<BR> dwReasmSize As Long<BR> unused1 As Integer<BR> unused2 As Integer<BR> End Type<BR> Private Type MIB_IPADDRTABLE<BR> dEntrys As Long<BR> mIPInfo(MAX_IP) As IPINFO<BR> End Type<BR> Private Type IP_Array<BR> mBuffer As MIB_IPADDRTABLE<BR> BufferLen As Long<BR> End Type<BR> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination _<BR> As Any, Source As Any, ByVal Length As
Long)<BR> Private Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, _<BR> pdwSize As Long, ByVal Sort As Long) As Long<BR> Dim strIP As String<BR> Private Function ConvertAddressToString(longAddr As Long) As String<BR> Dim myByte(3) As Byte<BR> Dim Cnt As Long<BR> CopyMemory myByte(0), longAddr, 4<BR> For Cnt = 0 To 3<BR> ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."<BR> Next Cnt<BR> ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)<BR> End Function<BR> <BR> Public Sub Start()<BR> Dim Ret As Long, Tel As Long<BR> Dim bBytes() As Byte<BR> Dim Listing As MIB_IPADDRTABLE<BR> On Error GoTo END1<BR> GetIpAddrTable ByVal 0&, Ret, True<BR> If Ret <= 0 Then Exit Sub<BR> ReDim bBytes(0 To Ret - 1) As Byte<BR> GetIpAddrTable bBytes(0), Ret, False<BR> CopyMemory Listing.dEntrys, bBytes(0), 4<BR> strIP = "你机子上有 " & Listing.dEntrys & " 个 IP 地址。" & vbCrLf<BR> strIP = strIP & "------------------------------------------------" & vbCrLf & vbCrLf<BR> For Tel = 0 To Listing.dEntrys - 1<BR> CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len _(Listing.mIPInfo(Tel))<BR> strIP = strIP & "IP 地址 : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf<BR> Next<BR> Exit Sub<BR>END1:<BR> MsgBox "ERROR"<BR> End Sub<BR>Private Sub Form_Load()<BR> Start<BR> MsgBox strIP<BR>End Sub 33. 用键盘方向键控制COMBOX<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _<BR> (ByVal hwnd As Long, _<BR> ByVal wMsg As Long, _<BR> ByVal wParam As Long, _<BR> lParam As Any) As Long<BR>Const CB_SHOWDROPDOWN = &H14F<BR>Dim bDrop As Boolean<BR>Private isDo As Boolean<BR>Private Sub Combo1_Click()<BR>If Not isDo Then<BR> isDo = True '<----------回置状态<BR> Exit Sub<BR> Else: MsgBox "safd"<BR> End If<BR>End Sub<BR>Private Sub Combo1_DropDown()<BR> bDrop = True<BR>End Sub<BR>Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)<BR> If KeyCode = 40 Then<BR> isDo = False<BR> SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, 0<BR>ElseIf KeyCode = 38 Then<BR> isDo = False<BR> If Combo1.ListIndex = 0 Then<BR> If bDrop Then<BR> bDrop = False<BR> SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 0, 0<BR> End If<BR> End If<BR> End If<BR> End Sub<BR>Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)<BR>If Combo1.Text = Combo1.List(0) Then<BR>isDo = True<BR>End If<BR>End Sub<BR>Private Sub Form_Load()<BR> isDo = True<BR> Combo1.AddItem "abcd"<BR> Combo1.AddItem "abcd1"<BR> Combo1.AddItem "abcd2"<BR> Combo1.AddItem "abcd3"<BR>End Sub<BR>35.VB下的CRC校验程序<BR>一 计算法<BR>计算法就是依据CRC校验码的产生原理来设计程序。其优点是模块代码少,修改灵活,可移植性好。其缺点为计算量大。为了便于理解,这里假
定了三位数据,而多项式码为A001(hex)。<BR> 在窗体上放置一命令按钮Command1,并添加如下代码:
Private Sub Command1_Click()<BR> Dim CRC() As Byte<BR> Dim d() As Byte '待传输数据<BR> ReDim d(2) As Byte<BR> d(0) = 123<BR> d(1) = 112<BR> d(2) = 135<BR> CRC = CRC16(d) '调用CRC16计算函数<BR> 'CRC(0)为高位<BR> 'CRC(1)为低位<BR> End Sub<BR> 注意:在数据传输时CRC的低位可能在前,而高位在后。
Function CRC16(data() As Byte) As String<BR> Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器<BR> Dim CL As Byte, CH As Byte '多项式码&HA001<BR> Dim SaveHi As Byte, SaveLo As Byte<BR> Dim i As Integer<BR> Dim Flag As Integer<BR> CRC16Lo = &HFF<BR> CRC16Hi = &HFF<BR> CL = &H1<BR> CH = &HA0<BR> For i = 0 To UBound(data)<BR> CRC16Lo = CRC16Lo Xor data(i) '每一个数据与CRC寄存器进行异或<BR> For Flag = 0 To 7<BR> SaveHi = CRC16Hi<BR> SaveLo = CRC16Lo<BR> CRC16Hi = CRC16Hi \ 2 '高位右移一位<BR> CRC16Lo = CRC16Lo \ 2 '低位右移一位<BR> If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1<BR> CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1<BR> End If '否则自动补0<BR> If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或<BR> CRC16Hi = CRC16Hi Xor CH<BR> CRC16Lo = CRC16Lo Xor CL<BR> End If<BR> Next Flag<BR> Next i<BR> Dim ReturnData(1) As Byte<BR> ReturnData(0) = CRC16Hi 'CRC高位<BR> ReturnData(1) = CRC16Lo 'CRC低位<BR> CRC16 = ReturnData<BR> End Function
2.查表法<BR> 查表法的优缺点与计算法的正好相反。为了便于比较,这里所有的假定与计算法的完全相同,都而在窗体上放置一个Command1的按钮,其
代码部分与上面的也完全一致。下面只介绍CRC函数的编写源代码。
Private Function CRC16(data() As Byte) As String<BR> Dim CRC16Hi As Byte<BR> Dim CRC16Lo As Byte<BR> CRC16Hi = &HFF<BR> CRC16Lo = &HFF<BR> Dim i As Integer<BR> Dim iIndex As Long<BR> For i = 0 To UBound(data)<BR> iIndex = CRC16Lo Xor data(i)<BR> CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex) '低位处理<BR> CRC16Hi = GetCRCHi(iIndex) '高位处理<BR> Next i<BR> Dim ReturnData(1) As Byte<BR> ReturnData(0) = CRC16Hi 'CRC高位<BR> ReturnData(1) = CRC16Lo 'CRC低位<BR> CRC16 = ReturnData<BR> End Function
'CRC低位字节值表<BR> Function GetCRCLo(Ind As Long) As Byte<BR> GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40,
&H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1,
&H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80,
&H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0,
&HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0,
&H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81,
&H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _<BR>&H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80,
&H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0,
&HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1,
&H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81,
&H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1,
&HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40)<BR> End Function
'CRC高位字节值表<BR> Function GetCRCHi(Ind As Long) As Byte<BR> GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4,
&HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB,
&HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13,
&HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4,
&H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB,
&H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2,
&HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _<BR>&H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E,
&HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF,
&H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0, &H50,
&H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F,
&H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F,
&H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83, &H41, &H81, &H80, &H40)<BR> End Function
<BR>36.如何打开光驱<BR>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<BR>Call CDdoor("set CDAudio door open", 0, 0, 0) '打开光驱<BR>Call CDdoor("set CDAudio door closed", 0, 0, 0) '关闭光驱<BR> 37.检测是否以联网及联网方式<BR>module:<BR>Public Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _<BR> Alias "InternetGetConnectedStateExA" _<BR> (ByRef lpdwFlags As Long, _<BR> ByVal lpszConnectionName As String, _<BR> ByVal dwNameLen As Long, _<BR> ByVal dwReserved As Long _<BR> ) As Long
Public Enum EIGCInternetConnectionState<BR> INTERNET_CONNECTION_MODEM = &H1&<BR> INTERNET_CONNECTION_LAN = &H2&<BR> INTERNET_CONNECTION_PROXY = &H4&<BR> INTERNET_RAS_INSTALLED = &H10&<BR> INTERNET_CONNECTION_OFFLINE = &H20&<BR> INTERNET_CONNECTION_CONFIGURED = &H40&<BR>End Enum
Public Property Get InternetConnected( _<BR> Optional ByRef eConnectionInfo As EIGCInternetConnectionState, _<BR> Optional ByRef sConnectionName As String _<BR> ) As Boolean<BR> Dim dwFlags As Long<BR> Dim sNameBuf As String<BR> Dim lR As Long<BR> Dim iPos As Long<BR> <BR> sNameBuf = String$(513, 0)<BR> lR = InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&)<BR> eConnectionInfo = dwFlags<BR> iPos = InStr(sNameBuf, vbNullChar)<BR> If iPos > 0 Then<BR> sConnectionName = Left$(sNameBuf, iPos - 1)<BR> ElseIf Not sNameBuf = String$(513, 0) Then<BR> sConnectionName = sNameBuf<BR> End If<BR> InternetConnected = (lR = 1)<BR>End Property<BR>窗体中<BR>Private Sub Form_Load()<BR> ' Determine whether we have a connection:<BR> bConnected = InternetConnected(eR, sName)
' The connection state info parameter provides details<BR> ' about how we connect:<BR> If (eR And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM Then<BR> sMsg = sMsg & "Connection uses a modem." & vbCrLf<BR> End If<BR> If (eR And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then<BR> sMsg = sMsg & "Connection uses LAN." & vbCrLf<BR> End If<BR> If (eR And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then<BR> sMsg = sMsg & "Connection is via Proxy." & vbCrLf<BR> End If<BR> If (eR And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then<BR> sMsg = sMsg & "Connection is Off-line." & vbCrLf<BR> End If<BR> If (eR And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then<BR> sMsg = sMsg & "Connection is Configured." & vbCrLf<BR> Else<BR> sMsg = sMsg & "Connection is Not Configured." & vbCrLf<BR> End If<BR> If (eR And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then<BR> sMsg = sMsg & "System has RAS installed." & vbCrLf<BR> End If<BR> <BR> ' Display the connection name and info:<BR> If bConnected Then<BR> Text1.Text = "Connected: " & sName & vbCrLf & vbCrLf & sMsg<BR> Else<BR> Text1.Text = "Not Connected: " & sName & vbCrLf & vbCrLf & sMsg<BR> End If<BR>End Sub
38.得到当前windows的版本号
module:<BR>Type OSVERSIONINFO<BR> dwOSVersionInfoSize As Long<BR> dwMajorVersion As Long<BR> dwMinorVersion As Long<BR> dwBuildNumber As Long<BR> dwPlatformId As Long<BR> szCSDVersion As String * 128 ' Maintenance string for PSS usage<BR>End Type<BR>Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long<BR>Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long<BR>Public Const SM_CLEANBOOT = 67<BR>Public Const SM_DEBUG = 22<BR>Public Const SM_SLOWMACHINE = 73<BR>Public Const VER_PLATFORM_WIN32s = 0<BR>Public Const VER_PLATFORM_WIN32_WINDOWS = 1<BR>Public Const VER_PLATFORM_WIN32_NT = 2<BR>窗体中
Private Sub Form_Load()<BR>Dim myVer As OSVERSIONINFO<BR>Dim nl As String<BR>Dim q As Long<BR>nl = Chr(10) & Chr(13)<BR>myVer.dwOSVersionInfoSize = 148<BR>q& = GetVersionEx(myVer)<BR>lblWininfo = ""<BR>lblMoreWininfo = ""<BR>If myVer.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then lblWininfo = lblWininfo & "运行平台 = Windows 95/98" & nl<BR>If myVer.dwPlatformId = VER_PLATFORM_WIN32_NT Then lblWininfo = lblWininfo & "Platform = Windows NT" & nl<BR>lblWininfo = lblWininfo & "Version = " & myVer.dwMajorVersion & "." & myVer.dwMinorVersion & " 创建于 " & (myVer.dwBuildNumber And &HFFFF&) & nl<BR>lblMoreWininfo = "Windows 现在运行在"<BR>If GetSystemMetrics(SM_CLEANBOOT) = 0 Then lblMoreWininfo = lblMoreWininfo & "正常模式" & nl<BR>If GetSystemMetrics(SM_CLEANBOOT) = 1 Then lblMoreWininfo = lblMoreWininfo & "安全模式" & nl<BR>If GetSystemMetrics(SM_CLEANBOOT) = 2 Then lblMoreWininfo = lblMoreWininfo & "局域网安全模式" & nl<BR>If GetSystemMetrics(SM_DEBUG) = True Then lblMoreWininfo = lblMoreWininfo & "Windows Debugging Mode in operation" & nl<BR>If GetSystemMetrics(SM_SLOWMACHINE) = True Then lblMoreWininfo = lblMoreWininfo & "这台PC配置太低无法高效运行 Windows." & nl<BR>End Sub
39.模拟键盘<BR>Private Declare Sub keybd_event Lib "user32" _<BR>(ByVal bVk As Byte, _<BR>ByVal bScan As Byte, _<BR>ByVal dwFlags As Long, _<BR>ByVal dwExtraInfo As Long)<BR>Private Const VK_LWIN = &H5B<BR>Private Const KEYEVENTF_KEYUP = &H2<BR>Private Const VK_APPS = &H5D<BR>Private Const VK_PLAY = &HFA<BR>Private Sub DoAction(Index As Integer)<BR>Dim VK_ACTION As Long<BR>Select Case Index<BR>Case 0: '打开资源管理器<BR>VK_ACTION = &H45<BR>Case 1: '查找文件<BR>VK_ACTION = &H46<BR>Case 2: '最小化所有窗口<BR>VK_ACTION = &H4D<BR>Case 3: '运行程序<BR>VK_ACTION = &H52<BR>Case 4: '弹出Win菜单<BR>VK_ACTION = &H5B<BR>Case 5: '将计算机转如睡眠状态<BR>VK_ACTION = &H5E<BR>Case 6: '执行Windows帮助<BR>VK_ACTION = &H70<BR>End Select<BR>Call keybd_event(VK_LWIN, 0, 0, 0)<BR>Call keybd_event(VK_ACTION, 0, 0, 0)<BR>Call keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)<BR>End Sub
40 延迟函数
Public Sub Delay(DelayTime As Single)<BR> Dim BeginTime As Single<BR> BeginTime = Timer<BR> While Timer < BeginTime + DelayTime<BR> DoEvents<BR> Wend<BR>End Sub<BR>调用形式 delay 1.5<BR>或者用Sleep函数