-
May 15th, 2007, 08:56 AM
#1
Automatically Resize CRichEditCtrl and Dialog
Hi,
I have a Dialog, and a CRichEditCtrl inside it. I set a string (which can be multiple lines) to the CRichEditCtrl programmatically (setWindowText).
What I need is, how to make the CRichEditCtrl and Dialog (because the CRichEditCtrl takes up whole space in the Dialog) resize automatically depending on the size of the shown string so that the string fits into CRichEditCtrl nicely without any need of scrollbars.
Thanks
-
May 15th, 2007, 09:47 AM
#2
Re: Automatically Resize CRichEditCtrl and Dialog
APIs ::MoveWindow or::SetWindowPos as well as their wrappers CWnd::MoveWindow or CWnd::SetWindowPos work for any window. You only have to obtain the rect size your string needs to.
Last edited by VictorN; May 15th, 2007 at 10:08 AM.
-
May 15th, 2007, 11:25 PM
#3
Re: Automatically Resize CRichEditCtrl and Dialog
Hmm...thanks, but how to obtain the rect size of the multiline string ?
-
May 16th, 2007, 06:09 AM
#4
Re: Automatically Resize CRichEditCtrl and Dialog
here you go
Code:
void CClientDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
static CRect rectClient; // Client Rectangle
// First child window
CWnd* pWnd = GetWindow(GW_CHILD);
// Runs loop to take into account of every child window
while (pWnd)
{
// Gets control rectangle in window cordinates with respect to dialog
CRect rectCtrl;
pWnd->GetWindowRect(rectCtrl);
ScreenToClient(rectCtrl);
// Calculates scale factors for resizing
double xScale = 1.0 * cx / rectClient.Width();
double yScale = 1.0 * cy / rectClient.Height();
// Calculates scaled control size
rectCtrl.left = (int) (rectCtrl.left * xScale);
rectCtrl.top = (int) (rectCtrl.top * yScale);
rectCtrl.right = (int) (rectCtrl.right * xScale);
rectCtrl.bottom = (int) (rectCtrl.bottom * yScale);
// Sizes the control
pWnd->MoveWindow(rectCtrl);
// Searches for next child window
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
// Stores client rectangle size for next sizing
rectClient = CRect(0, 0, cx, cy);
// TODO: Add your message handler code here
}
You just implement the OnSize handler, and this is what it should look like.
now keep putting as many controls as u want on it.
Enjoyy
If you found my reply to be useful, please dont hesitate to rate it.
DO NOT kick the Axe if it doesnt fall on your foot.
Salman
-
May 16th, 2007, 06:33 AM
#5
Re: Automatically Resize CRichEditCtrl and Dialog
Also take a look at EasySize .
-
May 30th, 2007, 10:50 PM
#6
Re: Automatically Resize CRichEditCtrl and Dialog
Hmm...thanks guy, maybe I should say like this :
given font settings and a string :"Space, the Final Frontier.\nThese are the voyages of starship Enterprise...". Then, the Richedit together with Dialog will resize according to the total size of the string. Maybe what I need is, how to calculcate total size (in pixels ?) of the string ?
-
May 31st, 2007, 02:44 AM
#7
Re: Automatically Resize CRichEditCtrl and Dialog
Have you tried the CDC:: DrawText (or API DrawText) with DT_CALCRECT flag?
-
June 10th, 2007, 07:56 AM
#8
Re: Automatically Resize CRichEditCtrl and Dialog
Thanks, that DrawText and MoveWindow do the trick
And, I have another question...my program is running in the background...How can I get the topleft coordinate and width/height of the current active window ?
Using GetSystemMetrics I can only get the width/height of the whole screen, but not the active window...
Thanks
-
June 10th, 2007, 08:12 AM
#9
Re: Automatically Resize CRichEditCtrl and Dialog
use ::GetActiveWindow() to get the handle of your currently active Window and can use GetWindowRect() method
Thanx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|