From: matanasescu on
Hi,

I have an ATL control used to display text.
The control lives inside a CRichEditControl.
The problem is that the application needs to resize the control to fit
the text.
O solution I adopted is to call FieViewChange() inside of OnDraw, after



I calculate the text extent using DC passed as parameter
(CDC::GetTextExtent).
This allows the control to be redrawn inside the boundaries I specify
in the prev call of OnDraw.
So:


CMyComControl::OnDraw(...)
{
....
CRect rcTextSize = pDC->GetTextExtent( strText, nChars );


if( m_sizeNatural != rcTextSize )
{
m_sizeNatural = rcTextSize + some margins
FireViewChange(); //this will alow OnDraw to be called with
correct margins
}
....



}


However in some specific situations the application crashes. I couldn't


figure out why, but I discovered that removing FireViewChange() from
OnDraw solves the crash.
I believe that calling FireViewChange() inside OnDraw() may be tricky,
but this was the only way I could find to calculate m_sizeNatural
function of text extent (DC dependent).


Is there any other solutions to my problem?
Any comments on this are welcomed.


Thanks

From: Ajay Kalra on
Change the size of control when its text changes and not in OnDraw.
OnDraw can be called many many times without any changes in the text.

---
Ajay

matanase...(a)gmail.com wrote:
> Hi,
>
> I have an ATL control used to display text.
> The control lives inside a CRichEditControl.
> The problem is that the application needs to resize the control to fit
> the text.
> O solution I adopted is to call FieViewChange() inside of OnDraw, after
>
>
>
> I calculate the text extent using DC passed as parameter
> (CDC::GetTextExtent).
> This allows the control to be redrawn inside the boundaries I specify
> in the prev call of OnDraw.
> So:
>
>
> CMyComControl::OnDraw(...)
> {
> ....
> CRect rcTextSize = pDC->GetTextExtent( strText, nChars );
>
>
> if( m_sizeNatural != rcTextSize )
> {
> m_sizeNatural = rcTextSize + some margins
> FireViewChange(); //this will alow OnDraw to be called with
> correct margins
> }
> ....
>
>
>
> }
>
>
> However in some specific situations the application crashes. I couldn't
>
>
> figure out why, but I discovered that removing FireViewChange() from
> OnDraw solves the crash.
> I believe that calling FireViewChange() inside OnDraw() may be tricky,
> but this was the only way I could find to calculate m_sizeNatural
> function of text extent (DC dependent).
>
>
> Is there any other solutions to my problem?
> Any comments on this are welcomed.
>
>
> Thanks

From: matanasescu on
Hi Ajay,

I need the text extent. I can calculate the text extent only when I
receive the DC to draw on. That's in OnDraw.


Ajay Kalra wrote:
> Change the size of control when its text changes and not in OnDraw.
> OnDraw can be called many many times without any changes in the text.
>
> ---
> Ajay
>
> matanase...(a)gmail.com wrote:
> > Hi,
> >
> > I have an ATL control used to display text.
> > The control lives inside a CRichEditControl.
> > The problem is that the application needs to resize the control to fit
> > the text.
> > O solution I adopted is to call FieViewChange() inside of OnDraw, after
> >
> >
> >
> > I calculate the text extent using DC passed as parameter
> > (CDC::GetTextExtent).
> > This allows the control to be redrawn inside the boundaries I specify
> > in the prev call of OnDraw.
> > So:
> >
> >
> > CMyComControl::OnDraw(...)
> > {
> > ....
> > CRect rcTextSize = pDC->GetTextExtent( strText, nChars );
> >
> >
> > if( m_sizeNatural != rcTextSize )
> > {
> > m_sizeNatural = rcTextSize + some margins
> > FireViewChange(); //this will alow OnDraw to be called with
> > correct margins
> > }
> > ....
> >
> >
> >
> > }
> >
> >
> > However in some specific situations the application crashes. I couldn't
> >
> >
> > figure out why, but I discovered that removing FireViewChange() from
> > OnDraw solves the crash.
> > I believe that calling FireViewChange() inside OnDraw() may be tricky,
> > but this was the only way I could find to calculate m_sizeNatural
> > function of text extent (DC dependent).
> >
> >
> > Is there any other solutions to my problem?
> > Any comments on this are welcomed.
> >
> >
> > Thanks

From: AliR on
You can get a dc anytime by calling GetDC(). Don't forget to call ReleaseDC
when you are done with the DC.

void CMyRichEdit::GetTextExtent(CString Text)
{
CDC *pDC = GetDC();

....

ReleaseDC(pDC);
}

By the way a better way to get the text boundaries for a rich edit control
is to use EM_FORMATRANGE. requires a bit more work, but if you are using RTF
it will be very accurate.

AliR.

