
'------------------------------------------------------------------------------------------------------------
     'Getting sel text

     'You have to set the end selection for a single sel to + 1

     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,5,5)
     CONTROLCMD w1,1,@RTGETSELTEXT,Line2$
     print "Text is ",Line2$ '<---------- Nothing!

     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,5,6) '<----- end position + 1
     CONTROLCMD w1,1,@RTGETSELTEXT,Line2$
     print "Text is ",Line2$ '<---------- Gets the sel text

'------------------------------------------------------------------------------------------------------------

     'Must re-select a selection after doing something like colouring

     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,6)
     cf.dwMask = CFM_COLOR
     cf.crTextColor = Red
     cf.cbSize = SIZEOF(cf)
     SENDMESSAGE(getcontrolhandle(w1,1),EM_SETCHARFORMAT,SCF_SELECTION,&cf)

     'Reselection here - also has the effect of highlighting
     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,6) '<--- Without re-selecting you won't get any text!
     CONTROLCMD w1,1,@RTGETSELTEXT,Line2$
     print "Text is ",Line2$

     'To remove the highlight you must select a single sel 
     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,0)

'------------------------------------------------------------------------------------------------------------

     'Setting focus back to your rich edit control

     'Using SETFOCUS on it's own does not work if you want to make the cursor flash!
     setfocus w1,1 '<--- No good

     'You need to set a sel firt
     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,0) '<---- Set a sel first
     setfocus w1,1 '<---- Now it works

'------------------------------------------------------------------------------------------------------------

     'Getting text:

     'Select your start / end positions first
     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,6) 
     CONTROLCMD w1,1,@RTGETSELTEXT,Line2$

     'Occasionally (and reason unknown) you don't get any text although your selction is correcrt!
     'Try using the Win32 API message instead:

     string dd
     SendMessage rHandle,EM_GETSELTEXT,0,&dd
     print "Text is ",dd

'------------------------------------------------------------------------------------------------------------

     'When speed is paramount:

     '1 - Turn off the rich edit notification messages
     'Notifications off:
	  eventMask = SendMessage(rHandle,EM_SETEVENTMASK,0,mask)
	  SendMessage(rHandle,WM_SETREDRAW,false,0)

					  'Do something here.....

     'Notifications on:
	  SendMessage(rHandle,WM_SETREDRAW,TRUE,0)
	  InvalidateRect(rHandle,0,true)
	  SendMessage(rHandle,EM_SETEVENTMASK,TRUE,ENM_CHANGE 

     'You must remember to turn them back on again afterwards! 

     '2 - Use Win32 API messages instead of IWB commands:
     SendMessage rHandle,EM_GETSELTEXT,0,&dd '<---- Use this 
     CONTROLCMD w1,1,@RTGETSELTEXT,dd        '<---- Instead of this

'------------------------------------------------------------------------------------------------------------

     'Checking you have the right selection

     'An easy way to check you have the right selection before say deleting it is to colour it
     SendMessage(getcontrolhandle(w1,1),EM_SETSEL,0,6)
     cf.dwMask = CFM_COLOR
     cf.crTextColor = Red
     cf.cbSize = SIZEOF(cf)
     SENDMESSAGE(getcontrolhandle(w1,1),EM_SETCHARFORMAT,SCF_SELECTION,&cf)

     'Once the correct sels are coloured just remove the colouring code and do what ever

'------------------------------------------------------------------------------------------------------------

     'The cursor:

     'Think about where you want the cursor set to

     'Doing something, say colouring means the cursor is lost afterwards

     'To get the cursor back select a single sel last   

'------------------------------------------------------------------------------------------------------------


     'Know which line you are on:
     cLine = CONTROLCMD(w1,1,@RTLINEFROMCHAR,-1) '<---- Using -1 gets the current line (clicked on)

     'Know where you are (character position) on the line
     POINTER pt

     case 1 'Rich edit
	       SendMessage w1,EM_POSFROMCHAR, &pt, pt.y ,1
	       cPos = GETCARETPOSITION w1,x,y 

     'Remember that the current position in line X is NOT the sel position!
     'cPos might be position 5 in line X, but it's sel position could be say 72

     'Know the position (Offest) of the first character on a chosen line in the rich edit
     Offset = SendMessage(rHandle,EM_LINEINDEX,22,0)
     'First character position for line 22 

'------------------------------------------------------------------------------------------------------------

     'Things you will need...

     'Add Richedit.inc to the start of your code:
     $INCLUDE "richedit.inc"      

     'Sub class your rich edit:

	  CONTROL w1,@RICHEDIT,"",110,100,10,480,0x50B010C4,1 
 	  SubclassRichEdit(w1,1)

	  SUB SubclassRichEdit(parent as WINDOW,id as INT)
		   hEdit = GETCONTROLHANDLE(parent,id)
		   lpfn = SetWindowLongA(hEdit,-4,&Rich_Edit_Handler)
		   SetPropA(hEdit,"edit_handler",lpfn)   
	  RETURN
	  ENDSUB
	  SUB UnSubclassRichEdit(parent as WINDOW,id as INT)
		   hEdit = GETCONTROLHANDLE(parent,id)
		   SetWindowLongA(hEdit,-4,GetPropA(hEdit,"edit_handler"))
		   RemovePropA(hEdit,"edit_handler")
	  RETURN
	  ENDSUB

     SUB Rich_Edit_Handler(hwnd:int,uMsg:int,wParam:int,lParam:pointer),int
         SELECT uMsg
                case WM_CHAR 'And so on...


     'For colouring text:
	  TYPE CHARFORMAT
		    UINT		 cbSize
		    DWORD	 dwMask
		    DWORD	 dwEffects
		    LONG		 yHeight
		    LONG		 yOffset
		    COLORREF crTextColor
		    BYTE		 bCharSet
		    BYTE		 bPitchAndFamily
		    CHAR	    szFaceName[LF_FACESIZE]
	  ENDTYPE
 	  CHARFORMAT cf

     'To get highlight start / end positions you need:
	  pointer cr
	  type Charrange
		    uint cpMin 'Start position
		    uint cpMax 'End position
	  endtype
	  cr = NEW(Charrange,5)
	  settype cr,Charrange

     'For mouse position:
     pointer mMouse
     type mouse_event
          int dwFlags
          int dx
          int dy
          int dwData
          uint dwExtraInfo
     endtype
     mMouse = NEW(mouse_event,5)  


