yulijin608 发表于 2005-1-6 11:36:00

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 Sub





2.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 = &amp;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 &lt;&gt; 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

yulijin608 发表于 2005-1-6 11:36:00

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 = &amp;HCF<BR>Private Sub Command1_Click()<BR>       Dim l As Long<BR>       If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &amp;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&amp;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 = "&amp;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&amp;<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

yulijin608 发表于 2005-1-6 11:37:00

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 = &amp;H1<BR>Private Const MOD_CONTROL = &amp;H2<BR>Private Const MOD_SHIFT = &amp;H4<BR>Private Const PM_REMOVE = &amp;H1<BR>Private Const WM_HOTKEY = &amp;H312<BR>Private HotKey_Fg As Boolean<BR>Private Sub Form_Load()<BR>                       Dim Message As Msg<BR>                       '注册 Ctrl+Y 为热键<BR>                       RegisterHotKey Me.hWnd, &amp;HBFFF&amp;, MOD_CONTROL, vbKeyY<BR>                       'RegisterHotKey Me.hWnd, &amp;HBFF2&amp;, 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, &amp;HBFFF&amp;)<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 = &amp;H32<BR>Private Const HOTKEYF_SHIFT = &amp;H1<BR>Private Const HOTKEYF_ALT = &amp;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&amp;)<BR>Const WS_SYSMENU = &amp;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>

yulijin608 发表于 2005-1-6 11:38:00

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 = &amp;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 = &amp;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 = &amp;H80000<BR>Private Const LWA_ALPHA = &amp;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

yulijin608 发表于 2005-1-6 11:38:00

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 = &amp;H112&amp;<BR>Const SC_MONITORPOWER = &amp;HF170&amp;<BR>Private Sub Command1_Click()<BR>                       SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 2&amp; '关闭显示器<BR>End Sub<BR>Private Sub Command2_Click()<BR>                       SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal -1&amp; '打开显示器<BR>End Sub


19. 在程序结束时自动关闭由SHELL打开的程序。<BR>Private Const PROCESS_QUERY_INFORMATION = &amp;H400       '关闭由SHELL函数打开的文件<BR>Private Const PROCESS_TERMINATE = &amp;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>

yulijin608 发表于 2005-1-6 11:39:00

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>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;<BR>&lt;assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"&gt;<BR>&lt;assemblyIdentity<BR>version="1.0.0.0"<BR>processorArchitecture="X86"<BR>name="CompanyName.ProductName.YourApp"<BR>type="win32"<BR>/&gt;<BR>&lt;description&gt;Your application description here.&lt;/description&gt;<BR>&lt;dependency&gt;<BR>&lt;dependentAssembly&gt;<BR>&lt;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>/&gt;<BR>&lt;/dependentAssembly&gt;<BR>&lt;/dependency&gt;<BR>&lt;/assembly&gt;

yulijin608 发表于 2005-1-6 11:40:00

25.如何打印PictureBox中的所有控件


添加另外一个PictureBox,然后:<BR>Private Const WM_PAINT = &amp;HF<BR>Private Const WM_PRINT = &amp;H317<BR>Private Const PRF_CLIENT = &amp;H4&amp;<BR>Private Const PRF_CHILDREN = &amp;H10&amp;<BR>Private Const PRF_OWNED = &amp;H20&amp;<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) &gt; 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 = &amp;H1<BR>Private Const SHERB_NOPROGRESSUI = &amp;H2<BR>Private Const SHERB_NOSOUND = &amp;H4<BR>Private Sub Command1_Click()<BR>        Dim retval As Long       ' return value<BR>                       retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOPROGRESSUI) ' 清空回收站, 确认<BR>                       ' 若有错误出现,则返回回收站图示<BR>                                                       If retval &lt;&gt; 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 &lt;&gt; 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