<matanasescu(a)gmail.com> wrote in message
news:1161023169.185547.317680(a)m73g2000cwd.googlegroups.com...
> Hi Ajay,
>
> I need the text extent. I can calculate the text extent only when I
> receive the DC to draw on. That's in OnDraw.
>
>
> Ajay Kalra wrote:
> > Change the size of control when its text changes and not in OnDraw.
> > OnDraw can be called many many times without any changes in the text.
> >
> > ---
> > Ajay
> >
> > matanase...(a)gmail.com wrote:
> > > Hi,
> > >
> > > I have an ATL control used to display text.
> > > The control lives inside a CRichEditControl.
> > > The problem is that the application needs to resize the control to fit
> > > the text.
> > > O solution I adopted is to call FieViewChange() inside of OnDraw,
after
> > >
> > >
> > >
> > > I calculate the text extent using DC passed as parameter
> > > (CDC::GetTextExtent).
> > > This allows the control to be redrawn inside the boundaries I specify
> > > in the prev call of OnDraw.
> > > So:
> > >
> > >
> > > CMyComControl::OnDraw(...)
> > > {
> > > ....
> > > CRect rcTextSize = pDC->GetTextExtent( strText, nChars );
> > >
> > >
> > > if( m_sizeNatural != rcTextSize )
> > > {
> > > m_sizeNatural = rcTextSize + some margins
> > > FireViewChange(); //this will alow OnDraw to be called with
> > > correct margins
> > > }
> > > ....
> > >
> > >
> > >
> > > }
> > >
> > >
> > > However in some specific situations the application crashes. I
couldn't
> > >
> > >
> > > figure out why, but I discovered that removing FireViewChange() from
> > > OnDraw solves the crash.
> > > I believe that calling FireViewChange() inside OnDraw() may be tricky,
> > > but this was the only way I could find to calculate m_sizeNatural
> > > function of text extent (DC dependent).
> > >
> > >
> > > Is there any other solutions to my problem?
> > > Any comments on this are welcomed.
> > >
> > >
> > > Thanks
>


From: matanasescu on

Hi AliR,

The container (RichEdit) calls and prepares the DC for
CComControl::OnDraw().
The boundaries of the rectangle inside which I can drawn are dictated
by CComControl::m_sizeNatural.
I can use CComControl::FireViewChange() to force the container to
repaint the control.
Conclusion: how can I set the correct m_sizeNatural before OnDraw
(which I can force with FireViewChange())

I would like to know how to use EM_FORMATRANGE in this scenario. I'm
using RTF.


AliR wrote:
> You can get a dc anytime by calling GetDC(). Don't forget to call ReleaseDC
> when you are done with the DC.
>
> void CMyRichEdit::GetTextExtent(CString Text)
> {
> CDC *pDC = GetDC();
>
> ....
>
> ReleaseDC(pDC);
> }
>
> By the way a better way to get the text boundaries for a rich edit control
> is to use EM_FORMATRANGE. requires a bit more work, but if you are using RTF
> it will be very accurate.
>
> AliR.
>
> <matanasescu(a)gmail.com> wrote in message
> news:1161023169.185547.317680(a)m73g2000cwd.googlegroups.com...
> > Hi Ajay,
> >
> > I need the text extent. I can calculate the text extent only when I
> > receive the DC to draw on. That's in OnDraw.
> >
> >
> > Ajay Kalra wrote:
> > > Change the size of control when its text changes and not in OnDraw.
> > > OnDraw can be called many many times without any changes in the text.
> > >
> > > ---
> > > Ajay
> > >
> > > matanase...(a)gmail.com wrote:
> > > > Hi,
> > > >
> > > > I have an ATL control used to display text.
> > > > The control lives inside a CRichEditControl.
> > > > The problem is that the application needs to resize the control to fit
> > > > the text.
> > > > O solution I adopted is to call FieViewChange() inside of OnDraw,
> after
> > > >
> > > >
> > > >
> > > > I calculate the text extent using DC passed as parameter
> > > > (CDC::GetTextExtent).
> > > > This allows the control to be redrawn inside the boundaries I specify
> > > > in the prev call of OnDraw.
> > > > So:
> > > >
> > > >
> > > > CMyComControl::OnDraw(...)
> > > > {
> > > > ....
> > > > CRect rcTextSize = pDC->GetTextExtent( strText, nChars );
> > > >
> > > >
> > > > if( m_sizeNatural != rcTextSize )
> > > > {
> > > > m_sizeNatural = rcTextSize + some margins
> > > > FireViewChange(); //this will alow OnDraw to be called with
> > > > correct margins
> > > > }
> > > > ....
> > > >
> > > >
> > > >
> > > > }
> > > >
> > > >
> > > > However in some specific situations the application crashes. I
> couldn't
> > > >
> > > >
> > > > figure out why, but I discovered that removing FireViewChange() from
> > > > OnDraw solves the crash.
> > > > I believe that calling FireViewChange() inside OnDraw() may be tricky,
> > > > but this was the only way I could find to calculate m_sizeNatural
> > > > function of text extent (DC dependent).
> > > >
> > > >
> > > > Is there any other solutions to my problem?
> > > > Any comments on this are welcomed.
> > > >
> > > >
> > > > Thanks
> >

 |  Next  |  Last
Pages: 1 2
Prev: _AfxGetNextMnem() changes in MFC 8.0
Next: CProgressDlg