LButtonDblClk in CEditView

In my Edit object, For example,it may contain some text as below:
if (iRet == -1)
{
::GlobalFree(lpBuff);
::GlobalFree(lpBuffUndo);
::GlobalFree(lpBuffRedo);
lpBuffUndo = 0;
lpBuffRedo = 0;
lpBuff = 0;
return;
}

when I double click left mouse button in ::GlobalFree, it will select "::GlobalFree" not "GlobalFree" just as in vc edit window.
How to make it works like in vc edit window.
thanks.

dead_fishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aphillipsCommented:
Intercept the double click event, work out where in the edit control was d-clicked (what word was d-clicked) and select it wth SetSel().
0
jemaxCommented:
Hi,

 aphillips is completely right, but instead of detecting where user has clicked, just let default hander work first, then GetSel(), adjust if needed, and SetSel(). It looks easier a bit.

HTH,
jemax
0
dead_fishAuthor Commented:
here is my code:
void CDsbView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
     CEdit& mEdit = GetEditCtrl();
     mEdit.GetSel(iStart,iEnd);
     iEnd = iStart + 1;

     CEditView::OnLButtonDblClk(nFlags, point);

     CString sTmp;
     char sChar[3];
     int i,j;

     GetSelectedText(sTmp);
     memset(sChar,0,sizeof(sChar));    
     mEdit.SetSel(iStart,iEnd);
     GetSelectedText(sTmp);
     strcpy(sChar,sTmp.GetBuffer(2));

     if ((sChar[0] >= 48)&&(sChar[0] <= 57) ||
        (sChar[0] >= 65)&&(sChar[0] <= 90) ||
          (sChar[0] >= 97)&&(sChar[0] <= 122))
     {
        i = iStart;
        while (1)
          {
               mEdit.SetSel(i-1,i);
               GetSelectedText(sTmp);
               strcpy(sChar,sTmp.GetBuffer(2));
               if (!((sChar[0] >= 48)&&(sChar[0] <= 57) ||
                      (sChar[0] >= 65)&&(sChar[0] <= 90) ||
                      (sChar[0] >= 97)&&(sChar[0] <= 122)))
               {
                    break;
               }
               i--;
          }
         
          j = iEnd;
        while (1)
          {
               mEdit.SetSel(j,j+1);
               GetSelectedText(sTmp);
               strcpy(sChar,sTmp.GetBuffer(2));
               if (!((sChar[0] >= 48)&&(sChar[0] <= 57) ||
                    (sChar[0] >= 65)&&(sChar[0] <= 90) ||
                    (sChar[0] >= 97)&&(sChar[0] <= 122)))
               {
                    break;
               }
               j++;
          }

          mEdit.SetSel(i,j);
          GetSelectedText(sTmp);
     }
}

when debug to the last line: GetSelectedText(sTmp);
sTmp = "GlobalFree"

but when out of the function,it still select "::GlobalFree"!!

why???
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

aphillipsCommented:
jemax said:
> but instead of detecting where user has clicked, just let default hander work first, then GetSel()...

trouble is you can't tell whether they clicked on the "::" or on the "GlobalFree".  So you don't know which word to select.

dead_fish said:
> here is my code:

I suspect that something is setting it back before the mouse event returns, or perhaps in OnLButtonUp.

What does GetSelectedText do?
0
jemaxCommented:
aphillips said:
>>trouble is you can't tell whether they clicked on the "::" or on the "GlobalFree"

dead_fish said "...just as in vc edit window". In vc editor if you click on "::", only ":" is selected

aphillips said:
>>I suspect that something is setting it back before the mouse event returns, or perhaps in OnLButtonUp.

that's right, it seems like you cannot change selection inside OnLButtonDblClk

As it came out, CEdit has func CharFromPos(CPoint), so the best solution, in my opinion, is aphillips's first comment. Something like that:
void CMyEditView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
     LPCTSTR lpszBuffer;
     int iStart, iEnd, iBufLen;
     CEdit& theEdit = GetEditCtrl();

     iBufLen = GetBufferLength();

     iStart = LOWORD(theEdit.CharFromPos(point));
     iEnd = iStart;
     
     if (iBufLen == iStart)
          return;

     lpszBuffer = LockBuffer();

     if (_istalnum(lpszBuffer[iStart])) //to select only ":" as in vc:)
     {
          for (; (iStart > 0) && _istalnum(lpszBuffer[iStart-1]); iStart--);
          for (; (iEnd < iBufLen) && _istalnum(lpszBuffer[iEnd+1]); iEnd++);
     }

     UnlockBuffer();

     theEdit.SetSel(iStart, iEnd+1);
}


or if you want to rely on CEdit selection code, you can set some flag in OnLButtonDblClk, and move your selection adjusting code into OnLButtonUp

CMyEditView::CMyEditView()
{
     // TODO: add construction code here
     m_fAdjustSelection = FALSE;
}


void CMyEditView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
     CEditView::OnLButtonDblClk(nFlags, point);

     m_fAdjustSelection = TRUE;
}

void CMyEditView::OnLButtonUp(UINT nFlags, CPoint point)
{
     LPCTSTR lpszBuffer;
     int iStart, iEnd;
     CEdit& theEdit = GetEditCtrl();

     CEditView::OnLButtonUp(nFlags, point);

     if (!m_fAdjustSelection)
          return;

     theEdit.GetSel(iStart, iEnd);

     lpszBuffer = LockBuffer();

     for (; (iStart < iEnd) && !_istalnum(lpszBuffer[iStart]); iStart++);
     for (--iEnd; (iEnd > iStart) && !_istalnum(lpszBuffer[iEnd]); iEnd--);

     UnlockBuffer();

     theEdit.SetSel(iStart, iEnd+1);
}




HTH,
jemax
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aphillipsCommented:
jemax said:
> dead_fish said "...just as in vc edit window". In vc editor if you click on "::", only ":" is selected

Exactly my point.  If the user clicks on a colon or if the user clicks on an 'e' or any other character of "::GlobalFree" then all of "::GlobalFree" is selected.  How do you then tell what should be done just by getting the current selection?
0
dead_fishAuthor Commented:
thank you and others so much!
0
jemaxCommented:
Thank you, dead_fish.


aphillips said:
>> user clicks on an 'e' or any other character of "::GlobalFree" then all of "::GlobalFree" is selected

in vc editor if user clicks on a colon only the colon is selected. In my vc(6) at least :-)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.