Baffled by CEdit.SetRect and CRect.DeflateRect

Posted on 2002-04-29
Last Modified: 2013-11-20
Baffled by CEdit.SetRect and CRect.DeflateRect

I'm enhancing an MFC dialog app that has overlapping controls. Now I'd like to implement something fancier.  

Here's how it works now ...
mouse over #2 CTreeCtrl control


mouse over #8 CEdit


The glitch is that the left side text in the #8 CEdit isn't visible when the #2 CTreeControl has focus ... it's covered up by the #2 CTreeCtrl.

What I want to implement is having the text in the #8 CEdit window 'auto-fit' when the #2 CTreeCtrl is in the foreground. It would shrink the left margin sufficiently and let auto-wrap keep all the text in view.

I looked over the documentation, and it would seem like CEdit.GetRect + CRect.DeflateRect + CEdit.SetRect would accomplish what I'm looking for.  The vertical scroll bar would be enabled if all the text wouldn't fit into the shrunk CEdit window.

However, I'm getting unexpected behavior. Several of the margins change, instead of just the left.  And the amount of text gets less/truncated. I don't know if I should be normalizing or otherwise doing coordinate transformations ... or if I'm just fouling up.

If you want to tackle this, I've defined a barebones dialog app with the controls and OnToggle defined. It can be downloaded directly at:

It has a member function, OnToggle, that I want to cause the 'margin' of the CEdit to deflate and inflate. The CEdit field is preloaded with a long string with embedded new-lines.  The net result should be the text starting off hidden behind #2 CTreeCtrl, then the toggle deflates the right margin so all the text is visible (and can be seen with the scroll bar), and another toggle inflates the right margin so some of the text is again hidden.

It has the vc6 .dsw, .dsp, and associated files. Note that I'm actually trying to get the #8 Cedit window on the right side, but that would not show up in the 'drawing' above due to proportional fonts.

Question by:l_d_allan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Yechezkel earned 100 total points
ID: 6990444
The Problem:

Quote from MSDN on "CEdit::SetRect":
"If the edit control contains a border, the formatting rectangle is reduced by the size of the border. If you adjust the rectangle returned by the GetRect member function, you must remove the size of the border before you pass the rectangle to SetRect."

Each time you call SetRect Windows shrinks the formatting rectangle by the size of the edit control border, so the formatting rectangle keeps getting smaller.

The Solution:

Quote from MSDN on "Changing the Formatting Rectangle":
"The visibility of an edit control's text is governed by the dimensions of its window rectangle and its formatting rectangle. The window rectangle is the client area of the window containing the edit control. The formatting rectangle is a construct maintained by the system for formatting the text displayed in the window rectangle. When an edit control is first displayed, the two rectangles are identical on the screen."

By default the formatting rectangle is set to the same as the client area of the edit control. So instead of getting the formatting rectangle and adjusting that, get the client rectangle (using GetClientRect), adjust it, and pass that to CEdit::SetRect.

Author Comment

ID: 7000396
Worked very well.  Thanks.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 73
matchUp  challenge 9 126
How to convert MFC::CString to UTF8 wchar_t* 10 387
c++  placing data into a form and an editbox 5 25
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question