Results 1 to 11 of 11

Thread: Code for "Copy, Paste" and Font for a New Message

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    58

    Question Code for "Copy, Paste" and Font for a New Message

    I am designing a form for New Message similar to Outlook Express.For commands like "Copy","Paste","Cut" how to code for these? And also when I click Font in the menu that standard Font window having "Font", "Style", "Size" should be displayed.So how to code for this?
    Any help will be grately appreciated. Thanks.

  2. #2
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    Hello Gopi,

    To copy or cut text to the Clipboard use this code. Suppose there is a textbox Text1 on ur form. And Cut, Copy & Paste on ur menu then the code is

    VB Code:
    1. Private Sub mnuEditPaste_Click()
    2.     On Error Resume Next
    3.     Text1.text = Clipboard.GetText
    4. End Sub
    5.  
    6. Private Sub mnuEditCopy_Click()
    7.     On Error Resume Next
    8.     Clipboard.SetText Text1.text
    9. End Sub
    10.  
    11. Private Sub mnuEditCut_Click()
    12.     On Error Resume Next
    13.     Clipboard.SetText Text1.text
    14. End Sub

    To show up the Choose Font dialog box, u can use a Common Dialog Control but its better if u use API code. Put a command button Commnad1 on ur form and insert this code.

    VB Code:
    1. Option Explicit
    2. Const FW_NORMAL = 400
    3. Const DEFAULT_CHARSET = 1
    4. Const OUT_DEFAULT_PRECIS = 0
    5. Const CLIP_DEFAULT_PRECIS = 0
    6. Const DEFAULT_QUALITY = 0
    7. Const DEFAULT_PITCH = 0
    8. Const FF_ROMAN = 16
    9.  
    10. Const CF_PRINTERFONTS = &H2
    11. Const CF_SCREENFONTS = &H1
    12. Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
    13. Const CF_EFFECTS = &H100&
    14. Const CF_FORCEFONTEXIST = &H10000
    15. Const CF_INITTOLOGFONTSTRUCT = &H40&
    16. Const CF_LIMITSIZE = &H2000&
    17. Const REGULAR_FONTTYPE = &H400
    18. Const LF_FACESIZE = 32
    19. Const GMEM_MOVEABLE = &H2
    20. Const GMEM_ZEROINIT = &H40
    21.  
    22.  
    23. Private Type LOGFONT
    24.         lfHeight As Long
    25.         lfWidth As Long
    26.         lfEscapement As Long
    27.         lfOrientation As Long
    28.         lfWeight As Long
    29.         lfItalic As Byte
    30.         lfUnderline As Byte
    31.         lfStrikeOut As Byte
    32.         lfCharSet As Byte
    33.         lfOutPrecision As Byte
    34.         lfClipPrecision As Byte
    35.         lfQuality As Byte
    36.         lfPitchAndFamily As Byte
    37.         lfFaceName As String * 31
    38. End Type
    39.  
    40. Private Type CHOOSEFONT
    41.         lStructSize As Long
    42.         hwndOwner As Long          '  caller's window handle
    43.         hDC As Long                '  printer DC/IC or NULL
    44.         lpLogFont As Long          '  ptr. to a LOGFONT struct
    45.         iPointSize As Long         '  10 * size in points of selected font
    46.         flags As Long              '  enum. type flags
    47.         rgbColors As Long          '  returned text color
    48.         lCustData As Long          '  data passed to hook fn.
    49.         lpfnHook As Long           '  ptr. to hook function
    50.         lpTemplateName As String     '  custom template name
    51.         hInstance As Long          '  instance handle of.EXE that
    52.                                        '    contains cust. dlg. template
    53.         lpszStyle As String          '  return the style field here
    54.                                        '  must be LF_FACESIZE or bigger
    55.         nFontType As Integer          '  same value reported to the EnumFonts
    56.                                        '    call back with the extra FONTTYPE_
    57.                                        '    bits added
    58.         MISSING_ALIGNMENT As Integer
    59.         nSizeMin As Long           '  minimum pt size allowed &
    60.         nSizeMax As Long           '  max pt size allowed if
    61.                                        '    CF_LIMITSIZE is used
    62. End Type
    63.  
    64.  
    65. Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
    66. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    67. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    68. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    69. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    70. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    71. Private Sub Command1_Click()
    72.     MsgBox ShowFont
    73. End Sub
    74. Private Sub Form_Load()
    75.     Command1.Caption = "ShowFont"
    76. End Sub
    77.  
    78. Private Function ShowFont() As String
    79.     Dim cf As CHOOSEFONT, lfont As LOGFONT, hMem As Long, pMem As Long
    80.     Dim fontname As String, retval As Long
    81.     lfont.lfHeight = 0  ' determine default height
    82.     lfont.lfWidth = 0  ' determine default width
    83.     lfont.lfEscapement = 0  ' angle between baseline and escapement vector
    84.     lfont.lfOrientation = 0  ' angle between baseline and orientation vector
    85.     lfont.lfWeight = FW_NORMAL  ' normal weight i.e. not bold
    86.     lfont.lfCharSet = DEFAULT_CHARSET  ' use default character set
    87.     lfont.lfOutPrecision = OUT_DEFAULT_PRECIS  ' default precision mapping
    88.     lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS  ' default clipping precision
    89.     lfont.lfQuality = DEFAULT_QUALITY  ' default quality setting
    90.     lfont.lfPitchAndFamily = DEFAULT_PITCH Or FF_ROMAN  ' default pitch, proportional with serifs
    91.     lfont.lfFaceName = "Times New Roman" & vbNullChar  ' string must be null-terminated
    92.     ' Create the memory block which will act as the LOGFONT structure buffer.
    93.     hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(lfont))
    94.     pMem = GlobalLock(hMem)  ' lock and get pointer
    95.     CopyMemory ByVal pMem, lfont, Len(lfont)  ' copy structure's contents into block
    96.     ' Initialize dialog box: Screen and printer fonts, point size between 10 and 72.
    97.     cf.lStructSize = Len(cf)  ' size of structure
    98.     cf.hwndOwner = Form1.hWnd  ' window Form1 is opening this dialog box
    99. '    cf.hDC = Printer.hDC  ' device context of default printer (using VB's mechanism)
    100.     cf.lpLogFont = pMem   ' pointer to LOGFONT memory block buffer
    101.     cf.iPointSize = 120  ' 12 point font (in units of 1/10 point)
    102.     cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
    103.     cf.rgbColors = RGB(0, 0, 0)  ' black
    104.     cf.nFontType = REGULAR_FONTTYPE  ' regular font type i.e. not bold or anything
    105.     cf.nSizeMin = 10  ' minimum point size
    106.     cf.nSizeMax = 72  ' maximum point size
    107.     ' Now, call the function.  If successful, copy the LOGFONT structure back into the structure
    108.     ' and then print out the attributes we mentioned earlier that the user selected.
    109.     retval = CHOOSEFONT(cf)  ' open the dialog box
    110.     If retval <> 0 Then  ' success
    111.         CopyMemory lfont, ByVal pMem, Len(lfont)  ' copy memory back
    112.         ' Now make the fixed-length string holding the font name into a "normal" string.
    113.         ShowFont = Left(lfont.lfFaceName, InStr(lfont.lfFaceName, vbNullChar) - 1)
    114.         Debug.Print  ' end the line
    115.     End If
    116.     ' Deallocate the memory block we created earlier.  Note that this must
    117.     ' be done whether the function succeeded or not.
    118.     retval = GlobalUnlock(hMem)  ' destroy pointer, unlock block
    119.     retval = GlobalFree(hMem)  ' free the allocated memory
    120. End Function

    Hope this helps...
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  3. #3
    Addicted Member Abilio's Avatar
    Join Date
    May 2003
    Location
    Aveiro - Portugal
    Posts
    222
    toughcoder

    Exellent work !!

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    58
    Ok thanks but can anyone also tell how to use that Common dialog control for showing that Font dialog box ?

  5. #5
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    Thanx Abilio

    Well Mahesh, for using the CommonDialog control, place an instance of it on ur form, add a button, then put the following code

    VB Code:
    1. Private Sub Command1_Click()
    2. With CommonDialog1
    3. .Flags = cdlCFScreenFonts
    4. .ShowFont
    5. End With
    6. End Sub

    Hope this helps...
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    58
    Ok and lastly how to code for those seperate toolbar buttons for Bold, Italic, Underline, Font Color and a dropdown listbox having font sizes. These are below the subject of the New Message window in the Outlook Express. Thanks.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Shouldn't the code for Cut be...
    VB Code:
    1. Private Sub mnuEditCut_Click()
    2.     On Error Resume Next
    3.     Clipboard.Clear
    4.     Clipboard.SetText Text1.text
    5.     Text1.Text = vbNullString
    6. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    58
    Yes Font window is appearing after I created this code,

    VB Code:
    1. Private Sub Command1_Click()
    2. With CommonDialog1
    3. .Flags = cdlCFScreenFonts
    4. .ShowFont
    5. End With
    6. End Sub

    But the text is not getting updated with my desired Font. So any code to do that? Thanks.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    You need to get the selection from the fontdialog and format you
    text accordingly.

    VB Code:
    1. Text.Font = CommonDialog1.Font
    2. '...
    3. '...
    4. '...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    Yep Robby, my mistake , thanx for pointing it out.

    Hello Gopi, u gotta apply the Common Dialog selection to the text.
    Use the following code for Bold, Italic, Underline & FontColor...

    VB Code:
    1. Private Sub Command1_Click()
    2. Text1.FontBold = True
    3. Text1.FontItalic = True
    4. Text1.FontUnderline = True
    5. Text1.ForeColor = vbGreen
    6. End Sub

    For applying Common Dialog values, use this code...

    VB Code:
    1. Private Sub Command2_Click()
    2. With CommonDialog1
    3. .Flags = cdlCFScreenFonts
    4. .ShowFont
    5. Text2.FontBold = .FontBold
    6. Text2.FontItalic = .FontItalic
    7. Text2.FontUnderline = .FontUnderline
    8. Text2.Font = .FontName
    9. Text2.FontSize = .FontSize
    10. End With
    11. End Sub

    Happy coding...
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No prob.

    You may want to put some error trapping in case the user cancels
    the Font dialog also.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width