轻松实现“撤消/恢复”功能
【打印文章】
很多应用软件提供的“撤消/恢复”按钮能取消上一步操作或恢复下一步操作,大大地方便了用户,可在自己设计的应用程序中如何实现“撤消/恢复”功能呢?
其实,“撤消”(即取消上一步操作)功能,在VB 6.0中只需调用Windows的应用函数接口(API)中的SendMessage( )函数就可轻松实现,而“恢复”功能只需再次“UNDO”即可!
SendMessage( )函数声明如下:
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
其中函数各参数说明:
hwnd是接收消息的窗口句柄;
wMsg是传递的消息;
wParam、lParam是消息参数。
现将制作过程列举如下:
设计界面:创建一个新工程、在默认窗体Form1中加入控件Command1;设置其Caption属性为“撤消”、加入文本控件Text1,Text属性设为空。
事件声明及事件代码:
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
Dim undo As Boolean
'定义一个逻辑变量
Private Sub Command1_Click()
ls = SendMessage(Text1.hwnd, &&H
304, 0, 0)
'调用SendMessage( )函数
Text1.SetFocus
If undo Then
'撤消/恢复状态的判定
Command1.Caption = "撤消"
Else
Command1.Caption = "恢复"
End If
undo = Not (undo)
End Sub
Private Sub Form_Load()
undo = False
End Sub
按[F5]运行,在文本框中进行某项操作!确实很简单吧!
其实,“撤消”(即取消上一步操作)功能,在VB 6.0中只需调用Windows的应用函数接口(API)中的SendMessage( )函数就可轻松实现,而“恢复”功能只需再次“UNDO”即可!
SendMessage( )函数声明如下:
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
其中函数各参数说明:
hwnd是接收消息的窗口句柄;
wMsg是传递的消息;
wParam、lParam是消息参数。
现将制作过程列举如下:
设计界面:创建一个新工程、在默认窗体Form1中加入控件Command1;设置其Caption属性为“撤消”、加入文本控件Text1,Text属性设为空。
事件声明及事件代码:
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
Dim undo As Boolean
'定义一个逻辑变量
Private Sub Command1_Click()
ls = SendMessage(Text1.hwnd, &&H
304, 0, 0)
'调用SendMessage( )函数
Text1.SetFocus
If undo Then
'撤消/恢复状态的判定
Command1.Caption = "撤消"
Else
Command1.Caption = "恢复"
End If
undo = Not (undo)
End Sub
Private Sub Form_Load()
undo = False
End Sub
按[F5]运行,在文本框中进行某项操作!确实很简单吧!
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】