yulijin608
发表于 2005-1-6 11:44:00
41. 修改窗体系统菜单<BR>module:<BR>Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd 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>Public 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 Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long<BR>Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long<BR>Public Const WM_SYSCOMMAND = &H112<BR>Public Const GWL_WNDPROC = (-4)<BR>Public Const MF_STRING = &H0&<BR>Public Const MF_SEPARATOR = &H800&<BR>Public OldWindowProc As Long<BR>' 保存默认的窗口函数地址<BR>Public SysMenuHwnd As Long<BR>Public Function SubClass1_WndMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long<BR> If Msg <> WM_SYSCOMMAND Then<BR> SubClass1_WndMessage = CallWindowProc(OldWindowProc, hwnd, Msg, wp, lp)<BR> ' 如果消息不是WM_SYSCOMMAND,就调用默认的窗口函数处理<BR> Exit Function<BR> End If<BR> Select Case wp<BR> Case 2001<BR> Call MsgBox("本程序实现了修改系统菜单的功能 ", vbOKOnly + vbInformation)<BR> Case 2003<BR> Call GetSystemMenu(Form1.hwnd, True)<BR> Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, OldWindowProc)<BR> Call MsgBox("已经恢复了默认的系统菜单 ", vbOKOnly + vbInformation)<BR> Case Else<BR> SubClass1_WndMessage = CallWindowProc(OldWindowProc, hwnd, Msg, wp, lp)<BR> Exit Function<BR> End Select<BR> SubClass1_WndMessage = True<BR> End Function<BR>窗体:<BR>Private Sub Form_Load()<BR> OldWindowProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)<BR> ' 取得窗口函数的地址<BR> Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)<BR> ' 用SubClass1_WndMessage代替窗口函数处理消息<BR> SysMenuHwnd = GetSystemMenu(Form1.hwnd, False)<BR> Call AppendMenu(SysMenuHwnd, MF_SEPARATOR, 2000, vbNullString)<BR> Call AppendMenu(SysMenuHwnd, MF_STRING, 2001, "关于本程序(&A)")<BR> Call AppendMenu(SysMenuHwnd, MF_SEPARATOR, 2002, vbNullString)<BR> Call AppendMenu(SysMenuHwnd, MF_STRING, 2003, "恢复系统菜单(&R)")<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR>If OldWindowProc <> GetWindowLong(Form1.hwnd, GWL_WNDPROC) Then<BR>Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, OldWindowProc)<BR>End If<BR>End Sub<BR>42.如何在小画面上显示大图片<BR>方法一:<BR>一个picturebox控件,一个image控件(以picturebox为容器),图片加载在image中,一个HScroll1,VScroll1(以picturebox为容器)。<BR>Private Sub Bar1_Change()<BR>Image1.Left = -bar1.Value<BR>End Sub
Private Sub Bar2_Change()<BR>Image1.Top = -Bar2.Value<BR>End Sub
Private Sub Form_Load()<BR>Image1.Left = 0<BR>Image1.Top = 0<BR>bar1.SmallChange = 300<BR>Bar2.SmallChange = 300<BR>bar1.Max = Image1.Width - Picture1.Width<BR>Bar2.Max = Image1.Height - Picture1.Height<BR>bar1.Min = 0<BR>Bar2.Min = 0<BR>End Sub
<BR>方法二:利用鼠标移动图片<BR>一个picturebox控件,一个image控件(以picturebox为容器),图片加载在image中<BR>Dim ix As Integer<BR>Dim iy As Integer<BR>Private Sub Form_Load()<BR>Image1.Left = 0<BR>Image1.Top = 0<BR>End Sub<BR>Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>If Button = vbLeftButton Then<BR>ix = X<BR>iy = Y<BR>End If<BR>End Sub<BR>Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>Dim ipx As Integer<BR>Dim ipy As Integer<BR>If Button = vbLeftButton Then<BR>ipx = Image1.Left + X - ix<BR>ipy = Image1.Top + Y - iy<BR>If ipx > 0 Then<BR>Image1.Left = 0<BR>Else<BR>If ipx < Picture1.Width - Image1.Width Then<BR>ipx = Picture1.Width - Image1.Width<BR>Else<BR>Image1.Left = ipx<BR>End If<BR>End If<BR>If ipy > 0 Then<BR>Image1.Top = 0<BR>Else<BR>If ipy < Picture1.Height - Image1.Height Then<BR>ipy = Picture1.Height - Image1.Height<BR>Else<BR>Image1.Top = ipy<BR>End If<BR>End If<BR>End If<BR>End Sub<BR>Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>Me.MousePointer = 0<BR>End Sub
43. 使窗体不出屏幕左边界<BR>module:<BR>Option Explicit<BR>Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _<BR> (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _<BR> (ByVal hwnd As Long, ByVal nIndex As Long) As Long<BR>Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _<BR> (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _<BR> ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( _<BR> lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)<BR>Public Const GWL_WNDPROC = (-4)<BR>Public Const WM_WINDOWPOSCHANGING = &H46<BR>Type WINDOWPOS<BR> hwnd As Long<BR> hWndInsertAfter As Long<BR> x As Long<BR> y As Long<BR> cx As Long<BR> cy As Long<BR> flags As Long<BR>End Type<BR>Public preWinProc As Long<BR>'而重点就在於Window重新定位之前会传<BR>'出WM_WINDOWPOSCHANGING这个讯息,而lParam指向一个WINDOWPOS的STRUCTURE。<BR>Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _<BR> ByVal wParam As Long, ByVal lParam As Long) As Long<BR> Dim lwd As Long, hwd As Long<BR> If Msg = WM_WINDOWPOSCHANGING Then<BR> Dim WPOS As WINDOWPOS<BR> CopyMemory WPOS, ByVal lParam, Len(WPOS)<BR> If WPOS.x < 0 Then<BR> WPOS.x = 0<BR> CopyMemory ByVal lParam, WPOS, Len(WPOS)<BR> End If<BR> End If<BR> '将之送往原来的Window Procedure<BR> wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)<BR>End Function<BR>窗体中<BR>Sub Form_Load()<BR> Dim ret As Long<BR> '记录原本的Window Procedure的位址<BR> preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)<BR> ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)<BR>End Sub
Private Sub Form_Unload(Cancel As Integer)<BR> Dim ret As Long<BR> '取消Message的截取,而使之又只送往原来的Window Procedure<BR> ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)<BR>End Sub<BR>44.打开指定的窗体<BR>Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long<BR>Const SW_SHOWNORMAL = 1<BR>Private Sub Command1_Click() '我的文档<BR>ShellExecute Me.hwnd, "open", "explorer", vbNullString, vbNullString, 1<BR>End Sub<BR>Private Sub Command2_Click() '我的电脑<BR>ShellExecute Me.hwnd, "open", "explorer", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", vbnulstring, 1<BR>End Sub<BR>Private Sub Command3_Click() '网上邻居<BR>ShellExecute Me.hwnd, "open", "explorer", "::{208d2c60-3aea-1069-a2d7-08002b30309d}", vbNullString, 1<BR>End Sub<BR>Private Sub Command4_Click() '回收站<BR>ShellExecute Me.hwnd, "open", "explorer", "::{645ff040-5081-101b-9f08-00aa002f954e}", vbNullString, 1<BR>End Sub<BR>Private Sub Command5_Click() '控制面板<BR>ShellExecute Me.hwnd, "open", "explorer", "::{21ec2020-3aea-1069-a2dd-08002b30309d}", vbNullString, 1<BR>End Sub<BR>Private Sub Command6_Click() '打开指定的路径<BR>ShellExecute Me.hwnd, "open", "D:\vb练习事例", vbNullString, vbNullString, 1<BR>End Sub<BR>Private Sub Command7_Click() '音量控制<BR> Shell "sndvol32.exe", vbNormalFocus<BR>End Sub
yulijin608
发表于 2005-1-6 11:47:00
45.窗体分割条
splitter为一picturebox控件。<BR>Option Explicit<BR>Private Const SPLT_WDTH As Integer = 35<BR>Private currSplitPosX As Long<BR>Dim CTRL_OFFSET As Integer<BR>Dim SPLT_COLOUR As Long<BR>Private Sub Form_Load()<BR>CTRL_OFFSET = 5<BR>SPLT_COLOUR = &H808080<BR>currSplitPosX = &H7FFFFFFF<BR>ListLeft.AddItem "VB俱乐部"<BR>ListLeft.AddItem "VB动画篇"<BR>ListLeft.AddItem "VB网络篇"<BR>ListLeft.AddItem "VB控件类"<BR>ListLeft.AddItem "VB界面类"<BR>TextRight = "经常见到窗体上有二个相邻的列表框,可以用鼠标任意拉动中间分割条,改变列表框大小。"<BR>End Sub<BR>Private Sub Form_Resize()<BR>Dim x1 As Integer<BR>Dim x2 As Integer<BR>Dim height1 As Integer<BR>Dim width1 As Integer<BR>Dim width2 As Integer<BR>On Error Resume Next<BR>height1 = ScaleHeight - (CTRL_OFFSET * 2)<BR>x1 = CTRL_OFFSET<BR>width1 = ListLeft.Width<BR>x2 = x1 + ListLeft.Width + SPLT_WDTH - 1<BR>width2 = ScaleWidth - x2 - CTRL_OFFSET<BR>ListLeft.Move x1% - 1, CTRL_OFFSET, width1, height1<BR>TextRight.Move x2, CTRL_OFFSET, width2 + 1, height1<BR>Splitter.Move x1 + ListLeft.Width - 1, CTRL_OFFSET, SPLT_WDTH, height1<BR>End Sub<BR>Private Sub Splitter_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>If Button = vbLeftButton Then<BR> Splitter.BackColor = SPLT_COLOUR<BR> currSplitPosX = CLng(X)<BR>Else<BR> If currSplitPosX <> &H7FFFFFFF Then Splitter_MouseUp Button, Shift, X, Y<BR> currSplitPosX = &H7FFFFFFF<BR>End If<BR>End Sub<BR>Private Sub Splitter_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>If currSplitPosX& <> &H7FFFFFFF Then<BR>If CLng(X) <> currSplitPosX Then<BR>Splitter.Move Splitter.Left + X, CTRL_OFFSET, SPLT_WDTH, ScaleHeight - (CTRL_OFFSET * 2)<BR>currSplitPosX = CLng(X)<BR>End If<BR>End If<BR>End Sub<BR>Private Sub Splitter_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>If currSplitPosX <> &H7FFFFFFF Then<BR>If CLng(X) <> currSplitPosX Then<BR> Splitter.Move Splitter.Left + X, CTRL_OFFSET, SPLT_WDTH, ScaleHeight - (CTRL_OFFSET * 2)<BR>End If<BR>currSplitPosX = &H7FFFFFFF<BR>Splitter.BackColor = &H8000000F<BR>If Splitter.Left > 60 And Splitter.Left < (ScaleWidth - 60) Then<BR>ListLeft.Width = Splitter.Left - ListLeft.Left<BR>ElseIf Splitter.Left < 60 Then<BR> ListLeft.Width = 60<BR>Else<BR> ListLeft.Width = ScaleWidth - 60<BR>End If<BR> Form_Resize<BR>End If
End Sub
<BR>46.托盘程序<BR>module:<BR>Option Explicit<BR>Public preWinProc As Long<BR>Public NewForm As Form<BR>Public NewMenu As Menu<BR>Public Const WM_USER = &H400<BR>Public Const WM_LBUTTONUP = &H202<BR>Public Const WM_MBUTTONUP = &H208<BR>Public Const WM_RBUTTONUP = &H205<BR>Public Const TRAY_CALLBACK = (WM_USER + 1001&)<BR>Public Const GWL_WNDPROC = (-4)<BR>Public Const GWL_USERDATA = (-21)<BR>Public Const NIF_ICON = &H2<BR>Public Const NIF_TIP = &H4<BR>Public Const NIM_ADD = &H0<BR>Public Const NIF_MESSAGE = &H1<BR>Public Const NIM_MODIFY = &H1<BR>Public Const NIM_DELETE = &H2<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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long<BR>Public Type NOTIFYICONDATA<BR> cbSize As Long<BR> hwnd As Long<BR> uID As Long<BR> uFlags As Long<BR> uCallbackMessage As Long<BR> hIcon As Long<BR> szTip As String * 64<BR>End Type<BR>Private NOTI As NOTIFYICONDATA<BR>Public Function NewWindone(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long<BR> If Msg = TRAY_CALLBACK Then<BR> <BR> If lParam = WM_LBUTTONUP Then<BR> ' 单击左键,弹出窗口<BR> If NewForm.WindowState = vbMinimized Then _<BR> NewForm.WindowState = NewForm.LastState<BR> NewForm.SetFocus<BR> Exit Function<BR> End If<BR> If lParam = WM_RBUTTONUP Then<BR> ' 单击右键,弹出菜单<BR> NewForm.PopupMenu NewMenu<BR> Exit Function<BR> End If<BR> End If<BR> NewWindone = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)<BR>End Function<BR>Public Sub AddToTray(frm As Form, mnu As Menu)<BR> Set NewForm = frm<BR> Set NewMenu = mnu<BR> preWinProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf NewWindone)<BR> With NOTI<BR> .uID = 0<BR> .hwnd = frm.hwnd<BR> .cbSize = Len(NOTI)<BR> .hIcon = frm.Icon.Handle<BR> .uFlags = NIF_ICON<BR> .uCallbackMessage = TRAY_CALLBACK<BR> .uFlags = .uFlags Or NIF_MESSAGE<BR> .cbSize = Len(NOTI)<BR> End With<BR> Shell_NotifyIcon NIM_ADD, NOTI<BR>End Sub<BR>'屏蔽托盘<BR>Public Sub RemoveFromTray()<BR> With NOTI<BR> .uFlags = 0<BR> End With<BR> Shell_NotifyIcon NIM_DELETE, NOTI<BR> SetWindowLong NewForm.hwnd, GWL_WNDPROC, preWinProc<BR>End Sub
Public Sub SetTrayTip(tip As String)<BR> With NOTI<BR> .szTip = tip & vbNullChar<BR> .uFlags = NIF_TIP<BR> End With<BR> Shell_NotifyIcon NIM_MODIFY, NOTI<BR>End Sub
Public Sub SetTrayIcon(pic As Picture)<BR> If pic.Type <> vbPicTypeIcon Then Exit Sub<BR> With NOTI<BR> .hIcon = pic.Handle<BR> .uFlags = NIF_ICON<BR> End With<BR> Shell_NotifyIcon NIM_MODIFY, NOTI<BR>End Sub<BR>窗体中
Private Sub Form_Load()<BR> AddToTray Me, Tray<BR> SetTrayTip "托盘演示"<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> RemoveFromTray<BR>End Sub<BR>47.led数值显示<BR>添加类模块:(name属性为mcLCD)<BR>Option Explicit<BR>Private Type Coordinate<BR>X As Integer<BR>Y As Integer<BR>End Type<BR>Dim BasePoint As Coordinate<BR>Dim SegWidth As Integer<BR>Dim SegHeight As Integer<BR>Dim p As PictureBox<BR>Property Let BackColor(Color As Long)<BR>p.BackColor = Color<BR>End Property<BR>Private Sub DrawNumber(Number As Integer)<BR>Select Case Number<BR>Case 0<BR>DrawSegment (1): DrawSegment (2): DrawSegment (3): DrawSegment (4)<BR>DrawSegment (5): DrawSegment (6)<BR>Case 1<BR>DrawSegment (2): DrawSegment (3)<BR>Case 2<BR>DrawSegment (1): DrawSegment (2): DrawSegment (7): DrawSegment (5)<BR>DrawSegment (4)<BR>Case 3<BR>DrawSegment (1): DrawSegment (2): DrawSegment (7): DrawSegment (3)<BR>DrawSegment (4)<BR>Case 4<BR>DrawSegment (2): DrawSegment (3): DrawSegment (7): DrawSegment (6)<BR>Case 5<BR>DrawSegment (1): DrawSegment (6): DrawSegment (7): DrawSegment (3)<BR>DrawSegment (4)<BR>Case 6<BR>DrawSegment (1): DrawSegment (6): DrawSegment (7): DrawSegment (3)<BR>DrawSegment (4): DrawSegment (5)<BR>Case 7<BR>DrawSegment (1): DrawSegment (2)<BR>DrawSegment (3)<BR>Case 8<BR>DrawSegment (1): DrawSegment (2): DrawSegment (3): DrawSegment (4)<BR>DrawSegment (5): DrawSegment (6): DrawSegment (7)<BR>Case 9<BR>DrawSegment (1): DrawSegment (2): DrawSegment (3): DrawSegment (4)<BR>DrawSegment (6): DrawSegment (7)<BR>End Select<BR>End Sub<BR>Private Sub DrawSegment(SegNum As Integer)<BR>' 1<BR>' ___<BR>' | |<BR>' 6 | | 2<BR>' |-7-|<BR>' 5 | | 3<BR>' |___|<BR>'<BR>' 4<BR>'画出七段数码管的七个组成部分<BR>Select Case SegNum<BR>Case 1<BR>p.Line (BasePoint.X + 1, BasePoint.Y)-(BasePoint.X + SegWidth - 1, BasePoint.Y)<BR>p.Line (BasePoint.X + 2, BasePoint.Y + 1)-(BasePoint.X + SegWidth - 2, BasePoint.Y + 1)<BR>p.Line (BasePoint.X + 3, BasePoint.Y + 2)-(BasePoint.X + SegWidth - 3, BasePoint.Y + 2)<BR>Case 2<BR>p.Line (BasePoint.X + SegWidth - 1, BasePoint.Y + 1)-(BasePoint.X + SegWidth - 1, BasePoint.Y + (SegHeight \ 2) - 1)<BR>p.Line (BasePoint.X + SegWidth - 2, BasePoint.Y + 2)-(BasePoint.X + SegWidth - 2, BasePoint.Y + (SegHeight \ 2))<BR>p.Line (BasePoint.X + SegWidth - 3, BasePoint.Y + 3)-(BasePoint.X + SegWidth - 3, BasePoint.Y + (SegHeight \ 2) - 1)<BR>Case 3<BR>p.Line (BasePoint.X + SegWidth - 1, BasePoint.Y + (SegHeight \ 2) + 2)-(BasePoint.X + SegWidth - 1, BasePoint.Y + SegHeight)<BR>p.Line (BasePoint.X + SegWidth - 2, BasePoint.Y + (SegHeight \ 2) + 1)-(BasePoint.X + SegWidth - 2, BasePoint.Y + SegHeight - 1)<BR>p.Line (BasePoint.X + SegWidth - 3, BasePoint.Y + (SegHeight \ 2) + 2)-(BasePoint.X + SegWidth - 3, BasePoint.Y + SegHeight - 2)<BR>Case 4<BR>p.Line (BasePoint.X + 3, BasePoint.Y + SegHeight - 2)-(BasePoint.X + SegWidth - 3, BasePoint.Y + SegHeight - 2)<BR>p.Line (BasePoint.X + 2, BasePoint.Y + SegHeight - 1)-(BasePoint.X + SegWidth - 2, BasePoint.Y + SegHeight - 1)<BR>p.Line (BasePoint.X + 1, BasePoint.Y + SegHeight)-(BasePoint.X + SegWidth - 1, BasePoint.Y + SegHeight)<BR>Case 5<BR>p.Line (BasePoint.X, BasePoint.Y + (SegHeight \ 2) + 2)-(BasePoint.X, BasePoint.Y + SegHeight)<BR>p.Line (BasePoint.X + 1, BasePoint.Y + (SegHeight \ 2) + 1)-(BasePoint.X + 1, BasePoint.Y + SegHeight - 1)<BR>p.Line (BasePoint.X + 2, BasePoint.Y + (SegHeight \ 2) + 2)-(BasePoint.X + 2, BasePoint.Y + SegHeight - 2)<BR>Case 6<BR>p.Line (BasePoint.X, BasePoint.Y + 1)-(BasePoint.X, BasePoint.Y + (SegHeight \ 2) - 1)<BR>p.Line (BasePoint.X + 1, BasePoint.Y + 2)-(BasePoint.X + 1, BasePoint.Y + (SegHeight \ 2))<BR>p.Line (BasePoint.X + 2, BasePoint.Y + 3)-(BasePoint.X + 2, BasePoint.Y + (SegHeight \ 2) - 1)<BR>Case 7<BR>p.Line (BasePoint.X + 3, BasePoint.Y + (SegHeight \ 2) - 1)-(BasePoint.X + SegWidth - 3, BasePoint.Y + (SegHeight \ 2) - 1)<BR>p.Line (BasePoint.X + 2, BasePoint.Y + (SegHeight \ 2))-(BasePoint.X + SegWidth - 2, BasePoint.Y + (SegHeight \ 2))<BR>p.Line (BasePoint.X + 3, BasePoint.Y + (SegHeight \ 2) + 1)-(BasePoint.X + SegWidth - 3, BasePoint.Y + (SegHeight \ 2) + 1)<BR>End Select<BR>End Sub<BR>Public Property Let Caption(ByVal Value As String)<BR>Dim OrigX As Integer<BR>OrigX = BasePoint.X<BR>p.Cls<BR>While Value <> ""<BR>If Left$(Value, 1) <> ":" And Left$(Value, 1) <> "." Then<BR>DrawNumber (Val(Left$(Value, 1)))<BR>BasePoint.X = BasePoint.X + SegWidth + 3<BR>Else<BR>If Left$(Value, 1) = "." Then<BR>p.Line (BasePoint.X + (SegWidth \ 2) - 4, BasePoint.Y + (SegHeight \ 2) + 6)-(BasePoint.X + (SegWidth \ 2), BasePoint.Y + (SegHeight \ 2) + 9), , BF<BR>BasePoint.X = BasePoint.X + SegWidth<BR>Else<BR>p.Line (BasePoint.X + (SegWidth \ 2) - 4, BasePoint.Y + (SegHeight \ 2) - 6)-(BasePoint.X + (SegWidth \ 2), BasePoint.Y + (SegHeight \ 2) - 3), , BF<BR>p.Line (BasePoint.X + (SegWidth \ 2) - 4, BasePoint.Y + (SegHeight \ 2) + 4)-(BasePoint.X + (SegWidth \ 2), BasePoint.Y + (SegHeight \ 2) + 7), , BF<BR>BasePoint.X = BasePoint.X + SegWidth<BR>End If<BR>End If<BR>Value = Right$(Value, Len(Value) - 1)<BR>Wend<BR>BasePoint.X = OrigX<BR>End Property<BR>Property Let ForeColor(Color As Long)<BR>p.ForeColor = Color<BR>End Property
Public Sub NewLCD(PBox As PictureBox)<BR>Set p = PBox<BR>p.ScaleMode = 3 ' pixel<BR>p.AutoRedraw = True<BR>BasePoint.X = 2<BR>BasePoint.Y = 2<BR>SegHeight = p.ScaleHeight - 6<BR>SegWidth = (SegHeight \ 2) + 2<BR>End Sub<BR>窗体中:<BR>Option Explicit<BR>Dim lcdTest1 As New mcLCD<BR>Private Sub Form_Load()<BR>lcdTest1.NewLCD picture1<BR>End Sub<BR>Private Sub Timer1_Timer()<BR>lcdTest1.Caption = Time<BR>End Sub<BR>48.将部分菜单放置在窗体的最右段(如帮助等)<BR>在菜单编辑器中在待放置于最右段的菜单前加一标题为空格的菜单,并去掉visable属性前钩号。<BR>Private Type MENUITEMINFO<BR>'.......请自己加上啊 <BR>End Type<BR>Private Const MFT_RIGHTJUSTIFY = &H4000<BR>'API函数声明<BR>Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal _ hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As _ MENUITEMINFO) As Long<BR>Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal _ hMenu As Long, ByVal un As Long, ByVal b As Long, lpMenuItemInfo As MENUITEMINFO) As Long<BR>Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long<BR>Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long<BR>'在窗体载入过程(也可放在其他过程)中对菜单设置进行更改<BR>Private Sub Form_Load()<BR>Dim my_menuItemInfo As MENUITEMINFO<BR>Dim return_value As Long<BR>my_menuItemInfo.cbSize = 44<BR>my_menuItemInfo.fMask = 16<BR>my_menuItemInfo.cch = 128<BR>my_menuItemInfo.dwTypeData = Space$(128)<BR>return_value = GetMenuItemInfo(GetMenu(Me.hwnd), 2, 1, my_menuItemInfo)<BR> '这里的2请根据自己的情况而定,为正常显示在左端的菜单数<BR>my_menuItemInfo.fType = MFT_RIGHTJUSTIFY<BR>return_value = SetMenuItemInfo(GetMenu(Me.hwnd), 2, 1, my_menuItemInfo)<BR>End Sub
yulijin608
发表于 2005-1-6 11:50:00
49.List每行以相应的内容为提示<BR>'----------------------By 陈锐------------------------------<BR>'如果你要在Internet或BBS上转贴文章,请通知我知道(没有通知,不知道犯不犯法,呵呵)<BR>'这个程序演示如何给List Box的每个列表行加上不同的提示行<BR>'运行该程序,当鼠标移动到任一行上后,弹出的ToolTip就会提示该行的完整内容<BR>'Option Explicit<BR>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _<BR> (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _<BR> lParam As Any) As Long<BR>Private Const LB_ITEMFROMPOINT = &H1A9<BR>Private Sub Form_Load()<BR> With List1<BR> .AddItem "aaa"<BR> .AddItem "bbb"<BR> .AddItem "ccc"<BR> End With<BR>End Sub<BR>Private Sub List1_MouseMove(Button As Integer, Shift As Integer, _<BR>X As Single, Y As Single)<BR> ' present related tip message<BR> Dim lXPoint As Long<BR> Dim lYPoint As Long<BR> Dim lIndex As Long<BR> If Button = 0 Then ' 如果没有按钮被按下<BR> lXPoint = CLng(X / Screen.TwipsPerPixelX)<BR> lYPoint = CLng(Y / Screen.TwipsPerPixelY)<BR> With List1<BR> ' 获得当前的光标所在的的屏幕位置确定标题位置<BR> lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, _<BR> ByVal ((lYPoint * 65536) + lXPoint))<BR> ' 显示提示行或清除提示行<BR> If (lIndex >= 0) And (lIndex <= .ListCount) Then<BR> .ToolTipText = .List(lIndex)<BR> Else<BR> .ToolTipText = ""<BR> End If<BR> End With<BR> End If<BR>End Sub
50.将部分菜单放置在窗体的最右段(如帮助等)<BR>在菜单编辑器中在待放置于最右段的菜单前加一标题为空格的菜单,并去掉visable属性前钩号。<BR>Private Type MENUITEMINFO<BR>'.......请自己加上啊 <BR>End Type<BR>Private Const MFT_RIGHTJUSTIFY = &H4000<BR>'API函数声明<BR>Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal _ hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As _ MENUITEMINFO) As Long<BR>Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal _ hMenu As Long, ByVal un As Long, ByVal b As Long, lpMenuItemInfo As MENUITEMINFO) As Long<BR>Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long<BR>Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long<BR>'在窗体载入过程(也可放在其他过程)中对菜单设置进行更改<BR>Private Sub Form_Load()<BR>Dim my_menuItemInfo As MENUITEMINFO<BR>Dim return_value As Long<BR>my_menuItemInfo.cbSize = 44<BR>my_menuItemInfo.fMask = 16<BR>my_menuItemInfo.cch = 128<BR>my_menuItemInfo.dwTypeData = Space$(128)<BR>return_value = GetMenuItemInfo(GetMenu(Me.hwnd), 2, 1, my_menuItemInfo)<BR> '这里的2请根据自己的情况而定,为正常显示在左端的菜单数<BR>my_menuItemInfo.fType = MFT_RIGHTJUSTIFY<BR>return_value = SetMenuItemInfo(GetMenu(Me.hwnd), 2, 1, my_menuItemInfo)<BR>End Sub
51. 改变屏幕分辨率<BR>Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, lpString2 As Any) As Long<BR>Private Const CCHDEVICENAME = 32<BR>Private Const CCHFORMNAME = 32<BR>Private Const ENUM_CURRENT_SETTINGS = 1<BR>Private Type DEVMODE<BR> .........(请自己添加上) <BR>End Type<BR>Private Declare Function ChangeDisplaySettings Lib "user32" _ Alias "ChangeDisplaySettingsA" (ByVal lpDevMode As Long, ByVal dwflags As Long) As Long<BR>Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _ (ByVal lpszDeviceName As String, ByVal iModeNum As Long, lpDevMode As Any) As Long<BR>Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long<BR>Private Const SM_CXSCREEN = 0<BR>Private Const SM_CYSCREEN = 1<BR>Dim pNewMode As DEVMODE<BR>Dim pOldMode As Long<BR>Dim nOrgWidth As Integer, nOrgHeight As Integer<BR> '设置显示器分辨率的执行函数<BR>Private Function SetDisplayMode(Width As Integer, Height As Integer, Color As Integer) _ As Long ', Freq As Long) As Long<BR> On Error GoTo ErrorHandler<BR> Const DM_PELSWIDTH = &H80000<BR> Const DM_PELSHEIGHT = &H100000<BR> Const DM_BITSPERPEL = &H40000<BR> Const DM_DISPLAYFLAGS = &H200000<BR> Const DM_DISPLAYFREQUENCY = &H400000<BR> With pNewMode<BR> .dmSize = Len(pNewMode)<BR> If Color = 0 Then 'Color = 0 时不更改屏幕颜色<BR> .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT<BR> Else<BR> .dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT<BR> End If<BR> .dmPelsWidth = Width<BR> .dmPelsHeight = Height<BR> If Color <> 0 Then<BR> .dmBitsPerPel = Color<BR> End If<BR> End With<BR> pOldMode = lstrcpy(pNewMode, pNewMode)<BR> SetDisplayMode = ChangeDisplaySettings(pOldMode, 1)<BR> Exit Function<BR>ErrorHandler:<BR> MsgBox Err.Description<BR>End Function<BR>Private Sub Command1_Click()<BR> Dim nWidth As Integer, nHeight As Integer, nColor As Integer<BR> Select Case Combo1.ListIndex<BR> Case 0<BR> nWidth = 640: nHeight = 480: nColor = 16 '640*480*16位真彩色,256色nColor _<BR> = 8,16色nColor = 4,nColor = 0 表示不改变颜色<BR> Case 1<BR> nWidth = 640: nHeight = 480: nColor = 24<BR> Case 2<BR> nWidth = 640: nHeight = 480: nColor = 32<BR> Case 3<BR> nWidth = 800: nHeight = 600: nColor = 16<BR> Case 4<BR> nWidth = 800: nHeight = 600: nColor = 24<BR> Case 5<BR> nWidth = 800: nHeight = 600: nColor = 32<BR> Case 6<BR> nWidth = 1024: nHeight = 768: nColor = 16<BR> Case 7<BR> nWidth = 1024: nHeight = 768: nColor = 24<BR> Case 8<BR> nWidth = 1024: nHeight = 768: nColor = 32<BR> Case other<BR> nWidth = 800: nHeight = 600: nColor = 16<BR> End Select<BR> Call SetDisplayMode(nWidth, nHeight, nColor) '注意,系统不支持的显示模式不<BR> '能选,否则准备用安全模式重启动吧.<BR>End Sub<BR>Private Sub Form_Load()<BR> Combo1.AddItem "640*480*16位真彩色"<BR> Combo1.AddItem "640*480*24位真彩色"<BR> Combo1.AddItem "640*480*32位真彩色"<BR> Combo1.AddItem "800*600*16位真彩色"<BR> Combo1.AddItem "800*600*24位真彩色"<BR> Combo1.AddItem "800*600*32位真彩色"<BR> Combo1.AddItem "1024*768*16位真彩色"<BR> Combo1.AddItem "1024*768*24位真彩色"<BR> Combo1.AddItem "1024*768*32位真彩色"<BR> Combo1.Text = Combo1.List(0)<BR> nOrgWidth = GetDisplayWidth<BR> nOrgHeight = GetDisplayHeight<BR> 'nOrgWidth = GetSystemMetrics(SM_CXSCREEN)'两种获取初始屏幕大小的方法均可<BR> 'nOrgHeight = GetSystemMetrics(SM_CYSCREEN)<BR>End Sub<BR>Private Function GetDisplayWidth() As Integer<BR> GetDisplayWidth = Screen.Width \ Screen.TwipsPerPixelX<BR>End Function<BR>Private Function GetDisplayHeight() As Integer<BR> GetDisplayHeight = Screen.Height \ Screen.TwipsPerPixelY<BR>End Function<BR>Private Sub RestoreDisplayMode()<BR> Call SetDisplayMode(nOrgWidth, nOrgHeight, 0)<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> RestoreDisplayMode<BR>End Sub<BR>52 .各种进制转换<BR>Function Bin2Dec(InputData As String) As Double '二进制转变成十进制<BR>Dim DecOut As Double:Dim I As Integer:Dim LenBin As Double:Dim JOne As String<BR>LenBin = Len(InputData) '确认是否为二进制数<BR>For I = 1 To LenBin<BR> JOne = Mid(InputData, I, 1)<BR> If JOne <> "0" And JOne <> "1" Then<BR> MsgBox "NOT A BINARY NUMBER", vbCritical<BR> Exit Function<BR> End If<BR>Next I<BR>DecOut = 0<BR>For I = Len(InputData) To 1 Step -1<BR> If Mid(InputData, I, 1) = "1" Then<BR> DecOut = DecOut + 2 ^ (Len(InputData) - I)<BR> End If<BR>Next I<BR>Bin2Dec = DecOut<BR>End Function
Function Dec2Bin(InputData As Double) As String '十进制转变为二进制<BR>Dim Quot As Double:Dim Remainder As Double:Dim BinOut As String:Dim I As Integer<BR>Dim NewVal As Double:Dim TempString As String:Dim TempVal As Double<BR>Dim BinTemp As String:Dim BinTemp1 As String:Dim PosDot As Integer<BR>Dim Temp2 As String <BR> '检查是否为十进制的小数点<BR>If InStr(1, CStr(InputData), ".") Then<BR> MsgBox "Only Whole Numbers can be converted", vbCritical<BR> GoTo eds<BR>End If<BR>BinOut = ""<BR>NewVal = InputData<BR>DoAgain: '开始计算<BR>NewVal = (NewVal / 2) '如果有余数<BR>If InStr(1, CStr(NewVal), ".") Then<BR> BinOut = BinOut + "1" '得到余数<BR> NewVal = Format(NewVal, "#0")<BR> NewVal = (NewVal - 1)<BR> If NewVal < 1 Then<BR> GoTo DoneIt<BR> End If<BR>Else<BR> BinOut = BinOut + "0"<BR> If NewVal < 1 Then<BR> GoTo DoneIt<BR> End If<BR>End If<BR>GoTo DoAgain<BR>DoneIt:<BR>BinTemp = "" '颠倒结果<BR>For I = Len(BinOut) To 1 Step -1<BR> BinTemp1 = Mid(BinOut, I, 1)<BR> BinTemp = BinTemp + BinTemp1<BR>Next I<BR>BinOut = BinTemp '输出结果<BR>Dec2Bin = BinOut<BR>eds:<BR>End Function
Function Bin2Hex(InputData As String) As String '二进制转变成十六进制<BR>Dim I As Integer:Dim LenBin As Integer:Dim JOne As String:Dim NumBlocks As Integer<BR>Dim FullBin As String:Dim HexOut As String:Dim TempBinBlock As String<BR>Dim TempHex As String<BR>LenBin = Len(InputData)'确认是否为二进制数<BR>For I = 1 To LenBin<BR> JOne = Mid(InputData, I, 1)<BR> If JOne <> "0" And JOne <> "1" Then<BR> MsgBox "NOT A BINARY NUMBER", vbCritical<BR> Exit Function<BR> End If<BR>Next I '设置二进制变量<BR>FullBin = InputData ' 如果这个值的长度小于4,则补0<BR>If LenBin < 4 Then<BR> If LenBin = 3 Then<BR> FullBin = "0" + FullBin<BR> ElseIf LenBin = 2 Then<BR> FullBin = "00" + FullBin<BR> ElseIf LenBin = 1 Then<BR> FullBin = "000" + FullBin<BR> ElseIf LenBin = 0 Then<BR> MsgBox "Nothing Given..", vbCritical<BR> Exit Function<BR> End If<BR> NumBlocks = 1<BR> GoTo DoBlocks<BR>End If<BR>If LenBin = 4 Then<BR> NumBlocks = 1<BR> GoTo DoBlocks<BR>End If<BR>If LenBin > 4 Then<BR>Dim TempHold As Currency<BR>Dim TempDiv As Currency<BR>Dim AfterDot As Integer<BR>Dim Pos As Integer<BR>TempHold = Len(InputData)<BR>TempDiv = (TempHold / 4)<BR>Pos = InStr(1, CStr(TempDiv), ".")<BR>If Pos = 0 Then<BR> NumBlocks = TempDiv<BR> GoTo DoBlocks<BR>End If<BR>AfterDot = Mid(CStr(TempDiv), (Pos + 1))<BR>If AfterDot = 25 Then<BR> FullBin = "000" + FullBin<BR> NumBlocks = (Len(FullBin) / 4)<BR>ElseIf AfterDot = 5 Then<BR> FullBin = "00" + FullBin<BR> NumBlocks = (Len(FullBin) / 4)<BR>ElseIf AfterDot = 75 Then<BR> FullBin = "0" + FullBin<BR> NumBlocks = (Len(FullBin) / 4)<BR>Else<BR> MsgBox "Big Time Screw up happened, WAHHHHHHHHHHH", vbInformation<BR> Exit Function<BR>End If<BR> GoTo DoBlocks<BR>End If<BR>DoBlocks:<BR>HexOut = ""<BR>For I = 1 To Len(FullBin) Step 4<BR> TempBinBlock = Mid(FullBin, I, 4)<BR>If TempBinBlock = "0000" Then<BR> HexOut = HexOut + "0"<BR>ElseIf TempBinBlock = "0001" Then<BR> HexOut = HexOut + "1"<BR>ElseIf TempBinBlock = "0010" Then<BR> HexOut = HexOut + "2"<BR>ElseIf TempBinBlock = "0011" Then<BR> HexOut = HexOut + "3"<BR>ElseIf TempBinBlock = "0100" Then<BR> HexOut = HexOut + "4"<BR>ElseIf TempBinBlock = "0101" Then<BR> HexOut = HexOut + "5"<BR>ElseIf TempBinBlock = "0110" Then<BR> HexOut = HexOut + "6"<BR>ElseIf TempBinBlock = "0111" Then<BR> HexOut = HexOut + "7"<BR>ElseIf TempBinBlock = "1000" Then<BR> HexOut = HexOut + "8"<BR>ElseIf TempBinBlock = "1001" Then<BR> HexOut = HexOut + "9"<BR>ElseIf TempBinBlock = "1010" Then<BR> HexOut = HexOut + "A"<BR>ElseIf TempBinBlock = "1011" Then<BR> HexOut = HexOut + "B"<BR>ElseIf TempBinBlock = "1100" Then<BR> HexOut = HexOut + "C"<BR>ElseIf TempBinBlock = "1101" Then<BR> HexOut = HexOut + "D"<BR>ElseIf TempBinBlock = "1110" Then<BR> HexOut = HexOut + "E"<BR>ElseIf TempBinBlock = "1111" Then<BR> HexOut = HexOut + "F"<BR>End If<BR>Next I<BR>Bin2Hex = HexOut<BR>eds:<BR>End Function
Function Hex2Bin(InputData As String) As String<BR>Dim I As Integer:Dim BinOut As String:Dim Lenhex As Integer<BR>InputData = UCase(InputData)<BR>Lenhex = Len(InputData)<BR>For I = 1 To Lenhex<BR>If IsNumeric(Mid(InputData, I, 1)) Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "A" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "B" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "C" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "D" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "E" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "F" Then<BR> GoTo NumOk<BR>Else<BR> MsgBox "Number given is not in Hex format", vbCritical<BR> Exit Function<BR>End If<BR>NumOk:<BR>Next I<BR>BinOut = ""<BR>For I = 1 To Lenhex<BR>If Mid(InputData, I, 1) = "0" Then<BR> BinOut = BinOut + "0000"<BR>ElseIf Mid(InputData, I, 1) = "1" Then<BR> BinOut = BinOut + "0001"<BR>ElseIf Mid(InputData, I, 1) = "2" Then<BR> BinOut = BinOut + "0010"<BR>ElseIf Mid(InputData, I, 1) = "3" Then<BR> BinOut = BinOut + "0011"<BR>ElseIf Mid(InputData, I, 1) = "4" Then<BR> BinOut = BinOut + "0100"<BR>ElseIf Mid(InputData, I, 1) = "5" Then<BR> BinOut = BinOut + "0101"<BR>ElseIf Mid(InputData, I, 1) = "6" Then<BR> BinOut = BinOut + "0110"<BR>ElseIf Mid(InputData, I, 1) = "7" Then<BR> BinOut = BinOut + "0111"<BR>ElseIf Mid(InputData, I, 1) = "8" Then<BR> BinOut = BinOut + "1000"<BR>ElseIf Mid(InputData, I, 1) = "9" Then<BR> BinOut = BinOut + "1001"<BR>ElseIf Mid(InputData, I, 1) = "A" Then<BR> BinOut = BinOut + "1010"<BR>ElseIf Mid(InputData, I, 1) = "B" Then<BR> BinOut = BinOut + "1011"<BR>ElseIf Mid(InputData, I, 1) = "C" Then<BR> BinOut = BinOut + "1100"<BR>ElseIf Mid(InputData, I, 1) = "D" Then<BR> BinOut = BinOut + "1101"<BR>ElseIf Mid(InputData, I, 1) = "E" Then<BR> BinOut = BinOut + "1110"<BR>ElseIf Mid(InputData, I, 1) = "F" Then<BR> BinOut = BinOut + "1111"<BR>Else<BR> MsgBox "Something is Screwed up, Wahhhhhhhhhhh", vbCritical<BR>End If<BR>Next I<BR>Hex2Bin = BinOut<BR>eds:<BR>End Function<BR>Function Hex2Dec(InputData As String) As Double<BR>Dim I As Integer:Dim DecOut As Double:Dim Lenhex As Integer:Dim HexStep As Double<BR>DecOut = 0<BR>InputData = UCase(InputData)<BR>Lenhex = Len(InputData)<BR>For I = 1 To Lenhex<BR>If IsNumeric(Mid(InputData, I, 1)) Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "A" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "B" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "C" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "D" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "E" Then<BR> GoTo NumOk<BR>ElseIf Mid(InputData, I, 1) = "F" Then<BR> GoTo NumOk<BR>Else<BR> MsgBox "Number given is not in Hex format", vbCritical<BR> Exit Function<BR>End If<BR>NumOk:<BR>Next I<BR>HexStep = 0<BR>For I = Lenhex To 1 Step -1<BR>HexStep = HexStep * 16<BR>If HexStep = 0 Then<BR> HexStep = 1<BR>End If<BR> If Mid(InputData, I, 1) = "0" Then <BR> DecOut = DecOut + (0 * HexStep) <BR> ElseIf Mid(InputData, I, 1) = "1" Then<BR> DecOut = DecOut + (1 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "2" Then<BR> DecOut = DecOut + (2 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "3" Then<BR> DecOut = DecOut + (3 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "4" Then<BR> DecOut = DecOut + (4 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "5" Then<BR> DecOut = DecOut + (5 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "6" Then<BR> DecOut = DecOut + (6 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "7" Then<BR> DecOut = DecOut + (7 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "8" Then<BR> DecOut = DecOut + (8 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "9" Then<BR> DecOut = DecOut + (9 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "A" Then<BR> DecOut = DecOut + (10 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "B" Then<BR> DecOut = DecOut + (11 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "C" Then<BR> DecOut = DecOut + (12 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "D" Then<BR> DecOut = DecOut + (13 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "E" Then<BR> DecOut = DecOut + (14 * HexStep)<BR> ElseIf Mid(InputData, I, 1) = "F" Then<BR> DecOut = DecOut + (15 * HexStep)<BR> Else<BR> MsgBox "Something is Screwed up, Wahhhhhhhhhhh", vbCritical<BR> End If<BR>Next I<BR>Hex2Dec = DecOut<BR>eds:<BR>End Function<BR>调用方式:<BR>Private Sub cmdbin2hex_Click()<BR> txthex.Text = Bin2Hex(txtbinary.Text)<BR>End Sub<BR>Private Sub cmddec2bin_Click()<BR>If IsNumeric(txtdec2bin.Text) Then<BR> txtdec2bin2.Text = Dec2Bin(txtdec2bin.Text)<BR>End If<BR>End Sub<BR>Private Sub cmdDecHex_Click()<BR>If IsNumeric(txtDecimal.Text) Then<BR> txtdechex.Text = Hex(CDbl(txtDecimal.Text))<BR>Else<BR> MsgBox "Not a Number.", vbCritical<BR>End If<BR>End Sub<BR>Private Sub cmdhex2bin_Click()<BR> txtbinary2.Text = Hex2Bin(txthex2.Text)<BR>End Sub<BR>Private Sub cmdhexdec_Click()<BR> txtdec2.Text = CStr(Hex2Dec(txthexdec.Text))<BR>End Sub
yulijin608
发表于 2005-1-6 11:53:00
53. 控制左右声道<BR>Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _<BR> Long, ByVal hwndCallback As Long) As Long<BR>Private Sub Command1_Click()<BR>PlaySound "F:\music\incubus\水木年华-再见了最爱的人.mp3"<BR>End Sub<BR>Function PlaySound(ByVal FileName As String) As Boolean<BR> Dim cmd As String, exName As String<BR> exName = Right(FileName, 3)<BR> mciSendString "close " & exName, 0, 0, 0<BR> cmd = "open " & FileName & " alias " & exName<BR> mciSendString cmd, 0, 0, 0<BR> PlaySound = mciSendString("play " & exName, 0, 0, 0)<BR>End Function<BR>Private Sub Command2_Click()<BR> Static flag As Boolean ' 设置左声道开关<BR> mciSendString "set all audio all " & IIf(flag, "on", "off"), 0, 0, 0<BR> If flag = False Then<BR> Command2.Caption = "左声道(关)"<BR> Else<BR> Command2.Caption = "左声道(开)"<BR> End If<BR> flag = Not flag<BR>End Sub<BR>Private Sub Command3_Click()<BR> Static flag As Boolean ' 设置右声道开关<BR> mciSendString "set all audio all " & IIf(flag, "on", "off"), 0, 0, 0<BR> If flag = False Then<BR> Command3.Caption = "右声道(关)"<BR> Else<BR> Command3.Caption = "右声道(开)"<BR> End If<BR> flag = Not flag<BR>End Sub<BR>Private Sub Command4_Click() '' 设置mp3设备音量:0--1000,500表示音量适中<BR> mciSendString "set mp3 audio volume to 500", 0, 0, 0 <BR>End Sub<BR>54.利用VB产生屏幕变暗的效果(转,别人的代码)<BR>Private Type RECT<BR>Left As Long<BR>Top As Long<BR>Right As Long<BR>Bottom As Long<BR>End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long<BR>Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long<BR>Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long<BR>Private Declare Function PatBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal dwRop As Long) As Long<BR>Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long<BR>Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long<BR>Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long<BR>Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
Private bybits(1 To 16) As Byte<BR>Private hBitmap As Long, hBrush As Long<BR>Private hDesktopWnd As Long
Private Sub Command1_Click()<BR>Dim rop As Long, res As Long<BR>Dim hdc5 As Long, width5 As Long, height5 As Long
hdc5 = GetDC(0)<BR>width5 = Screen.Width \ Screen.TwipsPerPixelX<BR>height5 = Screen.Height \ Screen.TwipsPerPixelY
rop = &HA000C9<BR>Call SelectObject(hdc5, hBrush)<BR>res = PatBlt(hdc5, 0, 0, width5, height5, rop)<BR>Call DeleteObject(hBrush)
res = ReleaseDC(0, hdc5)<BR>End Sub
Private Sub Command2_Click()<BR>Dim aa As Long
<BR>aa = InvalidateRect(0, 0, 1)<BR>End Sub
Private Sub Form_Load()<BR>Dim ary<BR>Dim i As Long<BR>ary = Array(&H55, &H0, &HAA, &H0, _<BR>&H55, &H0, &HAA, &H0, _<BR>&H55, &H0, &HAA, &H0, _<BR>&H55, &H0, &HAA, &H0)<BR>For i = 1 To 16<BR>bybits(i) = ary(i - 1)<BR>Next i<BR>hBitmap = CreateBitmap(8, 8, 1, 1, bybits(1))<BR>hBrush = CreatePatternBrush(hBitmap)<BR>Picture1.ForeColor = RGB(0, 0, 0)<BR>Picture1.BackColor = RGB(255, 255, 255)<BR>Picture1.ScaleMode = 3<BR>End Sub<BR>55.限定鼠标在某一区域内<BR>Type RECT<BR> Left As Long<BR> Top As Long<BR> Right As Long<BR> Bottom As Long<BR>End Type<BR> Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long<BR>Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long<BR>Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _<BR> ByVal y As Long) As Long<BR>Declare Function GetWindowRect Lib "user32" _<BR> (ByVal hwnd As Long, lpRect As RECT) As Long<BR> <BR>'设定Mouse可移动的围是在某个control项之内<BR>Public Function toLockCursor(ByVal ctlHwnd As Long) As Boolean<BR>Dim rect5 As RECT<BR>Dim res As Long<BR>GetWindowRect ctlHwnd, rect5 '取得window的四个角<BR>SetCursorPos (rect5.Top + rect5.Bottom) \ 2, (rect5.Left + rect5.Right) \ 2<BR> res = ClipCursor(rect5)<BR>If res = 1 Then<BR> toLockCursor = True<BR>Else<BR> toLockCursor = False<BR>End If<BR>End Function<BR> <BR>'设定Mouse移动的围为个萤幕<BR>Public Sub toUnLockCursor()<BR>Dim rscreen As RECT<BR>rscreen.Top = 0<BR>rscreen.Left = 0<BR>rscreen.Right = Screen.Width \ Screen.TwipsPerPixelX<BR>rscreen.Bottom = Screen.Height \ Screen.TwipsPerPixelY<BR>ClipCursor rscreen<BR>End Sub
Private Sub Command1_Click()<BR> Call toLockCursor(Me.hwnd) '把Me.hwnd改为其他控件的句柄,则鼠标就限制在这个区域里。<BR>End Sub<BR>Private Sub Command2_Click()<BR> Call toUnLockCursor<BR>End Sub
56.获得屏幕分辨率<BR>方法一:<BR>Debug.Print Screen.Width / Screen.TwipsPerPixelX<BR>Debug.Print Screen.Height / Screen.TwipsPerPixelY<BR>方法二:<BR>Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long<BR>Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long<BR>Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long<BR>Public Sub DeviceInfo(DisplayX As Integer, DisplayY As Integer, DisplayColor As Integer)<BR>Dim hdesktopwnd<BR>Dim hdccaps<BR>Dim lblRes As String<BR>Dim DisplayBits<BR>Dim DisplayPlanes<BR>Dim RetVal<BR>hdccaps = GetDC(hdesktopwnd)<BR>DisplayBits = GetDeviceCaps(hdccaps, 12)<BR>DisplayPlanes = GetDeviceCaps(hdccaps, 14)<BR>DisplayX = GetDeviceCaps(hdccaps, 8)<BR>DisplayY = GetDeviceCaps(hdccaps, 10)<BR>RetVal = ReleaseDC(hdesktopwnd, hdccaps)<BR>Select Case DisplayBits<BR>Case 1<BR>If DisplayPlanes = 1 Then<BR>DisplayColor = 1<BR>Else<BR>If DisplayPlanes = 4 Then DisplayColor = 4 Else DisplayColor = 0<BR>End If<BR>Case 8<BR>DisplayColor = 8<BR>Case 16<BR>DisplayColor = 16<BR>Case 24<BR>DisplayColor = 24<BR>Case 32<BR>DisplayColor = 32<BR>Case Else<BR>DisplayColor = 0 '未知色彩度<BR>End Select<BR>End Sub
Private Sub Command1_Click()<BR>Dim x As Integer, y As Integer, color As Integer<BR>DeviceInfo x, y, color<BR>MsgBox "分辨率为 " & x & "x" & y<BR>End Sub
yulijin608
发表于 2005-1-6 11:54:00
57.动态添加菜单<BR>Const MF_CHECKED = &H8&<BR>Const MF_APPEND = &H100&<BR>Const TPM_LEFTALIGN = &H0&<BR>Const MF_DISABLED = &H2&<BR>Const MF_GRAYED = &H1&<BR>Const MF_SEPARATOR = &H800&<BR>Const MF_STRING = &H0&<BR>Const TPM_RETURNCMD = &H100&<BR>Const TPM_RIGHTBUTTON = &H2&<BR>Private Type POINTAPI<BR> x As Long<BR> y As Long<BR>End Type<BR>Private Declare Function CreatePopupMenu Lib "user32" () As Long<BR>Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal HWnd As Long, ByVal lptpm As Any) As Long<BR>Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long<BR>Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long<BR>Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long<BR>Dim hMenu As Long<BR>Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)<BR> If Button = 2 Then<BR> Dim Pt As POINTAPI<BR> Dim ret As Long<BR> hMenu = CreatePopupMenu()<BR> AppendMenu hMenu, MF_STRING, 1, "Hello !"<BR> AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, 2, "Testing ..."<BR> AppendMenu hMenu, MF_SEPARATOR, 3, ByVal 0&<BR> AppendMenu hMenu, MF_CHECKED, 4, "TrackPopupMenu"<BR> GetCursorPos Pt<BR> ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, Pt.x, Pt.y, Me.HWnd, ByVal 0&)<BR> DestroyMenu hMenu<BR> Select Case ret<BR> Case 1<BR> MsgBox "Hello !"<BR> Case 4<BR> MsgBox "TrackPopupMenu"<BR> End Select<BR> End If<BR>End Sub<BR>58.利用API函数实现定时器功能<BR>模块中:<BR>Option Explicit<BR>Public lTimerId As Long<BR>Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long<BR>Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long<BR>Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)<BR> Static i As Long<BR> Form1.Label1.Caption = i<BR> i = i + 1<BR>End Sub<BR>Public Sub StartTimer(lMinute As Long)<BR> lTimerId = SetTimer(0, 0, lMinute, AddressOf TimerProc)<BR>End Sub<BR>Public Function StopTimer(lTimerId As Long) As Long<BR> StopTimer = KillTimer(0, lTimerId)<BR>End Function
窗体中:<BR>Private Sub Form_Load()<BR>StartTimer 1000<BR>End Sub
Private Sub Form_Unload(Cancel As Integer)<BR> StopTimer lTimerId<BR>End Sub<BR>59.创建GUID
'GUID是Globally Unique IDentifier的缩写.由一个特殊的算法来产生这些128位的数,并保证不产生重复的GUID—重复的可能性当然存在,但有太多可用的数了,因此算法特别防止产生重复的数,这种情况你一生都不会看到. ActiveX控件都有一个用于相互区别的GUID.你如何在自己的程序中使用GUID呢? 例如,当一个数据库的每个条目都需要由一个唯一的键值时.下面的代码将给你一个答案:
Option Explicit<BR>Private Type GUID<BR> Data1 As Long<BR> Data2 As Long<BR> Data3 As Long<BR> Data4(8) As Byte<BR>End Type<BR>Private Declare Function CoCreateGuid Lib "ole32.dll" (pguid As GUID) As Long<BR>Private Declare Function StringFromGUID2 Lib "ole32.dll" (rguid As Any, ByVal lpstrClsId As Long, ByVal cbMax As Long) As Long<BR> <BR>Private Function GUIDGen() As String<BR> Dim uGUID As GUID<BR> Dim sGUID As String<BR> Dim bGUID() As Byte<BR> Dim lLen As Long<BR> Dim RetVal As Long<BR> lLen = 40<BR> bGUID = String(lLen, 0)<BR> CoCreateGuid uGUID '把结构转换为一个可显示的字符串<BR> RetVal = StringFromGUID2(uGUID, VarPtr(bGUID(0)), lLen)<BR> sGUID = bGUID<BR> If (Asc(Mid$(sGUID, RetVal, 1)) = 0) Then RetVal = RetVal - 1<BR> GUIDGen = Left$(sGUID, RetVal)<BR>End Function<BR> <BR>Private Sub cmdGUID_Click()<BR> txtGUID.Text = GUIDGen<BR>End Sub
60.创建渐变窗体<BR>Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long<BR>Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long<BR>Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long<BR>Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long<BR> Private Type RECT<BR> left As Long<BR> top As Long<BR> right As Long<BR> bottom As Long<BR> End Type<BR>Private Sub Form_Paint()<BR> Dim Color As Long<BR> Dim hBrush As Long<BR> Dim OldMode As Long<BR> Dim RetVal As Long<BR> Dim StepSize As Long<BR> Dim X As Long<BR> Dim FillArea As RECT<BR> OldMode = Me.ScaleMode<BR> Me.ScaleMode = 3<BR> StepSize = 1 + Me.ScaleHeight / 80<BR> Color = 255<BR> FillArea.left = 0<BR> FillArea.right = Me.ScaleWidth<BR> FillArea.top = 0<BR> FillArea.bottom = StepSize<BR> For X = 1 To 80<BR> hBrush = CreateSolidBrush(RGB(Color / 2, Color * 2, Color))<BR> RetVal = FillRect(Me.hdc, FillArea, hBrush)<BR> RetVal = DeleteObject(hBrush)<BR> Color = Color - 2<BR> If Color < 0 Then Color = 0<BR> FillArea.top = FillArea.bottom<BR> FillArea.bottom = FillArea.bottom + StepSize<BR> Next<BR> Me.ScaleMode = OldMode<BR> End Sub<BR>
yulijin608
发表于 2005-1-6 11:56:00
61.禁止屏幕保护<BR>Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, ByVal fuWinIni As Long) As Long<BR>Const SPI_SETSCREENSAVEACTIVE = 17<BR>Const SPIF_SENDWININICHANGE = &H2<BR>Const SPIF_UPDATEINIFILE = &H1
Private Sub Form_Load()<BR>SystemParametersInfo SPI_SETSCREENSAVEACTIVE, 0, 0, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE<BR>End Sub
62.类似QQ界面的按钮。<BR>command以picturebox为容器。<BR>Private Sub Form_Load()<BR>Me.WindowState = 2<BR>With Picture1<BR>.Width = 1200 + 60<BR>.Height = 4860<BR>End With<BR>Dim i As Integer<BR>For i = Command1.Count - 1 To 0 Step -1<BR>With Command1(i)<BR>.Width = 1200<BR>.Height = 300<BR>.Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)<BR>.Left = 0<BR>.Caption = "分组 " & i + 1<BR>End With<BR>Next i<BR>Command1(0).Top = 0<BR>End Sub
Private Sub Command1_Click(Index As Integer)<BR>Picture1.SetFocus<BR>'把焦点给Picture1是为了不让按钮出现难看的黑框<BR>Dim i As Integer<BR>For i = 1 To Index<BR>Command1(i).Top = 300 * i<BR>Next i<BR>For i = Command1.Count - 1 To Index + 1 Step -1<BR>Command1(i).Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)<BR>Next i<BR>End Sub
63.alpha blend<BR>Private Declare Function AlphaBlend Lib "msimg32" ( ByVal hDestDC As Long, _<BR> ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _<BR> ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal widthSrc As Long, _<BR> ByVal heightSrc As Long, ByVal dreamAKA As Long) As Long 'only Windows 98 or Latter<BR> Dim Num As Byte, nN%, nBlend&<BR> Private Sub Run_Blending()<BR> Num = 255<BR> nN = 5<BR>Do<BR> DoEvents<BR>nBlend = vbBlue - CLng(Num) * (vbYellow + 1)<BR>Num = Num - nN<BR> If Num = 0 Then<BR> nN = -5<BR> ElseIf Num = 255 Then<BR> nN = 5<BR> End If<BR> Me.Cls<BR> AlphaBlend Me.hDC, 0, 0, picSrc.ScaleWidth, picSrc.ScaleHeight, picSrc.hDC, 0, 0, picSrc.ScaleWidth, picSrc.ScaleHeight, nBlend<BR>Loop<BR>End Sub<BR>Private Sub Form_Activate()<BR> Call Run_Blending<BR>End Sub<BR>Private Sub Form_Unload(Cancel As Integer)<BR> End ' STOP Do Loop<BR>End Sub
64.简单贝赛尔曲线绘制<BR>Option Explicit<BR>Private Type POINTAPI<BR> x As Long<BR> y As Long<BR>End Type
Private Declare Function PolyBezier Lib "gdi32" _<BR> (ByVal hdc As Long, _<BR> lppt As POINTAPI, _<BR> ByVal cpoints As Long) As Long
Dim Points(0 To 3) As POINTAPI<BR>Dim oldPoint As POINTAPI<BR>Dim Index As Integer<BR>Private Sub form_load()<BR> Caption = "绘制贝塞尔曲线"<BR> ScaleMode = 3<BR>End Sub
Private Sub form_mousedown(button As Integer, _<BR> shift As Integer, x As Single, y As Single)<BR> Points(Index).x = x: Points(Index).y = y<BR> If Index = 0 Then<BR> Cls<BR> Else<BR> Line (oldPoint.x, oldPoint.y)-(x, y) '绘制特征多边形<BR> End If<BR> oldPoint.x = x: oldPoint.y = y<BR> Circle (x, y), 3, vbBlue<BR> If Index = 3 Then<BR> Form1.ForeColor = vbRed<BR> PolyBezier Me.hdc, Points(0), 4 '绘制贝赛尔曲线<BR> Index = 0<BR> Else<BR> Index = Index + 1<BR> End If<BR>End Sub
yulijin608
发表于 2005-1-6 11:59:00
65.读写INI文件模块
Option Explicit<BR>'读写INI文件模块<BR>Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long<BR>Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal Filename As String) As Long
Public Function myReadINI(iniFileName, iniSection, iniKey, iniDefault)<BR>'该函数的使用与读注册表类似<BR>'inifilename为INI文件名,inisection为INI文件中的项目,inikey为项目下的键名称,inidefault为默认键值<BR>'If no section (appname), default is first appname<BR>'(若无项目名,默认为初始名称)<BR>'if no key, default is first key<BR>'(若无键名,默认为初始键名)<BR> Dim lpApplicationName As String<BR> Dim lpKeyName As String<BR> Dim lpDefault As String<BR> Dim lpReturnedString As String<BR> Dim nSize As Long<BR> Dim lpFileName As String<BR> Dim retval As Long<BR> Dim Filename As String<BR> '判断INI文件是否存在<BR> If Dir(iniFileName) <> "" Then<BR> lpDefault = Space$(254)<BR> lpDefault = iniDefault <BR> lpReturnedString = Space$(254) <BR> nSize = 254<BR> lpFileName = iniFileName<BR> lpApplicationName = iniSection<BR> lpKeyName = iniKey<BR> Filename = lpFileName<BR> retval = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)<BR> myReadINI = lpReturnedString<BR> End If<BR>End Function
Public Function myWriteINI(iniFileName As String, iniSection As String, iniKey As String, Info As String) As String<BR> '该函数的使用与写注册表类似,可在INI文件中添加或修改项、键、值<BR> 'iniFileName为INI文件名,inisection为INI文件中的项目,inikey为项目下的键名称,Info为键值<BR> Dim retval As Long<BR> retval = WritePrivateProfileString(iniSection, iniKey, Info, iniFileName)<BR> myWriteINI = LTrim$(Str$(retval))<BR>End Function
Public Sub DelSectionINI(iniFileName As String, iniSection As String)<BR> '该过程可删除INI文件中指定的项<BR> 'iniFileName为INI文件名,iniSection为指定的项 <BR> '判断INI文件是否存在<BR> If Dir(iniFileName) <> "" Then<BR> WritePrivateProfileString iniSection, vbNullString, vbNullString, iniFileName<BR> End If<BR> <BR>End Sub
Public Sub DelKeyINI(iniFileName As String, iniSection As String, iniKey As String)<BR> '该过程可删除INI文件中指定的键<BR> 'iniFileName为INI文件名,iniSection为指定的项,iniKey为指定的键 <BR> '判断INI文件是否存在<BR> If Dir(iniFileName) <> "" Then<BR> WritePrivateProfileString iniSection, iniKey, vbNullString, iniFileName<BR> End If<BR>End Sub
Public Sub DelValueINI(iniFileName As String, iniSection As String, iniKey As String)<BR> '该过程可删除INI文件中指定键的值<BR> 'iniFileName为INI文件名,iniSection为指定的项,iniKey为指定的键 <BR> '判断INI文件是否存在<BR> If Dir(iniFileName) <> "" Then<BR> WritePrivateProfileString iniSection, iniKey, "", iniFileName<BR> End If<BR>End Sub
Public Sub DelFileINI(iniFileName As String)<BR> '该过程可删除INI文件<BR> 'iniFileName为INI文件名<BR> '判断INI文件是否存在<BR> If Dir(iniFileName) <> "" Then<BR> Kill iniFileName<BR> End If<BR>End Sub<BR>66.显示浏览文件夹对话框
Option Explicit<BR>'显示浏览文件夹对话框<BR>' 调用方式:: string = BrowseForFolders(Hwnd,TitleOfDialog)<BR>' 例如:String1 = BrowseForFolders(Hwnd, "Select target folder...")<BR>Public Type BrowseInfo<BR>hwndOwner As Long<BR>pIDLRoot As Long<BR>pszDisplayName As Long<BR>lpszTitle As Long<BR>ulFlags As Long<BR>lpfnCallback As Long<BR>lParam As Long<BR>iImage As Long<BR>End Type<BR>Public Const BIF_RETURNONLYFSDIRS = 1<BR>Public Const MAX_PATH = 260<BR>Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)<BR>Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long<BR>Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long<BR>Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String<BR> Dim iNull As Integer<BR> Dim lpIDList As Long<BR> Dim lResult As Long<BR> Dim sPath As String<BR> Dim udtBI As BrowseInfo<BR> '初始化变量<BR> With udtBI<BR> .hwndOwner = hwndOwner<BR> .lpszTitle = lstrcat(sPrompt, "")<BR> .ulFlags = BIF_RETURNONLYFSDIRS<BR> End With<BR> '调用 API<BR> lpIDList = SHBrowseForFolder(udtBI)<BR> If lpIDList Then<BR> sPath = String$(MAX_PATH, 0)<BR> lResult = SHGetPathFromIDList(lpIDList, sPath)<BR> Call CoTaskMemFree(lpIDList)<BR> iNull = InStr(sPath, vbNullChar)<BR> If iNull Then sPath = Left$(sPath, iNull - 1)<BR> End If<BR> '如果选择取消, sPath = ""<BR> BrowseForFolder = sPath<BR>End Function<BR>67.注册表读写模块
Option Explicit<BR>'注册表读写模块<BR>'This program needs 3 buttons<BR>Public Const REG_DWORD = 4<BR>Const ERROR_SUCCESS = 0&<BR>Const KEY_ALL_ACCESS = &H3F<BR>Public Const REG_SZ = 1 ' Unicode nul terminated string<BR>Public Const REG_BINARY = 3 ' Free form binary<BR>Public Const HKEY_CURRENT_USER = &H80000001<BR>Public Const HKEY_LOCAL_MACHINE = &H80000002<BR>Public Const REG1_KEY = "software\microsoft\windows\currentversion\run"<BR>Public Const REG2_KEY = "software\microsoft\windows\currentversion\RunServices"
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long<BR>Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long<BR>Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long<BR>Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long<BR>Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long<BR>Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long<BR>Public Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String<BR> Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long<BR> 'retrieve nformation about the key<BR> lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)<BR> If lResult = 0 Then<BR> If lValueType = REG_SZ Then<BR> 'Create a buffer<BR> strBuf = String(lDataBufSize, Chr$(0))<BR> 'retrieve the key's content<BR> lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)<BR> If lResult = 0 Then<BR> 'Remove the unnecessary chr$(0)'s<BR> RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)<BR> End If<BR> ElseIf lValueType = REG_BINARY Then<BR> Dim strData As Integer<BR> 'retrieve the key's value<BR> lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)<BR> If lResult = 0 Then<BR> RegQueryStringValue = strData<BR> End If<BR> End If<BR> End If<BR>End Function<BR>Public Function GetString(hKey As Long, strPath As String, strValue As String)<BR> Dim Ret<BR> 'Open the key<BR> RegOpenKey hKey, strPath, Ret<BR> 'Get the key's content<BR> GetString = RegQueryStringValue(Ret, strValue)<BR> 'Close the key<BR> RegCloseKey Ret<BR>End Function<BR>Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)<BR> Dim Ret<BR> 'Create a new key<BR> RegCreateKey hKey, strPath, Ret<BR> 'Save a string to the key<BR> RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)<BR> 'close the key<BR> RegCloseKey Ret<BR>End Sub<BR>Public Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)<BR> Dim Ret<BR> 'Create a new key<BR> RegCreateKey hKey, strPath, Ret<BR> 'Set the key's value<BR> RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4<BR> 'close the key<BR> RegCloseKey Ret<BR>End Sub<BR>Public Sub DelSetting(hKey As Long, strPath As String, strValue As String)<BR> Dim Ret<BR> 'Create a new key<BR> RegCreateKey hKey, strPath, Ret<BR> 'Delete the key's value<BR> RegDeleteValue Ret, strValue<BR> 'close the key<BR> RegCloseKey Ret<BR>End Sub<BR>Public Sub SaveRegDWORD(hKey As Long, strPath As String, strValueName As String, strData As String)<BR> Dim Ret<BR> 'Create a new key<BR> RegCreateKey hKey, strPath, Ret<BR> 'Save a DWORD to the key<BR> RegSetValueEx Ret, strValueName, 0, REG_DWORD, CByte(strData), 4<BR> 'close the key<BR> RegCloseKey Ret
End Sub<BR>68.API打造浮动按钮
Option Base 1<BR>Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long<BR>Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long<BR>Public 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 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)<BR>Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long<BR>Public Declare Function TrackMouseEvent Lib "user32" (lpEventTrack As TRACKMOUSEEVENTTYPE) As Long<BR>'--------------------------------------------------------------------------------------------GDI相关函数<BR>Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long<BR>Public Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long<BR>Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long<BR>Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long<BR>Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long<BR>Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long<BR>Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long<BR>Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long<BR>'--------------------------------------------------------------------------------------------<BR>Public Const TME_LEAVE = &H2&<BR>Public Const ODS_SELECTED = &H1<BR>Public Const ODT_BUTTON = 4<BR>Public Const WM_DRAWITEM = &H2B<BR>Public Const WM_MEASUREITEM = &H2C<BR>Public Const IMAGE_BITMAP = 0<BR>Public Const LR_LOADFROMFILE = &H10<BR>Public Const BS_OWNERDRAW = &HB&<BR>Public Const GWL_WNDPROC = (-4)<BR>Public Const WM_MOUSEMOVE = &H200<BR>Public Const WM_MOUSELEAVE = &H2A3<BR>Public Const WM_LBUTTONUP = &H202<BR>Public Const WS_CHILD = &H40000000<BR>Public Const WS_VISIBLE = &H10000000<BR>Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source<BR>'-------------------------以下是自定义按钮状态常数<BR>Public Const Leave = 1 '离开按钮范围<BR>Public Const Click = 2 ' 按下按钮<BR>Public Const Undo = 3 '松开按钮<BR>Public Type RECT<BR> Left As Long<BR> Top As Long<BR> Right As Long<BR> Bottom As Long<BR>End Type<BR>Public Type DRAWITEMSTRUCT '自绘控件的绘图结构,另外由于它在WIN32里面是唯一的结构,所以在VB里面要用到CopyMemory这个API函数直接指向它的地址<BR> CtlType As Long<BR> CtlID As Long<BR> ItemID As Long<BR> itemAction As Long<BR> itemState As Long<BR> hwndItem As Long<BR> hdc As Long<BR> rcItem As RECT<BR> itemData As Long<BR>End Type<BR>Public Type MEASUREITEMSTRUCT '自绘时候设置控件的大小'同上<BR> CtlType As Long<BR> CtlID As Long<BR> ItemID As Long<BR> itemWidth As Long<BR> itemHeight As Long<BR> itemData As Long<BR>End Type<BR>Public Type TRACKMOUSEEVENTTYPE<BR> cbSize As Long<BR> dwFlags As Long<BR> hwndTrack As Long<BR> dwHoverTime As Long<BR>End Type<BR>Public ImageHandle(3) As Long<BR>Public OldMainProc As Long<BR>Public OldButtonProc As Long<BR>Public CmdHwnd As Long<BR>Public MouseLeave As Boolean<BR>Public Sub Initialize() '初始化<BR>LoadPic<BR>MouseLeave = True<BR>MainProc<BR>CreateOwnerDrawButton<BR>ButtonProc<BR>End Sub<BR>Public Sub MainProc() '窗口自类化(NewMainProc)<BR>OldMainProc = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf NewMainProc)<BR>End Sub<BR>Public Sub ButtonProc() '按钮自类化(NewButtonProc)<BR>OldButtonProc = SetWindowLong(CmdHwnd, GWL_WNDPROC, AddressOf NewButtonProc)<BR>End Sub<BR>Public Sub CreateOwnerDrawButton() '创造一个自绘按钮<BR>CmdHwnd = CreateWindowEx(0, "Button", "", WS_CHILD Or BS_OWNERDRAW Or WS_VISIBLE, 50, 60, 70, 25, Form1.hwnd, 0, App.hInstance, 0)<BR>Dim dc As Long<BR>dc = GetDC(CmdHwnd)<BR>drawPic Leave
End Sub<BR>Public Function NewMainProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '处理主窗口消息<BR> Select Case Msg<BR> Case WM_DRAWITEM<BR> OnDrawItem lParam<BR> Exit Function<BR> Case WM_MEASUREITEM<BR> OnMeasureItem lParam<BR> End Select<BR> NewMainProc = CallWindowProc(OldMainProc, hwnd, Msg, wParam, lParam)<BR>End Function<BR>Public Function NewButtonProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '处理按钮消息<BR>Select Case Msg<BR> Case WM_MOUSELEAVE<BR> Button_MouseLeave<BR> MouseLeave = True<BR> Case WM_MOUSEMOVE<BR> Button_MouseMove<BR> Case WM_LBUTTONUP<BR> Button_MouseLButtonUp<BR> End Select<BR> NewButtonProc = CallWindowProc(OldButtonProc, hwnd, Msg, wParam, lParam)<BR>End Function<BR>Public Sub Button_MouseMove() '鼠标移动事件<BR>drawPic Undo<BR> If MouseLeave = True Then<BR> MouseLeave = False<BR> Dim MouseTrack As TRACKMOUSEEVENTTYPE<BR> With MouseTrack<BR> .cbSize = Len(MouseTrack)<BR> .dwFlags = TME_LEAVE<BR> .hwndTrack = CmdHwnd<BR> End With<BR> TrackMouseEvent MouseTrack<BR> End If<BR>End Sub<BR>Public Sub Button_MouseLButtonUp() '左键按下事件<BR>Debug.Print "已按下左键"<BR>End Sub<BR>Public Sub Button_MouseLeave() '离开事件<BR>drawPic Leave<BR>Debug.Print "已离开按钮的范围"<BR>End Sub<BR>Public Sub OnMeasureItem(lParam As Long) '设置的大小<BR>Dim lpMIS As MEASUREITEMSTRUCT<BR> CopyMemory lpMIS, ByVal lParam, Len(lpMIS)<BR> lpMIS.itemHeight = 25<BR> lpMIS.itemWidth = 70<BR> CopyMemory ByVal lParam, lpMIS, Len(lpMIS)<BR>End Sub<BR>Public Sub OnDrawItem(lParam As Long) '为按钮绘制样貌<BR>Dim lpDIS As DRAWITEMSTRUCT<BR>CopyMemory lpDIS, ByVal lParam, Len(lpDIS)<BR>Dim mem As Long<BR>Dim Object As Long<BR> mem = CreateCompatibleDC(hdc)<BR> If lpDIS.CtlType = ODT_BUTTON Then<BR> If lpDIS.itemState And ODS_SELECTED Then '按下时外貌<BR> drawPic Click<BR> Else '松开时外貌<BR> If MouseLeave = True Then<BR> drawPic Leave<BR> Else<BR> drawPic Undo<BR> End If<BR> End If<BR> End If<BR>CopyMemory ByVal lParam, lpDIS, Len(lpDIS)<BR>End Sub<BR>Public Sub LoadPic() '读取图片<BR> ImageHandle(1) = LoadImage(App.hInstance, App.Path & "\" & "1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)<BR> ImageHandle(2) = LoadImage(App.hInstance, App.Path & "\" & "2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)<BR> ImageHandle(3) = LoadImage(App.hInstance, App.Path & "\" & "3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)<BR>End Sub<BR>Public Sub drawPic(State As Long) '为按钮绘制不同状态的图案<BR>Dim hdc As Long<BR>Dim mem As Long<BR>Dim Object As Long<BR> hdc = GetDC(CmdHwnd)<BR> mem = CreateCompatibleDC(hdc)<BR> Object = SelectObject(mem, ImageHandle(State))<BR> BitBlt hdc, 0, 0, 70, 25, mem, 0, 0, SRCCOPY<BR> DeleteObject Object<BR> DeleteDC mem<BR>End Sub<BR>
yulijin608
发表于 2005-1-6 12:04:00
终于上传完了,希望对大家会有所帮助。
tiger8888
发表于 2005-1-6 22:06:00
如此好贴,真是不顶不行!
tfyyf
发表于 2005-1-8 08:21:00
好贴,顶