home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / gradie1r / picedit.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-07-25  |  15.6 KB  |  508 lines

  1. VERSION 5.00
  2. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  4. Begin VB.Form frmPicEdit 
  5.    AutoRedraw      =   -1  'True
  6.    BackColor       =   &H80000004&
  7.    Caption         =   "Overlay text on picture"
  8.    ClientHeight    =   5205
  9.    ClientLeft      =   1515
  10.    ClientTop       =   3030
  11.    ClientWidth     =   5010
  12.    ClipControls    =   0   'False
  13.    BeginProperty Font 
  14.       Name            =   "MS Sans Serif"
  15.       Size            =   8.25
  16.       Charset         =   0
  17.       Weight          =   700
  18.       Underline       =   0   'False
  19.       Italic          =   0   'False
  20.       Strikethrough   =   0   'False
  21.    EndProperty
  22.    Icon            =   "PicEdit.frx":0000
  23.    LinkTopic       =   "Form1"
  24.    LockControls    =   -1  'True
  25.    OLEDropMode     =   1  'Manual
  26.    ScaleHeight     =   5205
  27.    ScaleWidth      =   5010
  28.    Begin MSComDlg.CommonDialog CommonDialog1 
  29.       Left            =   4080
  30.       Top             =   120
  31.       _ExtentX        =   847
  32.       _ExtentY        =   847
  33.       _Version        =   393216
  34.    End
  35.    Begin VB.CommandButton CmdOverlayText 
  36.       Height          =   405
  37.       Left            =   810
  38.       Picture         =   "PicEdit.frx":000C
  39.       Style           =   1  'Graphical
  40.       TabIndex        =   11
  41.       ToolTipText     =   "Proceed overlay"
  42.       Top             =   180
  43.       Visible         =   0   'False
  44.       Width           =   405
  45.    End
  46.    Begin VB.CommandButton cmdTextFont 
  47.       Height          =   405
  48.       Left            =   1440
  49.       Picture         =   "PicEdit.frx":010E
  50.       Style           =   1  'Graphical
  51.       TabIndex        =   1
  52.       ToolTipText     =   "Select text font"
  53.       Top             =   180
  54.       Width           =   405
  55.    End
  56.    Begin VB.CommandButton cmdTextColor 
  57.       Height          =   405
  58.       Left            =   2070
  59.       Picture         =   "PicEdit.frx":0908
  60.       Style           =   1  'Graphical
  61.       TabIndex        =   2
  62.       ToolTipText     =   "Select text color"
  63.       Top             =   180
  64.       Width           =   405
  65.    End
  66.    Begin VB.CommandButton cmdInputText 
  67.       Height          =   405
  68.       Left            =   810
  69.       Picture         =   "PicEdit.frx":0F72
  70.       Style           =   1  'Graphical
  71.       TabIndex        =   3
  72.       ToolTipText     =   "Input text"
  73.       Top             =   180
  74.       Width           =   405
  75.    End
  76.    Begin VB.CommandButton cmdClose 
  77.       Height          =   405
  78.       Left            =   3360
  79.       Picture         =   "PicEdit.frx":1074
  80.       Style           =   1  'Graphical
  81.       TabIndex        =   5
  82.       ToolTipText     =   "Close"
  83.       Top             =   180
  84.       Width           =   405
  85.    End
  86.    Begin VB.CommandButton cmdSave 
  87.       Height          =   405
  88.       Left            =   2730
  89.       Picture         =   "PicEdit.frx":186E
  90.       Style           =   1  'Graphical
  91.       TabIndex        =   4
  92.       ToolTipText     =   "Save"
  93.       Top             =   180
  94.       Width           =   405
  95.    End
  96.    Begin VB.CommandButton cmdOpen 
  97.       Height          =   405
  98.       Left            =   180
  99.       Picture         =   "PicEdit.frx":1ED8
  100.       Style           =   1  'Graphical
  101.       TabIndex        =   0
  102.       ToolTipText     =   "Open graphics file"
  103.       Top             =   180
  104.       Width           =   405
  105.    End
  106.    Begin VB.HScrollBar HScroll1 
  107.       Height          =   345
  108.       Left            =   0
  109.       TabIndex        =   8
  110.       Top             =   6360
  111.       Width           =   10755
  112.    End
  113.    Begin VB.PictureBox PicZ 
  114.       AutoRedraw      =   -1  'True
  115.       BackColor       =   &H80000006&
  116.       Height          =   3135
  117.       Left            =   180
  118.       ScaleHeight     =   205
  119.       ScaleMode       =   3  'Pixel
  120.       ScaleWidth      =   301
  121.       TabIndex        =   6
  122.       Top             =   1800
  123.       Width           =   4575
  124.       Begin RichTextLib.RichTextBox rtbText 
  125.          Height          =   465
  126.          Left            =   120
  127.          TabIndex        =   10
  128.          Top             =   2610
  129.          Visible         =   0   'False
  130.          Width           =   1815
  131.          _ExtentX        =   3201
  132.          _ExtentY        =   820
  133.          _Version        =   393217
  134.          BackColor       =   16777215
  135.          BorderStyle     =   0
  136.          Enabled         =   -1  'True
  137.          HideSelection   =   0   'False
  138.          Appearance      =   0
  139.          OLEDragMode     =   0
  140.          OLEDropMode     =   0
  141.          TextRTF         =   $"PicEdit.frx":1FDA
  142.       End
  143.       Begin VB.PictureBox PicX 
  144.          AutoRedraw      =   -1  'True
  145.          BorderStyle     =   0  'None
  146.          Height          =   2085
  147.          Left            =   0
  148.          ScaleHeight     =   2085
  149.          ScaleWidth      =   4035
  150.          TabIndex        =   9
  151.          Top             =   0
  152.          Width           =   4035
  153.       End
  154.       Begin VB.PictureBox PicY 
  155.          AutoRedraw      =   -1  'True
  156.          BackColor       =   &H8000000E&
  157.          BorderStyle     =   0  'None
  158.          Height          =   2580
  159.          Left            =   0
  160.          ScaleHeight     =   2580
  161.          ScaleWidth      =   4500
  162.          TabIndex        =   7
  163.          Top             =   0
  164.          Visible         =   0   'False
  165.          Width           =   4500
  166.       End
  167.    End
  168.    Begin VB.Label Label1 
  169.       Caption         =   "Label1"
  170.       Height          =   855
  171.       Left            =   180
  172.       TabIndex        =   12
  173.       Top             =   750
  174.       Width           =   4545
  175.    End
  176. Attribute VB_Name = "frmPicEdit"
  177. Attribute VB_GlobalNameSpace = False
  178. Attribute VB_Creatable = False
  179. Attribute VB_PredeclaredId = True
  180. Attribute VB_Exposed = False
  181. ' PicEdit.frm
  182. ' By Herman Liu
  183. ' To show how one can place rich text on picture, in a simple way. (VB seperates
  184. ' rich text and picture as distinctly different types of format, and does not provide
  185. ' functions to allow superimposing the former on the latter).
  186. Option Explicit
  187. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  188.      (ByVal hwnd As Long, ByVal msg As Long, ByVal wp As Long, Ip As Any) As Long
  189.      
  190. Private Type Rect
  191.     Left As Long
  192.     Top As Long
  193.     Right As Long
  194.     Bottom As Long
  195. End Type
  196. Private Type CharRange
  197.     firstChar As Long
  198.     lastChar As Long
  199. End Type
  200. Private Type FormatRange
  201.     hdc As Long
  202.     hdcTarget As Long
  203.     rectRegion As Rect
  204.     rectPage As Rect
  205.     mCharRange As CharRange
  206. End Type
  207. Private Const WM_USER As Long = &H400
  208. Private Const EM_FORMATRANGE As Long = WM_USER + 57
  209. Dim mFormatRange As FormatRange
  210. Dim rectDrawTo As Rect, rectPage As Rect
  211. Dim TextLength As Long, newStartPos As Long
  212. Dim dumpaway As Long
  213. Dim X1 As Single, Y1 As Single, X2 As Single, Y2 As Single
  214. Dim NoPicFlag As Boolean, RegionFlag As Boolean
  215. Dim fso As FileSystemObject
  216. Private Sub Form_Load()
  217.     Me.ScaleMode = vbTwips
  218.     PicX.ScaleMode = vbTwips
  219.     PicY.ScaleMode = vbTwips
  220.     PicZ.ScaleMode = vbPixels
  221.     PicZ.AutoSize = True
  222.     PicX.AutoSize = True
  223.     PicY.AutoSize = True
  224.     PicZ.AutoRedraw = True
  225.     PicX.AutoRedraw = True
  226.     PicY.AutoRedraw = True
  227.     PicZ.Visible = True
  228.     PicX.Visible = True
  229.     PicY.Visible = False
  230.     PicZ.BorderStyle = 1
  231.     PicX.BorderStyle = 0
  232.     PicY.BorderStyle = 0
  233.     PicZ.BackColor = &H80000006
  234.     PicY.Top = PicX.Top
  235.     PicY.Left = PicX.Left
  236.     X1 = 0: Y1 = 0: X2 = 0: Y2 = 0
  237.     CmdOverlayText.Visible = False
  238.     Set fso = New FileSystemObject
  239.     rtbText.Visible = False
  240.     If fso.FileExists("\windows\clouds.bmp") Then
  241.          PicX.Picture = LoadPicture("\windows\clouds.bmp", vbCFBitmap)
  242.          NoPicFlag = False
  243.     Else
  244.          NoPicFlag = True
  245.     End If
  246.     PicY.Width = PicX.Width
  247.     PicY.Height = PicX.Height
  248.     PicY.Picture = PicX.Picture
  249.     PicY.Move PicX.Top, PicX.Left
  250.     Dim t
  251.     t = "Steps: 1. Drag left-mouse on picture to frame a rectangle area."
  252.     t = t & " 2. Click the second button to allow input of text in that"
  253.     t = t & " area. 3. Type in text (you may select font and color)."
  254.     t = t & " 4. Click the second button again."
  255.     Label1.Caption = t
  256.     RegionFlag = False
  257. End Sub
  258. Private Sub cmdInputText_Click()
  259.     On Error GoTo errhandler
  260.     RegionFlag = False
  261.     If X2 - X1 <= 100 Or Y2 - Y1 <= 100 Then
  262.          MsgBox "No text input region yet"
  263.          Exit Sub
  264.     End If
  265.     If CmdOverlayText.Visible = True Then
  266.          If Len(rtbText.Text) = 0 Then
  267.              MsgBox "No text input yet"
  268.              Exit Sub
  269.          End If
  270.          TextToPic
  271.          Exit Sub
  272.     End If
  273.     If Clipboard.GetFormat(vbCFText) = True Then
  274.          rtbText.Text = Clipboard.GetText
  275.     Else
  276.          rtbText.Text = "Type text here"
  277.     End If
  278.         
  279.     ValidateDraw
  280.     rtbText.Width = (X2 - X1) / Screen.TwipsPerPixelX + 2
  281.     rtbText.Height = (Y2 - Y1) / Screen.TwipsPerPixelY + 2
  282.     If rtbText.SelColor = vbWhite Then
  283.         rtbText.BackColor = vbBlue
  284.     Else
  285.         rtbText.BackColor = vbWhite
  286.     End If
  287.     cmdInputText.Visible = False
  288.     CmdOverlayText.Visible = True
  289.     rtbText.Visible = True
  290.     rtbText.Enabled = True
  291.     rtbText.Move (X1 / Screen.TwipsPerPixelX), (Y1 / Screen.TwipsPerPixelY)
  292.     rtbText.SetFocus
  293.     cmdInputText.Visible = False
  294.     CmdOverlayText.Visible = True
  295.     Exit Sub
  296. errhandler:
  297.     ErrMsgProc "mnuFileOverlayText_Click"
  298. End Sub
  299. Private Sub TextToPic()
  300.     RegionFlag = False
  301.     If Len(rtbText.Text) = 0 Then
  302.         Exit Sub
  303.     End If
  304.     rtbText.Visible = False
  305.     rtbText.Enabled = False
  306.     Overlaying
  307.     PicY.Picture = PicY.Image
  308.     PicX.Picture = PicY.Picture
  309.     rtbText.Text = ""
  310.     CmdOverlayText.Visible = False
  311.     cmdInputText.Visible = True
  312. End Sub
  313. Private Sub Overlaying()
  314.     On Error GoTo errhandler
  315.     DoEvents
  316.     Screen.MousePointer = vbHourglass
  317.     rectPage.Left = X1
  318.     rectPage.Top = Y1
  319.     rectPage.Right = X2
  320.     rectPage.Bottom = Y2
  321.     rectDrawTo.Left = rectPage.Left
  322.     rectDrawTo.Top = rectPage.Top
  323.     rectDrawTo.Right = rectPage.Right
  324.     rectDrawTo.Bottom = rectPage.Bottom
  325.     mFormatRange.hdc = PicY.hdc
  326.     mFormatRange.hdcTarget = PicY.hdc
  327.     newStartPos = 0
  328.     mFormatRange.rectRegion = rectDrawTo
  329.     mFormatRange.rectPage = rectPage
  330.     mFormatRange.mCharRange.firstChar = newStartPos
  331.     mFormatRange.mCharRange.lastChar = -1
  332.     TextLength = Len(rtbText.Text)
  333.     Do
  334.         newStartPos = SendMessage(rtbText.hwnd, EM_FORMATRANGE, True, mFormatRange)
  335.         If newStartPos >= TextLength Then
  336.             Exit Do
  337.         End If
  338.         mFormatRange.mCharRange.firstChar = newStartPos
  339.         mFormatRange.hdc = PicY.hdc
  340.         mFormatRange.hdcTarget = PicY.hdc
  341.         DoEvents
  342.     Loop
  343.     dumpaway = SendMessage(rtbText.hwnd, EM_FORMATRANGE, False, ByVal CLng(0))
  344.     Screen.MousePointer = vbDefault
  345.     Exit Sub
  346. errhandler:
  347.     Screen.MousePointer = vbDefault
  348.     ErrMsgProc "Overlaying"
  349. End Sub
  350. Private Sub Form_Unload(Cancel As Integer)
  351.      End
  352. End Sub
  353. Private Sub picx_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  354.     If Button = vbLeftButton Then
  355.          rtbText.Visible = False
  356.          CmdOverlayText.Visible = False
  357.          cmdInputText.Visible = True
  358.          RegionFlag = True
  359.          PicX.DrawMode = vbInvert
  360.          X1 = X: X2 = X: Y1 = Y: Y2 = Y
  361.          PicX.Cls
  362.          PicX.Line (X, Y)-(X, Y), , B
  363.     End If
  364. End Sub
  365. Private Sub picX_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  366.     If Not RegionFlag Then
  367.          Exit Sub
  368.     End If
  369.     PicX.Line (X1, Y1)-(X2, Y2), , B
  370.     X2 = X
  371.     Y2 = Y
  372.     PicX.Line (X1, Y1)-(X2, Y2), , B
  373. End Sub
  374. Private Sub picX_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  375.     If Not RegionFlag Then
  376.         Exit Sub
  377.     Else
  378.         RegionFlag = False
  379.         PicX.DrawMode = vbCopyPen
  380.     End If
  381. End Sub
  382. Private Sub ValidateDraw()
  383.     Dim tmp As Single
  384.     If X1 > X2 Then
  385.         tmp = X1
  386.         X1 = X2
  387.         X2 = tmp
  388.     End If
  389.     If Y1 > Y2 Then
  390.         tmp = Y1
  391.         Y1 = Y2
  392.         Y2 = tmp
  393.     End If
  394. End Sub
  395. Private Sub cmdOpen_Click()
  396.     On Error GoTo errhandler
  397.     Dim mfilespec As String
  398.     CommonDialog1.Flags = cdlOFNHideReadOnly
  399.     CommonDialog1.FileName = ""
  400.     CommonDialog1.Filter = ""
  401.     CommonDialog1.CancelError = True
  402. FileNameRetry:
  403.     CommonDialog1.ShowOpen
  404.     If CommonDialog1.FileName = "" Then
  405.         Exit Sub
  406.     End If
  407.         
  408.     If Not fso.FileExists(CommonDialog1.FileName) Then
  409.         GoTo FileNameRetry
  410.     End If
  411.     mfilespec = CommonDialog1.FileName
  412.     Screen.MousePointer = vbHourglass
  413.     PicX.Cls
  414.     PicY.Cls
  415.     PicX.AutoSize = True
  416.     PicY.AutoSize = True
  417.     PicX.Picture = LoadPicture(mfilespec)
  418.     PicY.Picture = PicX.Picture
  419.     PicX.AutoSize = False
  420.     PicY.AutoSize = False
  421.     PicY.Move PicX.Top, PicX.Left
  422.     rtbText.SelColor = vbBlack
  423.     rtbText.BackColor = vbWhite
  424.     NoPicFlag = False
  425.     Screen.MousePointer = vbDefault
  426.     Exit Sub
  427. errhandler:
  428.     PicX.AutoSize = False
  429.     PicY.AutoSize = False
  430.     Screen.MousePointer = vbDefault
  431.     If Err <> 32755 Then
  432.          ErrMsgProc "frmPicEdit LoadPicFile"
  433.     End If
  434. End Sub
  435. Private Sub cmdTextFont_Click()
  436.     On Error GoTo errhandler
  437.     CommonDialog1.CancelError = True
  438.     CommonDialog1.Flags = cdlCFBoth
  439.     CommonDialog1.FontName = Screen.ActiveForm.FontName
  440.     CommonDialog1.ShowFont
  441.     rtbText.SelStart = 0
  442.     rtbText.SelLength = Len(rtbText.Text)
  443.     rtbText.SelFontName = CommonDialog1.FontName
  444.     rtbText.SelFontSize = CommonDialog1.FontSize
  445.     Exit Sub
  446. errhandler:
  447.     If Err.Number <> 32755 Then
  448.         ErrMsgProc "mnuFileTextFont_click"
  449.     End If
  450. End Sub
  451. Private Sub cmdTextColor_Click()
  452.     On Error GoTo errhandler
  453.     CommonDialog1.CancelError = True
  454.     CommonDialog1.Flags = cdlCFBoth
  455.     CommonDialog1.Color = Screen.ActiveForm.ForeColor
  456.     CommonDialog1.ShowColor
  457.     rtbText.SelStart = 0
  458.     rtbText.SelLength = Len(rtbText.Text)
  459.     rtbText.SelColor = CommonDialog1.Color
  460.     If rtbText.SelColor = vbWhite Then
  461.         rtbText.BackColor = vbBlue
  462.     Else
  463.         rtbText.BackColor = vbWhite
  464.     End If
  465.     Exit Sub
  466. errhandler:
  467.     If Err.Number <> 32755 Then
  468.         ErrMsgProc "mnuFileTextColor"
  469.     End If
  470. End Sub
  471. Private Sub cmdOverlayText_Click()
  472.     cmdInputText_Click
  473. End Sub
  474. Private Sub cmdSave_Click()
  475.     If NoPicFlag Then
  476.          MsgBox "No picture loaded yet"
  477.          Exit Sub
  478.     End If
  479.     On Error GoTo errhandler
  480.     Dim mfilespec As String
  481.     With CommonDialog1
  482.         .FileName = mfilespec
  483.         .Flags = cdlOFNHideReadOnly
  484.         .ShowSave
  485.     End With
  486.     mfilespec = CommonDialog1.FileName
  487.     If fso.FileExists(mfilespec) Then
  488.          If MsgBox("File already exists.  Overwirte?", vbYesNo + vbQuestion) = vbNo Then
  489.               Exit Sub
  490.          End If
  491.     End If
  492.     Screen.MousePointer = vbHourglass
  493.     SavePicture PicX.Picture, mfilespec
  494.     Screen.MousePointer = vbDefault
  495.     Exit Sub
  496. errhandler:
  497.     Screen.MousePointer = vbDefault
  498.     If Err <> 32755 Then
  499.          ErrMsgProc "frmPicEdit mnuFileSave_Click"
  500.     End If
  501. End Sub
  502. Private Sub cmdClose_Click()
  503.     End
  504. End Sub
  505. Sub ErrMsgProc(mMsg As String)
  506.     MsgBox mMsg & vbCrLf & Err.Number & Space(5) & Err.Description
  507. End Sub
  508.