找回密码
 加入
搜索
查看: 5605|回复: 8

[AU3基础] 如何实现“撤销”这一功能(已解决)

  [复制链接]
发表于 2011-1-8 15:14:54 | 显示全部楼层 |阅读模式
本帖最后由 tuoxiansp 于 2011-1-11 17:20 编辑

想编个类似文本编辑器的软件,不知道如何实现“撤销”功能,望大神们指导
决定用咖啡报表做
发表于 2011-1-8 15:43:19 | 显示全部楼层
回复 1# tuoxiansp

编辑框搞撤消功能:

#Include <GuiEdit.au3>
_GUICtrlEdit_CanUndo($hWnd)

文本列表框的撤消:

#Include <GuiRichEdit.au3>
_GUICtrlRichEdit_Undo($hWnd)


这是帮助里面的源代码,供参考:

#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

Global $hRichEdit, $mnu, $mnuUndo, $mnuRedo, $mnuCut, $mnuCopy
Global $mnuPaste, $mnuPasteSpl, $mnuPasteSplRTF, $mnuPasteSplwObjs

Main()

Func Main()
    Local $hGui
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName,4) &")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_AppendText($hRichEdit, ReadBmpToRtf(FindFirstBMP()) & @CR)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    $mnu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
    $mnuUndo = GUICtrlCreateMenuItem("Undo", $mnu)
    $mnuRedo = GUICtrlCreateMenuItem("Redo", $mnu)
    GUICtrlCreateMenuItem("", $mnu)
    $mnuCut = GUICtrlCreateMenuItem("Cut", $mnu)
    $mnuCopy = GUICtrlCreateMenuItem("Copy", $mnu)
    $mnuPaste = GUICtrlCreateMenuItem("Paste", $mnu)
    $mnuPasteSpl = GUICtrlCreateMenu("Paste Special", $mnu)
    $mnuPasteSplRTF = GUICtrlCreateMenuItem("RTF only", $mnuPasteSpl)
    $mnuPasteSplwObjs = GUICtrlCreateMenuItem("With objects", $mnuPasteSpl)
    _GuiCtrlRichEdit_SetEventMask($hRichEdit, $ENM_MOUSEEVENTS)

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $mnuUndo
                _GuiCtrlRichEdit_Undo($hRichEdit)
            Case $mnuRedo
                _GuiCtrlRichEdit_Redo($hRichEdit)
            Case $mnuCut
                _GuiCtrlRichEdit_Cut($hRichEdit)
            Case $mnuCopy
                _GuiCtrlRichEdit_Copy($hRichEdit)
            Case $mnuPaste
                _GuiCtrlRichEdit_Paste($hRichEdit)
            Case $mnuPasteSplRTF
                _GuiCtrlRichEdit_PasteSpecial($hRichEdit, False)
            Case $mnuPasteSplwObjs
                _GuiCtrlRichEdit_PasteSpecial($hRichEdit, True)
        EndSwitch
    WEnd
EndFunc   ;==>Main

