• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

changing control size and position

I'm working with Visual C++ 4.0 and Windows95.
I'm having problems on changing controls size and position inside a dialog (exactly with CPropertyPage).
I've coded in order to get the control properties:
      LPRECT rectBmp1;
where IDC_BMP is a rectangle control (picture) present in my dialog and there happened an "unhandled exception".
I want to change the height and "y" position of this picture.  What's the problem on my code and what's the solution?
1 Solution

The error is because you're passing in a bad pointer to the GetWindowRect() call. You must make rectBmp1 point to a RECT structure.

Assuming you're using MFC, your code should be either:
   CRect rectBmp1;
   GetDlgItem(IDC_BMP)->GetWindowRect( &rectBmp1 );

   CRect* pRectBmp1 = new CRect();
   GetDlgItem(IDC_BMP)->GetWindowRect( pRectBmp1);
marakrAuthor Commented:
Thanks bernfarr for your answer but unfortunately it didn't apply to my case.
I've tried to pass all the possible types to GetWindowRect including the ones you've suggested.  
I think the problem is with my control because in my last trial I've added the code:
CWnd* pWnd = GetDlgItem(IDC_STATIC);
      if (IsWindow(*pWnd))

and the result of the if statement was 0.  I can't understand why it happened because my control was created in the graphic editor and also  pWnd was different than NULL.
I'd better explain my situation: I need a bitmap that is a rectangle filled with 3 different colors (like a bottle filled with 3 imiscibles liquids). As an association with percents, the sum of the 3 heights should be 100%. When I click on a button, the percent of each liquid  must change although the sum (total height) remains 100%.  
I thought of dealing with 3 rectangles with differents colors.  Each rectangle would represent one liquid. That's why I'm using this type of control.
Please, help me.

In response to this code snippet:

CWnd* pWnd = GetDlgItem(IDC_STATIC);
if (IsWindow(*pWnd))

The reason that the IsWindow() call returns 0 is because the IDC_STATIC property is not an explicit control identifier. If you notice that when you are designing a dialog box you can add several static text controls and they will ALL have the id of IDC_STATIC.  For this to work with a static text control you must name it something explicit and unique.

As for your first problem, I cannot be sure how your dialog box is laid out but the exception your are experiencing is due to the fact that the call to GetDlgItem() is returning a NULL pointer and you are trying to access the function GetWindowRect from it. My guess is that MFC cannot find the IDC_BMP control on the dialog and retuning NULL.  Check to see if it is spelled exactly the same on both the dialog template and in the code. I have done this before in the past (control was named ID_MY_EDIT and the code used IDC_MY_EDIT).

Hope this helps,


   The reason why the if statement in

CWnd* pWnd = GetDlgItem(IDC_STATIC);
if (IsWindow(*pWnd))

failed is b'cos IsWindow takes a Window handle as its input,
not a CWnd class. You should do

if (IsWindow (pWnd -> GetSafeHwnd ()))

for the check to work as intended. Taking into consideration
what lobrjos had said about IDC_STATIC ids and assigning a
unique id to your control along with the check mentioned above,
your code should work fine.

For your real problem itself, it is my opinion that having some
bmps and stretching and contracting them to make up one
rect would be problematic. Instead, I suggest you draw the
rectangle yourself.

Follow these steps :

1. Create a static control in the area in your dlg/prop page
where this rectangle must appear. Give a unique control id,
like IDC_BMPRECT. Remove the default text "static" from it. If
you wish, you can also select the "Border" property from the
style page for this control.

2. I assume that you have already derived your own class from
CPropertyPage which you add to your prop sheet. If not, do it.

3. Add a new class to your workspace through class wizard. Make
the base class as CStatic. Lets call this class CClrRect, say.

4. Add a member variable CClrRect m_oClrRect, to your prop page

5. Override WM_INITDIALOG for your prop page class and do the

  m_oClrRect.SubclassDlgItem (IDC_BMPRECT, this);

6. In the implementation file of CClrRect class, override
WM_PAINT message.

Here's where you are going to paint your rectangle. I give
here a code snippet that I tried out:

GetClientRect (&rcBmp);

// Dividing rcBmp into 10% (red), 30%(blue) and 60%(green), for // eg.
int x1 = rcBmp.Height() * 0.1, x2 = rcBmp.Height() * 0.3;
CRect rcRect (&rcBmp);

dc.FillSolidRect (&rcRect, RGB(255, 0, 0));
rcRect.top = x1;
dc.FillSolidRect (&rcRect, RGB(0,0,255));
rcRect.top = x2;
dc.FillSolidRect (&rcRect, RGB(0,255,0));

This worked fine. Ofcourse, you have to determine the percentages
dynamically. But that is not very tough. You said, when the
user clicks a button, you are to update the rectangle. So in
the WM_CLICKED handler of your button, you have with you the
percentages that you need. All that you need to do here is save
these percentages, say in some member variables of CClrRect class
and use those values while painting the ClrRect. To force a
paint message to be posted to your static control, you can

m_oClrRect.Invalidate ();

in WM_CLICKED handler of the button.

Hope this solves your problem,

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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now