home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / controls / editundo.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1993-05-16  |  14.6 KB  |  433 lines

  1. VERSION 2.00
  2. Begin Form UndoMe 
  3.    Caption         =   "Undo Me"
  4.    ClientHeight    =   3510
  5.    ClientLeft      =   1455
  6.    ClientTop       =   2310
  7.    ClientWidth     =   6210
  8.    Height          =   4200
  9.    Icon            =   EDITUNDO.FRX:0000
  10.    Left            =   1395
  11.    LinkMode        =   1  'Source
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   3510
  14.    ScaleWidth      =   6210
  15.    Top             =   1680
  16.    Width           =   6330
  17.    Begin TextBox Text1 
  18.       Height          =   1455
  19.       Left            =   240
  20.       MultiLine       =   -1  'True
  21.       ScrollBars      =   3  'Both
  22.       TabIndex        =   0
  23.       Text            =   "Hello World"
  24.       Top             =   120
  25.       Width           =   3015
  26.    End
  27.    Begin TextBox Text2 
  28.       Height          =   1455
  29.       Left            =   3240
  30.       MultiLine       =   -1  'True
  31.       ScrollBars      =   3  'Both
  32.       TabIndex        =   1
  33.       Text            =   "Goodbye World"
  34.       Top             =   120
  35.       Width           =   2895
  36.    End
  37.    Begin PictureBox Picture1 
  38.       AutoRedraw      =   -1  'True
  39.       Height          =   1575
  40.       Left            =   120
  41.       Picture         =   EDITUNDO.FRX:0302
  42.       ScaleHeight     =   1545
  43.       ScaleWidth      =   4065
  44.       TabIndex        =   2
  45.       Top             =   1680
  46.       Width           =   4095
  47.    End
  48.    Begin ComboBox Combo1 
  49.       Height          =   1500
  50.       Left            =   4320
  51.       Style           =   1  'Simple Combo
  52.       TabIndex        =   4
  53.       Text            =   "Combo1"
  54.       Top             =   1680
  55.       Width           =   1815
  56.    End
  57.    Begin PictureBox TrashPic 
  58.       Height          =   375
  59.       Left            =   120
  60.       ScaleHeight     =   345
  61.       ScaleWidth      =   1065
  62.       TabIndex        =   3
  63.       Top             =   3000
  64.       Visible         =   0   'False
  65.       Width           =   1095
  66.    End
  67.    Begin Menu mBar 
  68.       Caption         =   "&File"
  69.       Index           =   0
  70.       Begin Menu mFile 
  71.          Caption         =   "&New"
  72.          Enabled         =   0   'False
  73.          Index           =   0
  74.       End
  75.       Begin Menu mFile 
  76.          Caption         =   "&Open..."
  77.          Enabled         =   0   'False
  78.          Index           =   1
  79.       End
  80.       Begin Menu mFile 
  81.          Caption         =   "&Close"
  82.          Enabled         =   0   'False
  83.          Index           =   2
  84.       End
  85.       Begin Menu mFile 
  86.          Caption         =   "&Save"
  87.          Enabled         =   0   'False
  88.          Index           =   3
  89.       End
  90.       Begin Menu mFile 
  91.          Caption         =   "Save &As..."
  92.          Enabled         =   0   'False
  93.          Index           =   4
  94.       End
  95.       Begin Menu mFile 
  96.          Index           =   5
  97.       End
  98.       Begin Menu mFile 
  99.          Caption         =   "&Print"
  100.          Enabled         =   0   'False
  101.          Index           =   6
  102.       End
  103.       Begin Menu mFile 
  104.          Caption         =   "P&rinter Setup"
  105.          Enabled         =   0   'False
  106.          Index           =   7
  107.       End
  108.       Begin Menu mFile 
  109.          Index           =   8
  110.       End
  111.       Begin Menu mFile 
  112.          Caption         =   "E&xit"
  113.          Index           =   9
  114.       End
  115.    End
  116.    Begin Menu mBar 
  117.       Caption         =   "&Edit"
  118.       Index           =   1
  119.       Begin Menu mEdit 
  120.          Caption         =   "&Undo           Alt+Backspace"
  121.          Enabled         =   0   'False
  122.          Index           =   0
  123.       End
  124.       Begin Menu mEdit 
  125.          Index           =   1
  126.       End
  127.       Begin Menu mEdit 
  128.          Caption         =   "Cu&t              Shift+Del"
  129.          Enabled         =   0   'False
  130.          Index           =   2
  131.       End
  132.       Begin Menu mEdit 
  133.          Caption         =   "&Copy           Ctrl+Ins"
  134.          Index           =   3
  135.       End
  136.       Begin Menu mEdit 
  137.          Caption         =   "&Paste          Shift+Ins"
  138.          Index           =   4
  139.       End
  140.       Begin Menu mEdit 
  141.          Caption         =   "Paste &Link"
  142.          Enabled         =   0   'False
  143.          Index           =   5
  144.       End
  145.       Begin Menu mEdit 
  146.          Caption         =   "&Delete         Del"
  147.          Index           =   6
  148.       End
  149.       Begin Menu mEdit 
  150.          Caption         =   "Select &All"
  151.          Index           =   7
  152.       End
  153.       Begin Menu mEdit 
  154.          Index           =   8
  155.       End
  156.       Begin Menu mEdit 
  157.          Caption         =   "Clea&r"
  158.          Index           =   9
  159.       End
  160.    End
  161.    Begin Menu mBar 
  162.       Caption         =   "&Help"
  163.       Index           =   2
  164.       Begin Menu mHelp 
  165.          Caption         =   "&Index"
  166.          Enabled         =   0   'False
  167.          Index           =   0
  168.       End
  169.       Begin Menu mHelp 
  170.          Caption         =   "&Keyboard"
  171.          Enabled         =   0   'False
  172.          Index           =   1
  173.       End
  174.       Begin Menu mHelp 
  175.          Caption         =   "Using &Help"
  176.          Enabled         =   0   'False
  177.          Index           =   2
  178.       End
  179.       Begin Menu mHelp 
  180.          Caption         =   "&About..."
  181.          Index           =   3
  182.       End
  183.    End
  184. Option Explicit
  185. Declare Function SendMessage Lib "User" (ByVal hwnd%, ByVal msg%, ByVal wp%, lp As Any) As Integer
  186. Declare Function GetFocus Lib "User" () As Integer
  187. ' Windows Messages
  188. Const WM_USER = &H400
  189. Const EM_CANUNDO = WM_USER + 22
  190. Const EM_UNDO = WM_USER + 23
  191. Const WM_CUT = &H300
  192. Const WM_COPY = &H301
  193. Const WM_PASTE = &H302
  194. Const WM_UNDO = &H304
  195. ' Clipboard Formats
  196. Const CF_TEXT = 1
  197. Const CF_BITMAP = 2
  198. Const CF_METAFILE = 3
  199. Const CF_DIB = 8
  200. Const CF_LINK = &HBF00
  201. ' Menu Item Control Array Indexes
  202. Const NEW_INDEX = 0
  203. Const OPEN_INDEX = 1
  204. Const CLOSE_INDEX = 2
  205. Const SAVE_INDEX = 3
  206. Const SAVEAS_INDEX = 4
  207. Const PRINT_INDEX = 6
  208. Const PRINTERSETUP_INDEX = 7
  209. Const EXIT_INDEX = 9
  210. Const UNDO_INDEX = 0
  211. Const CUT_INDEX = 2
  212. Const COPY_INDEX = 3
  213. Const PASTE_INDEX = 4
  214. Const PASTELINK_INDEX = 5
  215. Const DEL_INDEX = 6
  216. Const SELECTALL_INDEX = 7
  217. Const CLEAR_INDEX = 9
  218. Const INDEX_INDEX = 0
  219. Const KB_INDEX = 1
  220. Const USING_INDEX = 2
  221. Const ABOUT_INDEX = 3
  222. Sub CopyPic (Pic As Control)
  223.     On Error Resume Next
  224.     Clipboard.Clear
  225.     Clipboard.SetData Pic.Picture, CF_METAFILE
  226.     Clipboard.SetData Pic.Picture, CF_DIB
  227.     Clipboard.SetData Pic.Picture, CF_BITMAP
  228.     Exit Sub
  229. End Sub
  230. Sub Form_Load ()
  231.     Dim i As Integer
  232.     For i = 65 To 90
  233.     combo1.AddItem String$(12, i)
  234.     Next i
  235.     UndoMe.Visible = False
  236.     UndoMe.Height = 3500
  237.     UndoMe.Width = 5000
  238.     UndoMe.Visible = True
  239. End Sub
  240. Sub Form_Resize ()
  241.     Const borderWidth = 100
  242.     Const VertPercent = .5
  243.     Const HorzPercent = .7
  244.     text1.Move UndoMe.ScaleLeft + borderWidth, UndoMe.ScaleTop + borderWidth
  245.     text1.Height = (UndoMe.ScaleHeight * VertPercent) - (borderWidth * 2)
  246.     text1.Width = ((UndoMe.ScaleWidth) / 2) - (borderWidth * 2)
  247.     text2.Move ((UndoMe.ScaleWidth) / 2) + borderWidth, UndoMe.ScaleTop + borderWidth
  248.     text2.Height = (UndoMe.ScaleHeight * VertPercent) - (borderWidth * 2)
  249.     text2.Width = ((UndoMe.ScaleWidth) / 2) - (borderWidth * 2)
  250.     Picture1.Move UndoMe.ScaleLeft + borderWidth, (UndoMe.ScaleTop + (UndoMe.ScaleHeight * VertPercent)) + borderWidth
  251.     Picture1.Height = (UndoMe.ScaleHeight * (1 - VertPercent)) - (borderWidth * 2)
  252.     Picture1.Width = ((UndoMe.ScaleWidth) * HorzPercent) - (borderWidth * 2)
  253.     combo1.Move ((UndoMe.ScaleWidth) * HorzPercent) + borderWidth, (UndoMe.ScaleTop + (UndoMe.ScaleHeight * (1 - VertPercent))) + borderWidth
  254.     combo1.Height = (UndoMe.ScaleHeight * (1 - VertPercent)) - (borderWidth * 2)
  255.     combo1.Width = ((UndoMe.ScaleWidth) * (1 - HorzPercent)) - (borderWidth * 2)
  256. End Sub
  257. Sub mBar_Click (Index As Integer)
  258.     ' This handles the main menu bar item functions
  259.     ' In reality, the only main menu items which needs
  260.     ' attention is the Edit menu so we can en/disable the
  261.     ' Edit menu items appropriately.
  262.     If Index = 1 Then ' Edit Menu
  263.     'Enable / disable Undo
  264.     If TypeOf Screen.ActiveControl Is TextBox Then
  265.         mEdit(UNDO_INDEX).Enabled = SendMessage(GetFocus(), EM_CANUNDO, 0, ByVal 0&)
  266.     ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  267.         mEdit(UNDO_INDEX).Enabled = (Val(TrashPic.Tag) = GetFocus())
  268.     Else
  269.         mEdit(UNDO_INDEX).Enabled = False
  270.         'No way to find out if a combo box can be undone.
  271.         'Can use EM_CANUNDO in Win 3.0, but that RIPs in Win 3.1
  272.     End If
  273.     'As a pretty feature, change Undo to can't undo if we can't
  274.     If Not mEdit(UNDO_INDEX).Enabled Then
  275.         mEdit(UNDO_INDEX).Caption = "Can't Undo"
  276.     Else
  277.         mEdit(UNDO_INDEX).Caption = "&Undo           Alt+Backspace"
  278.     End If
  279.     'Enable / disable Cut & Copy & SelectAll & Del
  280.     If TypeOf Screen.ActiveControl Is TextBox Then
  281.         mEdit(CUT_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  282.         mEdit(COPY_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  283.         mEdit(SELECTALL_INDEX).Enabled = Len(Screen.ActiveControl.Text)
  284.         mEdit(DEL_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  285.     ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  286.         mEdit(CUT_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  287.         mEdit(COPY_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  288.         mEdit(SELECTALL_INDEX).Enabled = False
  289.         mEdit(DEL_INDEX).Enabled = (Screen.ActiveControl.SelText <> "")
  290.     ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  291.         mEdit(CUT_INDEX).Enabled = True
  292.         mEdit(COPY_INDEX).Enabled = True
  293.         mEdit(SELECTALL_INDEX).Enabled = False
  294.         mEdit(DEL_INDEX).Enabled = False
  295.     Else
  296.         mEdit(CUT_INDEX).Enabled = False
  297.         mEdit(COPY_INDEX).Enabled = False
  298.         mEdit(SELECTALL_INDEX).Enabled = False
  299.         mEdit(DEL_INDEX).Enabled = False
  300.     End If
  301.     'Enable / disable Paste
  302.     If TypeOf Screen.ActiveControl Is TextBox Then
  303.         mEdit(PASTE_INDEX).Enabled = Clipboard.GetFormat(CF_TEXT)
  304.     ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  305.         mEdit(PASTE_INDEX).Enabled = Clipboard.GetFormat(CF_TEXT)
  306.     ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  307.         mEdit(PASTE_INDEX).Enabled = Clipboard.GetFormat(CF_DIB) Or Clipboard.GetFormat(CF_BITMAP) Or Clipboard.GetFormat(CF_METAFILE)
  308.     Else
  309.         mEdit(PASTE_INDEX).Enabled = False
  310.     End If
  311.     'Enable / disable Paste Link
  312.     If TypeOf Screen.ActiveControl Is TextBox Then
  313.         mEdit(PASTELINK_INDEX).Enabled = Clipboard.GetFormat(CF_LINK)
  314.     ElseIf TypeOf Screen.ActiveControl Is Label Then
  315.         mEdit(PASTELINK_INDEX).Enabled = Clipboard.GetFormat(CF_LINK)
  316.     ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  317.         mEdit(PASTELINK_INDEX).Enabled = Clipboard.GetFormat(CF_LINK)
  318.     Else
  319.         mEdit(PASTELINK_INDEX).Enabled = False
  320.     End If
  321.     'Enable / disable Clear
  322.     mEdit(CLEAR_INDEX).Enabled = Clipboard.GetFormat(CF_LINK) Or Clipboard.GetFormat(CF_TEXT) Or Clipboard.GetFormat(CF_DIB) Or Clipboard.GetFormat(CF_BITMAP) Or Clipboard.GetFormat(CF_METAFILE)
  323.     End If
  324. End Sub
  325. Sub mEdit_Click (Index As Integer)
  326.     Dim rc As Integer, pos As Integer
  327.     Dim pastelink As String
  328.     'This handles all the Edit Menu Items...
  329.     Select Case Index
  330.     Case UNDO_INDEX
  331.         If TypeOf Screen.ActiveControl Is TextBox Then
  332.         rc = SendMessage(GetFocus(), WM_UNDO, 0, ByVal 0&)
  333.         ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  334.         rc = SendMessage(GetFocus(), WM_UNDO, 0, ByVal 0&)
  335.         ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  336.         Screen.ActiveControl.Picture = TrashPic.Picture
  337.         TrashPic.Picture = LoadPicture()
  338.         TrashPic.Tag = ""
  339.         End If
  340.     Case CUT_INDEX, COPY_INDEX
  341.         If TypeOf Screen.ActiveControl Is TextBox Then
  342.         If Index = CUT_INDEX Then
  343.              rc = SendMessage(GetFocus(), WM_CUT, 0, ByVal 0&)
  344.         Else ' must be Copy
  345.              rc = SendMessage(GetFocus(), WM_COPY, 0, ByVal 0&)
  346.         End If
  347.         ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  348.         If Index = CUT_INDEX Then
  349.             rc = SendMessage(GetFocus(), WM_CUT, 0, ByVal 0&)
  350.         Else
  351.             rc = SendMessage(GetFocus(), WM_COPY, 0, ByVal 0&)
  352.         End If
  353.         ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  354.         CopyPic Screen.ActiveControl
  355.         If Index = CUT_INDEX Then
  356.             TrashPic.Picture = Screen.ActiveControl.Picture
  357.             TrashPic.Tag = Str$(GetFocus())
  358.             Screen.ActiveControl.Picture = LoadPicture()
  359.         End If
  360.         End If
  361.     Case PASTE_INDEX
  362.         If TypeOf Screen.ActiveControl Is TextBox Then
  363.         rc = SendMessage(GetFocus(), WM_PASTE, 0, ByVal 0&)
  364.         ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  365.         rc = SendMessage(GetFocus(), WM_PASTE, 0, ByVal 0&)
  366.         ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  367.         TrashPic.Picture = Screen.ActiveControl.Picture
  368.         TrashPic.Tag = Str$(GetFocus())
  369.         PastePic Screen.ActiveControl
  370.         End If
  371.     Case PASTELINK_INDEX
  372.         pastelink = Clipboard.GetText(&HBF00)
  373.         pos = InStr(pastelink, "!")
  374.         
  375.         If pos > 0 Then
  376.         Screen.ActiveControl.LinkMode = 0
  377.         Screen.ActiveControl.LinkTopic = Left$(pastelink, pos - 1)
  378.         Screen.ActiveControl.LinkItem = Mid$(pastelink, pos + 1)
  379.         Screen.ActiveControl.LinkMode = 1
  380.         Else
  381.         Beep
  382.         End If
  383.     Case DEL_INDEX
  384.         If TypeOf Screen.ActiveControl Is TextBox Then
  385.         Screen.ActiveControl.SelText = ""
  386.         ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
  387.         Screen.ActiveControl.SelText = ""
  388.         ElseIf TypeOf Screen.ActiveControl Is PictureBox Then
  389.         Screen.ActiveControl.Picture = LoadPicture()
  390.         End If
  391.         
  392.     Case SELECTALL_INDEX
  393.         If TypeOf Screen.ActiveControl Is TextBox Then
  394.         Screen.ActiveControl.SelStart = 0
  395.         Screen.ActiveControl.SelLength = Len(Screen.ActiveControl.Text)
  396.         End If
  397.     Case CLEAR_INDEX
  398.         Clipboard.Clear
  399.         
  400.     End Select
  401. End Sub
  402. Sub mFile_Click (Index As Integer)
  403.     Select Case Index
  404.     Case NEW_INDEX
  405.     Case OPEN_INDEX
  406.     Case CLOSE_INDEX
  407.     Case SAVE_INDEX
  408.     Case SAVEAS_INDEX
  409.     Case PRINT_INDEX
  410.     Case PRINTERSETUP_INDEX
  411.     Case EXIT_INDEX
  412.         End
  413.     End Select
  414. End Sub
  415. Sub mHelp_Click (Index As Integer)
  416.     Select Case Index
  417.     Case INDEX_INDEX
  418.     Case KB_INDEX
  419.     Case USING_INDEX
  420.     Case ABOUT_INDEX
  421.         MsgBox "Cut and Paste with Undo Demo" + Chr$(13) + "                Version 0.3" + Chr$(13) + "             By: Jeff Littrell", 64, "About"
  422.     End Select
  423. End Sub
  424. Sub PastePic (Pic As Control)
  425.     If Clipboard.GetFormat(CF_METAFILE) Then
  426.     Pic.Picture = Clipboard.GetData(CF_METAFILE)
  427.     ElseIf Clipboard.GetFormat(CF_DIB) Then
  428.     Pic.Picture = Clipboard.GetData(CF_DIB)
  429.     ElseIf Clipboard.GetFormat(CF_BITMAP) Then
  430.     Pic.Picture = Clipboard.GetData(CF_BITMAP)
  431.     End If
  432. End Sub
  433.