Func WM_NOTIFY($hWnd, $iMsg, $iWparam, $iLparam)
    #forceref $iMsg, $iWparam
    Local $hWndFrom, $iCode, $tNMHDR, $tMsgFilter, $hMenu
    $tNMHDR = DllStructCreate($tagNMHDR, $iLparam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hRichEdit
            Select
                Case $iCode = $EN_MSGFILTER
                    $tMsgFilter = DllStructCreate($tagEN_MSGFILTER, $iLparam)
                    If DllStructGetData($tMsgFilter, "msg") = $WM_RBUTTONUP Then
                        $hMenu = GUICtrlGetHandle($mnu)
                        SetMenuTexts($hWndFrom, $hMenu)
                        _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd)
                    EndIf
            EndSelect
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func SetMenuTexts($hWnd, $hMenu)
    Local $fState
    If _GUICtrlRichEdit_CanUndo($hWnd) Then
        _GUICtrlMenu_SetItemEnabled($hMenu, $mnuUndo, True, False)
        _GUICtrlMenu_SetItemText($hMenu, $mnuUndo, "Undo: " & _GUICtrlRichEdit_GetNextUndo($hWnd), False)
    Else
        _GUICtrlMenu_SetItemText($hMenu, $mnuUndo, "Undo", False)
        _GUICtrlMenu_SetItemEnabled($hMenu, $mnuUndo, False, False)
    EndIf
    If _GUICtrlRichEdit_CanRedo($hWnd) Then
        _GUICtrlMenu_SetItemEnabled($hMenu, $mnuRedo, True, False)
        _GUICtrlMenu_SetItemText($hMenu, $mnuRedo, "Redo: " & _GUICtrlRichEdit_GetNextRedo($hWnd), False)
    Else
        _GUICtrlMenu_SetItemText($hMenu, $mnuRedo, "Redo", False)
        _GUICtrlMenu_SetItemEnabled($hMenu, $mnuRedo, False, False)
    EndIf
    $fState = _GuiCtrlRichEdit_IsTextSelected($hWnd)
    _GUICtrlMenu_SetItemEnabled($hMenu, $mnuCut, $fState, False)
    _GUICtrlMenu_SetItemEnabled($hMenu, $mnuCopy, $fState, False)

    _GUICtrlMenu_SetItemEnabled($hMenu, $mnuPaste, _GUICtrlRichEdit_CanPaste($hWnd))

    _GUICtrlMenu_SetItemEnabled($hMenu, $mnuPasteSpl, _GUICtrlRichEdit_CanPasteSpecial($hWnd), False)
EndFunc   ;==>SetMenuTexts

Func ReadBmpToRtf($sBmpFilspc)
    Local $hFile, $sRtf
    $hFile = FileOpen($sBmpFilspc, 16)
    If FileRead($hFile, 2) <> "0x424D" Then Return SetError(1, 0, "")
    FileRead($hFile, 12)
    $sRtf = '{\rtf1{\pict\dibitmap ' & Hex(FileRead($hFile)) & '}}'
    FileClose($hFile)
    Return $sRtf
EndFunc   ;==>ReadBmpToRtf

Func FindFirstBMP($sPath = @WindowsDir)
    Local $hFind, $sBmpFilspc
    $hFind = FileFindFirstFile($sPath & "\*.bmp")
    $sBmpFilspc = FileFindNextFile($hFind)
    FileClose($hFind)
    Return $sPath & "\" & $sBmpFilspc
EndFunc   ;==>FindFirstBMP

评分

参与人数 1金钱 +20 贡献 +5 收起 理由
tryhi + 20 + 5

查看全部评分

 楼主| 发表于 2011-1-8 16:21:10 | 显示全部楼层
回复 2# kingfirekkk
原来撤销是按控件来的,那么列表框listbox这样的没有提供撤销的udf的空间该如何撤销呢?
可以根据编辑框的撤销函数自己写udf吗?
发表于 2011-1-8 16:47:44 | 显示全部楼层
回复 3# tuoxiansp

个人能力有限,不知道能否实现这样的功能....
但是:  在我印象里面,ListBox,顾名思义,应该是列表框.....罗列东西用的吧?.....RichEdit才是文本编辑啊,你要List box 的undo功能干嘛?
直接用lRichEdit就以啦......还是说楼主另外还有思路..
 楼主| 发表于 2011-1-8 18:23:51 | 显示全部楼层
回复 4# kingfirekkk
呵呵,我新手。。。    也是刚刚发现列表框没用。。。   然后就找到了富文本控件。。。
我想实现对表格的编辑功能,请问这个控件能实现吗,或者有其他表格编辑的空间?
发表于 2011-1-8 20:00:34 | 显示全部楼层
回复 2# kingfirekkk
貌似实现不了无限撤销,请问能实现无限撤销么?
发表于 2011-1-8 20:22:27 | 显示全部楼层
richedit的储蓄太占用空间了,特别是带有图片的
2-300K图片储蓄时达到3-4M多
发表于 2011-1-14 08:29:10 | 显示全部楼层
回复 6# tryhi


    抱歉,俺不会啊。
发表于 2014-5-13 11:52:51 | 显示全部楼层
雷锋精神传天下!谢谢分享!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 04:19 , Processed in 0.243337 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表