Prev: _AfxGetNextMnem() changes in MFC 8.0
Next: CProgressDlg
From: matanasescu on 16 Oct 2006 14:17 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 16 Oct 2006 14:21 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 16 Oct 2006 14:26 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 16 Oct 2006 14:29 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 16 Oct 2006 15:01
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 > > |