yulijin608 发表于 2005-1-6 11:40:00

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 = &amp;H112<BR>'这个参数指明了我们让系统启动屏幕保护<BR>Const SC_SCREENSAVE = &amp;HF140&amp;<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&amp;, Ret, True<BR>                               If Ret &lt;= 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 = "你机子上有 " &amp; Listing.dEntrys &amp; " 个 IP 地址。" &amp; vbCrLf<BR>                               strIP = strIP &amp; "------------------------------------------------" &amp; vbCrLf &amp; 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 &amp; "IP 地址 : " &amp; ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr)       &amp; 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

yulijin608 发表于 2005-1-6 11:41:00

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 = &amp;H14F<BR>Dim bDrop As Boolean<BR>Private isDo As Boolean<BR>Private Sub Combo1_Click()<BR>If Not isDo Then<BR>                                                       isDo = True                                                                                                                                               '&lt;----------回置状态<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        '多项式码&amp;HA001<BR>   Dim SaveHi As Byte, SaveLo As Byte<BR>   Dim i As Integer<BR>   Dim Flag As Integer<BR>   CRC16Lo = &amp;HFF<BR>   CRC16Hi = &amp;HFF<BR>   CL = &amp;H1<BR>   CH = &amp;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 &amp;H1) = &amp;H1) Then '如果高位字节最后一位为1<BR>      CRC16Lo = CRC16Lo Or &amp;H80   '则低位字节右移后前面补1<BR>     End If              '否则自动补0<BR>     If ((SaveLo And &amp;H1) = &amp;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 = &amp;HFF<BR>   CRC16Lo = &amp;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, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40,


&amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1,


&amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80,


&amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0,


&amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0,


&amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81,


&amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, _<BR>&amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80,


&amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0,


&amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1,


&amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81,


&amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1,


&amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H1, &amp;HC0, &amp;H80, &amp;H41, &amp;H0, &amp;HC1, &amp;H81, &amp;H40)<BR>  End Function


  'CRC高位字节值表<BR>  Function GetCRCHi(Ind As Long) As Byte<BR>   GetCRCHi = Choose(Ind + 1, &amp;H0, &amp;HC0, &amp;HC1, &amp;H1, &amp;HC3, &amp;H3, &amp;H2, &amp;HC2, &amp;HC6, &amp;H6, &amp;H7, &amp;HC7, &amp;H5, &amp;HC5, &amp;HC4, &amp;H4,


&amp;HCC, &amp;HC, &amp;HD, &amp;HCD, &amp;HF, &amp;HCF, &amp;HCE, &amp;HE, &amp;HA, &amp;HCA, &amp;HCB, &amp;HB, &amp;HC9, &amp;H9, &amp;H8, &amp;HC8, &amp;HD8, &amp;H18, &amp;H19, &amp;HD9, &amp;H1B, &amp;HDB,


&amp;HDA, &amp;H1A, &amp;H1E, &amp;HDE, &amp;HDF, &amp;H1F, &amp;HDD, &amp;H1D, &amp;H1C, &amp;HDC, &amp;H14, &amp;HD4, &amp;HD5, &amp;H15, &amp;HD7, &amp;H17, &amp;H16, &amp;HD6, &amp;HD2, &amp;H12, &amp;H13,


&amp;HD3, &amp;H11, &amp;HD1, &amp;HD0, &amp;H10, &amp;HF0, &amp;H30, &amp;H31, &amp;HF1, &amp;H33, &amp;HF3, &amp;HF2, &amp;H32, &amp;H36, &amp;HF6, &amp;HF7, &amp;H37, &amp;HF5, &amp;H35, &amp;H34, &amp;HF4,


&amp;H3C, &amp;HFC, &amp;HFD, &amp;H3D, &amp;HFF, &amp;H3F, &amp;H3E, &amp;HFE, &amp;HFA, &amp;H3A, &amp;H3B, &amp;HFB, &amp;H39, &amp;HF9, &amp;HF8, &amp;H38, &amp;H28, &amp;HE8, &amp;HE9, &amp;H29, &amp;HEB,


