How can I add a RichTextBox control to a PB/DLL program? How can I use SelLength, SelStart, SelText, and SelColor functions to print the word "Stop" in red and the rest of the string in black? Jeffrey.
Announcement
Collapse
No announcement yet.
RichTextBox
Collapse
X
-
Jeffrey, you certainly choose the hardest topics for your first PB/DLL program!
Assuming you are using DDT, you can use a CONTROL ADD "RICHEDIT" statament, but you have to explicitly load the RICHED32.DLL library using LoadLibrary(). See the PBNOTE.BAS example file for the necessary code to load the library, etc.
If all you wish to to is display STOP in red/black, then there are other ways... for example, use an IMAGE control with a BMP with the word STOP on it, etc. Yet another way (a bit complicated) is to create an static control (LABEL) and specify a custom font of the appropriate size, then intercept the
%WM_CTLCOLORSTATIC message and set the foreground & background colors - I seem to remember that you may have already experimented with code along these lines?
------------------
Lance
PowerBASIC Support
mailto:[email protected][email protected]</A>Lance
mailto:[email protected]
-
Lance, I don't think that PBNOTE program uses RichTextBox control. I looked through the code, WIN32API.INC, and RICHEDIT.INC files but they were not helpful. I found a sample program that used RichTextBox control in a message posted on this forum and used it. I think that usingCode:and
------------------
Comment
-
Not sure this is what you are looking for, but I've just posted a
small sample in the Source code section that shows how to create
and fill a Richedit control with text, search for all occurrences
of a certain word and then mark them all with a new color.
It loads the text "Start and Stop", where "Stop" then can be marked
with red (since that's the word you were asking for).
Who knows, maybe you can find something useful in it..
------------------
Comment
-
The PBNOTE.BAS example I have here uses a RICHEDIT control, but it seems that the most recent version of this file (as supplied with PB/DLL 6.0) uses a standard EDIT control instead. Sorry for the misinformation.
There have been quite a few threads in this BBS about using RichEdit controls, so you should find a few useful code snippets.
------------------
Lance
PowerBASIC Support
mailto:[email protected][email protected]</A>Lance
mailto:[email protected]
Comment
-
Borje, Thank you very much for the sample code. I modified the code for my subway stop program but I want "Stop" and "Change" to be printed in red and "Skip" to be printed in green. Here is the code:
Code:#compile exe #dim all #include "win32api.inc" #include "commctrl.inc" #include "richedit.inc" global hfont as long type charformat2 cbsize as long dwmask as dword dweffects as dword yheight as long yoffset as long crtextcolor as dword bcharset as byte bpitchandfamily as byte szfacename as asciiz * %lf_facesize szdummy as integer end type %id_richedit=500 global hedit as long declare callback function dlgcallback() declare function markall(byval mytext as string) as long declare function setrichtextcolor(byval newcolor as long) as long function pbmain local hdlg as long local hriched as long local rtext as string hriched=loadlibrary("riched32.dll") call initcommoncontrols dialog new 0,"DDT Richedit Demo",,,200,138,%ws_sysmenu+%ws_maximize to hdlg control add button,hdlg,%idok,"Next Station",10,20,50,20 control add button,hdlg,%idcancel,"E&xit",10,70,50,20 control add "richedit",hdlg,%id_richedit,"",70,10,450,40,%ws_visible or %ws_child hfont=createfont(60,30,0,0,0,0,0,0,0,0,0,0,0,"arial") control send hdlg,%id_richedit,%wm_setfont,hfont,%true control handle hdlg,%id_richedit to hedit rtext="Stop at Stillwell Avenue" call sendmessage(hedit,%wm_settext,0,strptr(rtext)) call markall("Stop") setfocus hedit dialog show modal hdlg call dlgcallback end function function setrichtextcolor(byval newcolor as long) as long local cf as charformat2 cf.cbsize=len(cf) cf.dwmask=%cfm_color cf.crtextcolor=newcolor call sendmessage(hedit,%em_setcharformat,%scf_selection,varptr(cf)) end function function markall(byval mytext as string) as long local alltext as string local tlen as long local stoppos as long tlen=sendmessage(hedit,%wm_gettextlength,0,0)+1 alltext=space$(tlen) call sendmessage(hedit,%wm_gettext,tlen,strptr(alltext)) call sendmessage(hedit,%wm_setredraw,0,0) stoppos=instr(alltext,mytext) do while stoppos call sendmessage(hedit,%em_setsel,stoppos-1,stoppos+len(mytext)-1) call setrichtextcolor(&hff) stoppos=instr(stoppos+len(mytext)-1,alltext,mytext) loop call sendmessage(hedit,%em_setsel,0,0) setfocus hedit sendmessage hedit,%wm_setredraw,1,0 invalidaterect hedit,byval %null,0 updatewindow hedit end function callback function dlgcallback() if cbmsg=%wm_command then select case cbctl case %idok call markall("Stop") case %idcancel call deleteobject(hfont) dialog end cbhndl,(cbctl=%idok) end select end if end function
------------------
Comment
-
Why not lift out the actual color marking to a separate sub, like:
Code:'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Loop through string, pick out positions, select occurrences and then paint ' Note - Instr returns pos +1 in relation to Selstart, so we have to use -1 '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ SUB MarkString(BYVAL AllText AS STRING, BYVAL MyText AS STRING, BYVAL cCol AS LONG) LOCAL stopPos AS LONG stopPos = INSTR(AllText, myText) DO WHILE stopPos CALL SendMessage(hEdit, %EM_SETSEL, stopPos - 1, stopPos + LEN(myText) - 1) CALL setRichTextColor(cCol) stopPos = INSTR(stopPos + LEN(myText) - 1, AllText, myText) LOOP END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' This is a simple "find all" Function '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION MarkAll (BYVAL myText AS STRING) AS LONG LOCAL AllText AS STRING, tLen AS LONG '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'Put Richedit's contents into a string '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tLen = SendMessage(hEdit, %WM_GETTEXTLENGTH, 0, 0) + 1 AllText = SPACE$(tLen) CALL SendMessage(hEdit, %WM_GETTEXT, tLen, STRPTR(AllText)) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Turn off redraw to get faster and smoother action '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CALL SendMessage(hEdit, %WM_SETREDRAW, 0, 0) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' call separate sub to set text color for any text you want '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CALL MarkString(AllText, "Start", &HFF) 'bright red CALL MarkString(AllText, "Stop", &HFF) 'bright red CALL MarkString(AllText, "Skip", &H008000) 'green CALL SendMessage(hEdit, %EM_SETSEL, 0, 0) SetFocus hEdit 'SetFocus to activate it after work is done, if you like.. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ' Turn on Redraw again and refresh '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SendMessage hEdit, %WM_SETREDRAW, 1, 0 'Refresh and reset redraw InvalidateRect hEdit, BYVAL %NULL, 0 : UpdateWindow hEdit END FUNCTION
------------------
Comment
-
Borje, I modified the code so that it looks like this:
Code:#compile exe #dim all #include "win32api.inc" #include "commctrl.inc" #include "richedit.inc" global hfont as long global buttonpressed as long global stopindex as long type charformat2 cbsize as long dwmask as dword dweffects as dword yheight as long yoffset as long crtextcolor as dword bcharset as byte bpitchandfamily as byte szfacename as asciiz * %lf_facesize szdummy as integer end type %id_richedit=500 global hedit as long declare callback function dlgcallback() declare sub markstring(byval alltext as string,byval mytext as string,byval ccol as long) declare function markall(byval mytext as string) as long declare function setrichtextcolor(byval newcolor as long) as long sub selectstation select case stopindex case 1 call text01("Stop at Beach 67th Street") call markall("Stop") case 2 call text01("Skip at Beach 60th Street") call markall("Skip") case 3 call text01("Stop at Beach 44th Street") call markall("Stop") case 4 call text01("Skip at Beach 36th Street") call markall("Skip") case 5 call text01("Stop at Beach 25th Street") call markall("Stop") case 6 call text01("Change Direction at Far Rockaway") call markall("Change") case 7 call text01("Stop at Beach 25th Street") call markall("Stop") case 8 call text01("Skip at Beach 36th Street") call markall("Skip") case 9 call text01("Stop at Beach 44th Street") call markall("Stop") case 10 call text01("Skip at Beach 60th Street") call markall("Skip") case 11 call text01("Stop at Beach 67th Street") call markall("Stop") end select end sub sub text01(string01 as string) call sendmessage(hedit,%wm_settext,0,strptr(string01)) call markall("Stop") setfocus hedit end sub callback function nextstation() as long buttonpressed=1 incr stopindex call selectstation end function function pbmain local hdlg as long local hriched as long local rtext as string hriched=loadlibrary("riched32.dll") call initcommoncontrols dialog new 0,"DDT Richedit Demo",,,200,138,%ws_sysmenu+%ws_maximize to hdlg control add button,hdlg,%idok,"Next Station",10,20,50,20 call nextstation control add button,hdlg,%idcancel,"E&xit",10,70,50,20 control add "richedit",hdlg,%id_richedit,"",70,10,450,40,%ws_visible or %ws_child hfont=createfont(60,30,0,0,0,0,0,0,0,0,0,0,0,"arial") control send hdlg,%id_richedit,%wm_setfont,hfont,%true control handle hdlg,%id_richedit to hedit stopindex=1 call selectstation dialog show modal hdlg call dlgcallback end function function setrichtextcolor(byval newcolor as long) as long local cf as charformat2 cf.cbsize=len(cf) cf.dwmask=%cfm_color cf.crtextcolor=newcolor call sendmessage(hedit,%em_setcharformat,%scf_selection,varptr(cf)) end function sub markstring(byval alltext as string,byval mytext as string,byval ccol as long) local stoppos as long stoppos=instr(alltext,mytext) call sendmessage(hedit,%em_setsel,stoppos-1,stoppos+len(mytext)-1) call setrichtextcolor(ccol) call sendmessage(hedit,%em_setsel,stoppos+4,stoppos+len(alltext)) call setrichtextcolor(0) end sub function markall(byval mytext as string) as long local alltext as string,tlen as long tlen=sendmessage(hedit,%wm_gettextlength,0,0)+1 alltext=space$(tlen) call sendmessage(hedit,%wm_gettext,tlen,strptr(alltext)) call sendmessage(hedit,%wm_setredraw,0,0) call markstring(alltext,"Change",&h80) call markstring(alltext,"Stop",&h80) call markstring(alltext,"Skip",&h008000) call sendmessage(hedit,%em_setsel,0,0) setfocus hedit sendmessage hedit,%wm_setredraw,1,0 invalidaterect hedit,byval %null,0 updatewindow hedit end function callback function dlgcallback() if cbmsg=%wm_command then select case cbctl case %idok call markall("Stop") case %idcancel call deleteobject(hfont) dialog end cbhndl,(cbctl=%idok) end select end if end function
------------------
Comment
-
You don't check to see if Instr actually finds something to mark,
so the function marks everything, which means the last word you
send to it, "Skip" will set the final looks of things.
The following should work:
Code:SUB markstring(BYVAL alltext AS STRING,BYVAL mytext AS STRING,BYVAL ccol AS LONG) LOCAL stoppos AS LONG stoppos=INSTR(alltext,mytext) IF stoppos THEN 'must check to see if something actually was found!!! CALL sendmessage(hedit,%em_setsel,stoppos-1,stoppos+LEN(mytext) - 1) CALL setrichtextcolor(ccol) CALL sendmessage(hedit,%em_setsel,stoppos+LEN(mytext)-1,stoppos+LEN(alltext)-1) CALL setrichtextcolor(0) END IF END SUB
Comment
-
Glad to hear you find it useful Jules, especially since I've
learned so much from *you* in these forums over the years..
I actually don't use the Richedit control that much myself, but I'll
see if I can wrap up a more complete sample for it this weekend for
the source code section. It seems to be some need for one..
------------------
Comment
Comment