Article View: borland.public.cppbuilder.nativeapi
Article #4973Extracting links from TRichEdit by SelStart
From: Gothi[c]
Date: Tue, 18 Oct 2005 17:43
Date: Tue, 18 Oct 2005 17:43
162 lines
5320 bytes
5320 bytes
Hello,
I have a TRichEdit control in which I am putting hyperlinks with this code:
[begin code]
bool __fastcall TFrmMain::InsertLink(HWND hRich, int nPos, const char
*pszTitle, const char *pszURL){
// Declarations
Richedit::CHARFORMAT2 cf2 ;
int nTitleLen, nURLen ;
char *pszLinkBuf = NULL ;
// Check if the pointers are valid and pszTitle has to be at least 2
characters.
if (!pszURL || !pszTitle || !pszTitle[0] || !pszTitle[1])
return FALSE; //title too short
// Get lengths of title & URL
nTitleLen = strlen (pszTitle) ;
nURLen = strlen (pszURL) + URL_HDR_LEN ;
// Allocate buffer
pszLinkBuf = new char [nTitleLen + nURLen] ;
if (!pszLinkBuf) return FALSE ;
// Reset cf2
memset (&cf2, 0, sizeof(cf2)) ;
cf2.cbSize = sizeof(cf2) ;
// 1st letter+url len+url text+rest of the title
sprintf (pszLinkBuf, "%c%04X%s%s", *pszTitle, nURLen, pszURL, pszTitle +
1);
// Set title text
::SendMessage (hRich, EM_SETSEL, nPos, nPos);
::SendMessage (hRich, EM_REPLACESEL, 0, (LPARAM)pszLinkBuf);
// Now we don't need the buf anymore, so delete it
delete[] pszLinkBuf;
// Apply link style on'G'
::SendMessage (hRich, EM_SETSEL, nPos, nPos+1);
cf2.dwMask = CFE_LINK|CFE_PROTECTED;
cf2.dwEffects = CFM_LINK|CFM_PROTECTED;
::SendMessage (hRich, EM_SETCHARFORMAT, SCF_SELECTION , (LPARAM)&cf2);
// Hide URL text
nPos++;
::SendMessage (hRich, EM_SETSEL, nPos, nPos+nURLen);
cf2.dwMask = CFE_LINK|CFE_PROTECTED|CFE_HIDDEN;
cf2.dwEffects = CFM_LINK|CFM_PROTECTED|CFM_HIDDEN;
::SendMessage (hRich, EM_SETCHARFORMAT, SCF_SELECTION , (LPARAM)&cf2);
// Apply link&protected style on the rest of the title
nPos += nURLen;
::SendMessage (hRich, EM_SETSEL, nPos, nPos+nTitleLen-1);
cf2.dwMask = CFE_LINK|CFE_PROTECTED;
cf2.dwEffects = CFM_LINK|CFM_PROTECTED;
::SendMessage (hRich, EM_SETCHARFORMAT, SCF_SELECTION , (LPARAM)&cf2);
return TRUE;
}
[end code]
Now I'm trying to extract these links and their url, and text, and store
them in a struct. But it seems to be failing. It keeps returning empty
strings. I would also like to get rid of the nLen parameter so I only need
to give the position and if this is within a hyperlink, it returns a valid
EW_link structure. I feel like I'm making things too complicated with this
code,.. Does anyone know a more efficient (and working) method of doing
this?
[begin code]
EW_link __fastcall TFrmMain::extractLink(int nPos, int nLen){
// Declarations
Richedit::TEXTRANGE tr ;
CHARRANGE chrg ;
int nURLen, nRange ;
EW_link lReturn ;
// Set up errorous link return
lReturn.selStart = -1 ;
lReturn.selLength = -1 ;
lReturn.text = "" ;
lReturn.url = "" ;
lReturn.target = "" ;
// Prepare TEXTRANGE and aloc suff. buf for link text
chrg.cpMin = nPos ;
chrg.cpMax = nPos + nLen ;
tr.chrg = chrg ;
tr.lpstrText = new TCHAR[tr.chrg.cpMax-tr.chrg.cpMin + 1] ;
if (!tr.lpstrText) return lReturn ; //alloc failed
// Get link text
nRange = ::SendMessage (FrmMain->TxtContents->Handle, EM_GETTEXTRANGE, 0 ,
(LPARAM)&tr) ;
// Get URL length
if (!sscanf (tr.lpstrText + 1, "%04X", &nURLen) || nRange < nURLen + 1)
return lReturn;
// Zero terminate URL-text string.
tr.lpstrText[nURLen + 1] = 0 ;
// Extract into EW_Link
lReturn.selStart = nPos ;
lReturn.selLength = nLen ;
lReturn.url = tr.lpstrText ;
// Cleanup
delete[] tr.lpstrText ;
// Exit
return lReturn ;
}
[end code]
Ps: The EW_link structure is as follows:
struct EW_link{
int selStart ;
int selLength ;
AnsiString text ;
AnsiString url ;
AnsiString target ;
} ;
Just a simple struct to store the links in a more readable way. I might rid
myself of the selStart and selLength in there, as they no longer would serve
any purpose when this code works properly. The placement of the link is
variable anyway if the user moves text around etc.
Technical Details
Message-ID:
<nLWdnaETQKGXqcjeUSdV9g@ptd.net>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.ptd.net!news.ptd.net.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e42d5f21ef0bfa82,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.ptd.net!news.ptd.net.POSTED!not-for-mail NNTP-Posting-Date: Tue, 18 Oct 2005 12:42:34 -0500 From: "Gothi[c]" <gothic@ptd.net> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Extracting links from TRichEdit by SelStart Date: Tue, 18 Oct 2005 13:43:22 -0400 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Message-ID: <nLWdnaETQKGXqcjeUSdV9g@ptd.net> Lines: 164 NNTP-Posting-Host: 24.115.151.204 X-Trace: sv3-Dg44EaqYd08Ua4lHM50kUxBWtXaOuGy/LaRdXs7FC4KXFaP1NYfGMA63+oW6W025w9d6X0qrIDTwNb8!pJ3jHvdRhLqbGZdqNwoTMP4rnXN5N4KfIBkfkb5McIoDwJZjSQ3RzuTvvCevJpk1/tBOjdZ+Nw== X-Complaints-To: abuse@ptd.net X-DMCA-Complaints-To: abuse@ptd.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32