&amp;H2B, &amp;H2A, &amp;HEA, &amp;HEE, &amp;H2E, &amp;H2F, &amp;HEF, &amp;H2D, &amp;HED, &amp;HEC, &amp;H2C, &amp;HE4, &amp;H24, &amp;H25, &amp;HE5, &amp;H27, &amp;HE7, &amp;HE6, &amp;H26, &amp;H22, &amp;HE2,


&amp;HE3, &amp;H23, &amp;HE1, &amp;H21, &amp;H20, &amp;HE0, &amp;HA0, &amp;H60, _<BR>&amp;H61, &amp;HA1, &amp;H63, &amp;HA3, &amp;HA2, &amp;H62, &amp;H66, &amp;HA6, &amp;HA7, &amp;H67, &amp;HA5, &amp;H65, &amp;H64, &amp;HA4, &amp;H6C, &amp;HAC, &amp;HAD, &amp;H6D, &amp;HAF, &amp;H6F, &amp;H6E,


&amp;HAE, &amp;HAA, &amp;H6A, &amp;H6B, &amp;HAB, &amp;H69, &amp;HA9, &amp;HA8, &amp;H68, &amp;H78, &amp;HB8, &amp;HB9, &amp;H79, &amp;HBB, &amp;H7B, &amp;H7A, &amp;HBA, &amp;HBE, &amp;H7E, &amp;H7F, &amp;HBF,


&amp;H7D, &amp;HBD, &amp;HBC, &amp;H7C, &amp;HB4, &amp;H74, &amp;H75, &amp;HB5, &amp;H77, &amp;HB7, &amp;HB6, &amp;H76, &amp;H72, &amp;HB2, &amp;HB3, &amp;H73, &amp;HB1, &amp;H71, &amp;H70, &amp;HB0, &amp;H50,


&amp;H90, &amp;H91, &amp;H51, &amp;H93, &amp;H53, &amp;H52, &amp;H92, &amp;H96, &amp;H56, &amp;H57, &amp;H97, &amp;H55, &amp;H95, &amp;H94, &amp;H54, &amp;H9C, &amp;H5C, &amp;H5D, &amp;H9D, &amp;H5F, &amp;H9F,


&amp;H9E, &amp;H5E, &amp;H5A, &amp;H9A, &amp;H9B, &amp;H5B, &amp;H99, &amp;H59, &amp;H58, &amp;H98, &amp;H88, &amp;H48, &amp;H49, &amp;H89, &amp;H4B, &amp;H8B, &amp;H8A, &amp;H4A, &amp;H4E, &amp;H8E, &amp;H8F,


&amp;H4F, &amp;H8D, &amp;H4D, &amp;H4C, &amp;H8C, &amp;H44, &amp;H84, &amp;H85, &amp;H45, &amp;H87, &amp;H47, &amp;H46, &amp;H86, &amp;H82, &amp;H42, &amp;H43, &amp;H83, &amp;H41, &amp;H81, &amp;H80, &amp;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>

