Announcement

Collapse
No announcement yet.

Highlited text in Richtext box

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Highlited text in Richtext box

    Can someone please give me some hints or an example of how to highlight one or more words in a text file displayed in a Rich text box.
    I have looked at the API calls but cannot work out the syntax.
    Thanks for any code or hints.

    Ed Turnbull (New to PB)

    ------------------

  • #2
    To set selection, one can use the %EM_EXSETSEL message. Need to know
    positions, of course, but sample to select character 10 to 20 in text:
    Code:
      LOCAL cr AS CHARRANGE
      cr.cpMin = 10 : cr.cpMax = 20
      SendMessage hRichEdit, %EM_EXSETSEL, 0, VARPTR(cr)

    ------------------
    http://www.tolkenxp.com/pb
    Download: incLean, PBcodec, custom controls and code, etc.
    Borje Hagsten - [email protected]

    Comment


    • #3
      Ed
      In case you really meant 'highlight' as opposed to 'set selection', have a look at this sample code..
      Code:
      #Include "WIN32API.INC"
      #Include "RICHEDIT.INC"
      
      Declare CallBack Function DIALOGProc()
      '------------------
      Function PbMain()
        Local lRslt As Long, hDlg As Dword
        LoadLibrary("RICHED32.DLL")
      
      
        Dialog New 0, "RichEdit Test2", , , 225, 110, %WS_SYSMENU To hDlg
        Control Add "RichEdit", hDlg, 1001, "Test2", 5, 5, 205, 85, %WS_CHILD Or _
          %WS_VISIBLE Or %ES_MULTILINE 
      
        
        PostMessage hDlg, %WM_USER + 1000&, 0, 0
        Dialog Show Modal hDlg, Call DIALOGProc To lRslt
      
      
        Function = lRslt
      End Function
      '------------------
      CallBack Function DIALOGProc()
        Local hRichEdit As Dword, TextFromFile As String, RTFHeader As String
        Local cr As CHARRANGE
      
      
        Select Case CbMsg
          Case %WM_USER + 1000&, 0, 0
            hRichEdit = GetDlgItem(CbHndl, 1001)
      
      
            'Read text from file into string
            TextFromFile = "   This is a sample - some text that could have come from a " +_
            "text file - may have had some special words inside that you would like highlighted? "+ _
            " - Chars 10 thru 20 are selected as per Borje's post"
      
      
            Replace "special words" With "{\highlight2 special words}" in TextFromFile
      
      
              'RTF Header: rtfVer1, ansi charset, ansicodepage WindowsUS, Lang EnglishUS, default font'1' -Arial
              'fontTable - defines Times New Roman, Arial (etc..)
              'colorTable - here three colors defined. Note: \cf1 = Red chars. \highlight2 = highlight with color2
      
      
            RTFHeader = "{\rtf1\ansi\ansicpg1252\deflang1033\deff1"+$CRLF+ _ 
            "{\fonttbl{\f0\froman\fcharset0 Times New Roman;}{\f1\fswiss\fcharset0 Arial;}}"+$CRLF+ _
            "{\colortbl\red0\green0\blue255;\red255\green0\blue0;\red0\green255\blue255;}"+$CRLF
      
      
            TextFromFile = RTFHeader + TextFromFile + "}"
      
      
            Control Set Text CbHndl, 1001, TextFromFile
            'select chars as per Borge's post
            cr.cpMin = 10 : cr.cpMax = 20
            SendMessage hRichEdit, %EM_EXSETSEL, 0, VARPTR(cr)
      
      
        End Select
      
      End Function

      Regards Dave

      ------------------




      [This message has been edited by Dave Biggs (edited May 18, 2003).]
      Rgds, Dave

      Comment


      • #4
        Borje and Dave

        Many thanks for the help with how to highlight text.
        The sample code worked first time and I will be able to make the effect that I want to.
        The example of how to use different colours was very helpful.

        Ed Turnbull


        ------------------

        Comment


        • #5
          Originally posted by Edwin Turnbull 2:
          Borje and Dave
          Many thanks for the help with how to highlight text...
          No worries mate!

          For more info on Richtext stuff check out this link,
          posted in another thread by Shannon Larratt.
          http://www.bmeworld.com/shannon/basic/rtfhelp.html
          ( He says it is 'Lame' but really it's )

          Regards Dave


          ------------------
          Rgds, Dave

          Comment

          Working...
          X