?? 110.txt
字號:
如何調用系統的【查找】、【替換】的對話框?
版本:VB6 / VB5 / VB4-32
在一般的文本編輯軟件中,例如 Windows 本身提供的記事本及寫字板中,我們都可以在【編輯】下拉菜單中,找到【查找】、【替換】二項功能,我想很多人自己在編寫程序時,也都會自己為程序去編寫這二個相當基本的功能。其實根本不用您自己花時間去編寫這樣的程序代碼!
還記得 Microsoft Common Dialog Control (16 位控件是 Comdlg16.ocx,32 位控件是 Comdlg32.ocx) 嗎?我們都知道,這個控件可以幫助我們做到以下幾件事情:
1、ShowOpen:打開文件對話框
2、ShowSave:保存對話框
3、ShowPrinter:打印設置對話框
4、ShowFont:字體對話框
5、ShowColor:顏色對話框
6、ShowHelp:幫助對話框
當然,您若還想要 Microsoft Common Dialog Control 多做一些別的事也沒辦法的!但是,Microsoft 在提供 .ocx 控件的同時,還提供了另外一個控件文檔,也就是 comdlg32.dll,它的功能就多了,除了上面提到的幾種對話框之外,還有好幾個不同功能的對話框,其中就包含【查找】、【替換】二項功能!這二個 API 分別是 FindText 及 ReplaceText 二個。
在程序中,要聲明這二個 API 之前,由于它們都會引用到一個名為 FINDREPLACE 的 Type,所以我們在聲明 Function 之前,必須先聲明 Type FINDREPLACE,程序代碼如下:
在表單的聲明區中加入以下聲明:
'Find/Replace Type Structure
Private Type FINDREPLACE
lStructSize As Long ' size of this struct 0x20
hwndOwner As Long ' handle to owner's window
hInstance As Long ' instance handle of.EXE that contains cust. dlg. template
flags As Long ' one or more of the FR_??
lpstrFindWhat As String ' ptr. to search string
lpstrReplaceWith As String ' ptr. to replace string
wFindWhatLen As Integer ' size of find buffer
wReplaceWithLen As Integer ' size of replace buffer
lCustData As Long ' data passed to hook fn.
lpfnHook As Long ' ptr. to hook fn. or NULL
lpTemplateName As String ' custom template name
End Type
'Common Dialog DLL Calls
Private Declare Function FindText Lib "comdlg32.dll" Alias "FindTextA" _
(pFindreplace As FINDREPLACE) As Long
Private Declare Function ReplaceText Lib "comdlg32.dll" Alias "ReplaceTextA" _
(pFindreplace As FINDREPLACE) As Long
'Delcaration of the type structure
Dim frText As FINDREPLACE
在表單中加入二個 Command Button,并命名為 cmdFind, cmdReplace,加入以下程序代碼:
Private Sub cmdFind_Click()
'Call the find text function
FindText frText
End Sub
Private Sub cmdReplace_Click()
'Call the replace text function
ReplaceText frText
End Sub
Private Sub Form_Load()
'Set the Find/Replace Type properties
With frText
.lpstrReplaceWith = "Replace Text"
.lpstrFindWhat = "Find Text"
.wFindWhatLen = 9
.wReplaceWithLen = 12
.hInstance = App.hInstance
.hwndOwner = Me.hWnd
.lStructSize = LenB(frText)
End With
End Sub
好了,您現在可以按 F5 試試了!
注:在 Type FINDREPLACE 中有一個 flag,您可以代入的 flag 是 FR_??,您可以在 API 文件查看器中找找!
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -