如何调用系統的【查找】、【替换】的对话框?

版本: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 文件
查看器中找找!


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值