Ticket #2876: _GUICtrlRichEdit_Paste.au3

File _GUICtrlRichEdit_Paste.au3, 4.6 KB (added by mLipok, on Sep 2, 2014 at 2:59:01 AM)
Line 
1#include <GUIConstantsEx.au3>
2#include <GuiMenu.au3>
3#include <GuiRichEdit.au3>
4#include <WindowsConstants.au3>
5
6Global $g_hRichEdit, $g_idMnu, $g_idMnuUndo, $g_idMnuRedo, $g_idMnuCut, $g_idMnuCopy
7Global $g_idMnuPaste, $g_idMnuPasteSpl
8
9Example()
10
11Func Example()
12 Local $hGui, $idMnuPasteSplRTF, $idMnuPasteSplwObjs
13 $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
14 $g_hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
15 BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
16 GUISetState(@SW_SHOW)
17
18 _GUICtrlRichEdit_AppendText($g_hRichEdit, ReadBmpToRtf(FindFirstBMP()))
19
20 GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
21
22 $g_idMnu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
23 $g_idMnuUndo = GUICtrlCreateMenuItem("Undo", $g_idMnu)
24 $g_idMnuRedo = GUICtrlCreateMenuItem("Redo", $g_idMnu)
25 GUICtrlCreateMenuItem("", $g_idMnu)
26 $g_idMnuCut = GUICtrlCreateMenuItem("Cut", $g_idMnu)
27 $g_idMnuCopy = GUICtrlCreateMenuItem("Copy", $g_idMnu)
28 $g_idMnuPaste = GUICtrlCreateMenuItem("Paste", $g_idMnu)
29 $g_idMnuPasteSpl = GUICtrlCreateMenu("Paste Special", $g_idMnu)
30 $idMnuPasteSplRTF = GUICtrlCreateMenuItem("RTF only", $g_idMnuPasteSpl)
31 $idMnuPasteSplwObjs = GUICtrlCreateMenuItem("With objects", $g_idMnuPasteSpl)
32 _GUICtrlRichEdit_SetEventMask($g_hRichEdit, $ENM_MOUSEEVENTS)
33
34 While True
35 Switch GUIGetMsg()
36 Case $GUI_EVENT_CLOSE
37 _GUICtrlRichEdit_Destroy($g_hRichEdit) ; needed unless script crashes
38 ; GUIDelete() ; is OK too
39 Exit
40 Case $g_idMnuUndo
41 _GUICtrlRichEdit_Undo($g_hRichEdit)
42 Case $g_idMnuRedo
43 _GUICtrlRichEdit_Redo($g_hRichEdit)
44 Case $g_idMnuCut
45 _GUICtrlRichEdit_Cut($g_hRichEdit)
46 Case $g_idMnuCopy
47 _GUICtrlRichEdit_Copy($g_hRichEdit)
48 Case $g_idMnuPaste
49 _GUICtrlRichEdit_Paste($g_hRichEdit)
50 Case $idMnuPasteSplRTF
51 _GUICtrlRichEdit_PasteSpecial($g_hRichEdit, False)
52 Case $idMnuPasteSplwObjs
53 _GUICtrlRichEdit_PasteSpecial($g_hRichEdit, True)
54 EndSwitch
55 WEnd
56EndFunc ;==>Example
57
58Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
59 #forceref $iMsg, $wParam
60 Local $hWndFrom, $iCode, $tNMHDR, $tMsgFilter, $hMenu
61 $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
62 $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
63 $iCode = DllStructGetData($tNMHDR, "Code")
64 Switch $hWndFrom
65 Case $g_hRichEdit
66 Select
67 Case $iCode = $EN_MSGFILTER
68 $tMsgFilter = DllStructCreate($tagMSGFILTER, $lParam)
69 If DllStructGetData($tMsgFilter, "msg") = $WM_RBUTTONUP Then
70 $hMenu = GUICtrlGetHandle($g_idMnu)
71 SetMenuTexts($hWndFrom, $hMenu)
72 _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd)
73 EndIf
74 EndSelect
75 EndSwitch
76 Return $GUI_RUNDEFMSG
77EndFunc ;==>WM_NOTIFY
78
79Func SetMenuTexts($hWnd, $hMenu)
80 Local $bState
81 If _GUICtrlRichEdit_CanUndo($hWnd) Then
82 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuUndo, True, False)
83 _GUICtrlMenu_SetItemText($hMenu, $g_idMnuUndo, "Undo: " & _GUICtrlRichEdit_GetNextUndo($hWnd), False)
84 Else
85 _GUICtrlMenu_SetItemText($hMenu, $g_idMnuUndo, "Undo", False)
86 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuUndo, False, False)
87 EndIf
88 If _GUICtrlRichEdit_CanRedo($hWnd) Then
89 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuRedo, True, False)
90 _GUICtrlMenu_SetItemText($hMenu, $g_idMnuRedo, "Redo: " & _GUICtrlRichEdit_GetNextRedo($hWnd), False)
91 Else
92 _GUICtrlMenu_SetItemText($hMenu, $g_idMnuRedo, "Redo", False)
93 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuRedo, False, False)
94 EndIf
95 $bState = _GUICtrlRichEdit_IsTextSelected($hWnd)
96 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuCut, $bState, False)
97 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuCopy, $bState, False)
98
99 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuPaste, _GUICtrlRichEdit_CanPaste($hWnd))
100
101 _GUICtrlMenu_SetItemEnabled($hMenu, $g_idMnuPasteSpl, _GUICtrlRichEdit_CanPasteSpecial($hWnd), False)
102EndFunc ;==>SetMenuTexts
103
104Func ReadBmpToRtf($sBmpFilspc)
105 Local $hFile, $sRtf
106 $hFile = FileOpen($sBmpFilspc, $FO_BINARY)
107 If FileRead($hFile, 2) <> "0x424D" Then Return SetError(1, 0, "")
108 FileRead($hFile, 12)
109 $sRtf = '{\rtf1{\pict\dibitmap ' & Hex(FileRead($hFile)) & '}}'
110 FileClose($hFile)
111 Return $sRtf
112EndFunc ;==>ReadBmpToRtf
113
114Func FindFirstBMP($sPath = Default)
115 If $sPath = Default Then
116 $sPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE" & ((@AutoItX64) ? "\Wow6432Node":"") & "\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\Advanced\Images"
117 EndIf
118 Local $hFind, $sBmpFilspc
119 $hFind = FileFindFirstFile($sPath & "\*.bmp")
120 $sBmpFilspc = FileFindNextFile($hFind)
121 FileClose($hFind)
122 Return $sPath & "\" & $sBmpFilspc
123EndFunc ;==>FindFirstBMP