yulijin608 发表于 2005-1-6 11:43:00

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 = &amp;H1&amp;<BR>                       INTERNET_CONNECTION_LAN = &amp;H2&amp;<BR>                       INTERNET_CONNECTION_PROXY = &amp;H4&amp;<BR>                       INTERNET_RAS_INSTALLED = &amp;H10&amp;<BR>                       INTERNET_CONNECTION_OFFLINE = &amp;H20&amp;<BR>                       INTERNET_CONNECTION_CONFIGURED = &amp;H40&amp;<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&amp;)<BR>                       eConnectionInfo = dwFlags<BR>                       iPos = InStr(sNameBuf, vbNullChar)<BR>                       If iPos &gt; 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 &amp; "Connection uses a modem." &amp; vbCrLf<BR>                       End If<BR>                       If (eR And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then<BR>                                                       sMsg = sMsg &amp; "Connection uses LAN." &amp; vbCrLf<BR>                       End If<BR>                       If (eR And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then<BR>                                                       sMsg = sMsg &amp; "Connection is via Proxy." &amp; vbCrLf<BR>                       End If<BR>                       If (eR And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then<BR>                                                       sMsg = sMsg &amp; "Connection is Off-line." &amp; vbCrLf<BR>                       End If<BR>                       If (eR And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then<BR>                                                       sMsg = sMsg &amp; "Connection is Configured." &amp; vbCrLf<BR>                       Else<BR>                                                       sMsg = sMsg &amp; "Connection is Not Configured." &amp; vbCrLf<BR>                       End If<BR>                       If (eR And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then<BR>                                                       sMsg = sMsg &amp; "System has RAS installed." &amp; vbCrLf<BR>                       End If<BR>               <BR>               ' Display the connection name and info:<BR>                       If bConnected Then<BR>                                                       Text1.Text = "Connected: " &amp; sName &amp; vbCrLf &amp; vbCrLf &amp; sMsg<BR>                       Else<BR>                                                       Text1.Text = "Not Connected: " &amp; sName &amp; vbCrLf &amp; vbCrLf &amp; 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) &amp; Chr(13)<BR>myVer.dwOSVersionInfoSize = 148<BR>q&amp; = GetVersionEx(myVer)<BR>lblWininfo = ""<BR>lblMoreWininfo = ""<BR>If myVer.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then lblWininfo = lblWininfo &amp; "运行平台 = Windows 95/98" &amp; nl<BR>If myVer.dwPlatformId = VER_PLATFORM_WIN32_NT Then lblWininfo = lblWininfo &amp; "Platform = Windows NT" &amp; nl<BR>lblWininfo = lblWininfo &amp; "Version = " &amp; myVer.dwMajorVersion &amp; "." &amp; myVer.dwMinorVersion &amp; " 创建于 " &amp; (myVer.dwBuildNumber And &amp;HFFFF&amp;) &amp; nl<BR>lblMoreWininfo = "Windows 现在运行在"<BR>If GetSystemMetrics(SM_CLEANBOOT) = 0 Then lblMoreWininfo = lblMoreWininfo &amp; "正常模式" &amp; nl<BR>If GetSystemMetrics(SM_CLEANBOOT) = 1 Then lblMoreWininfo = lblMoreWininfo &amp; "安全模式" &amp; nl<BR>If GetSystemMetrics(SM_CLEANBOOT) = 2 Then lblMoreWininfo = lblMoreWininfo &amp; "局域网安全模式" &amp; nl<BR>If GetSystemMetrics(SM_DEBUG) = True Then lblMoreWininfo = lblMoreWininfo &amp; "Windows Debugging Mode in operation" &amp; nl<BR>If GetSystemMetrics(SM_SLOWMACHINE) = True Then lblMoreWininfo = lblMoreWininfo &amp; "这台PC配置太低无法高效运行 Windows." &amp; 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 = &amp;H5B<BR>Private Const KEYEVENTF_KEYUP = &amp;H2<BR>Private Const VK_APPS = &amp;H5D<BR>Private Const VK_PLAY = &amp;HFA<BR>Private Sub DoAction(Index As Integer)<BR>Dim VK_ACTION As Long<BR>Select Case Index<BR>Case 0: '打开资源管理器<BR>VK_ACTION = &amp;H45<BR>Case 1: '查找文件<BR>VK_ACTION = &amp;H46<BR>Case 2: '最小化所有窗口<BR>VK_ACTION = &amp;H4D<BR>Case 3: '运行程序<BR>VK_ACTION = &amp;H52<BR>Case 4: '弹出Win菜单<BR>VK_ACTION = &amp;H5B<BR>Case 5: '将计算机转如睡眠状态<BR>VK_ACTION = &amp;H5E<BR>Case 6: '执行Windows帮助<BR>VK_ACTION = &amp;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 &lt; BeginTime + DelayTime<BR>                                                       DoEvents<BR>                       Wend<BR>End Sub<BR>调用形式       delay 1.5<BR>或者用Sleep函数
页: [1] 2 3 4 5 6 7 8
查看完整版本: vb编程68例