changing control size and position

Posted on 1998-08-04
Last Modified: 2013-11-19
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?
Question by:marakr

Expert Comment

ID: 1320224

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);

Author Comment

ID: 1320225
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.


Expert Comment

ID: 1320226
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,


Accepted Solution

tvgPrabhu earned 100 total points
ID: 1320227

   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)); = x1;
dc.FillSolidRect (&rcRect, RGB(0,0,255)); = 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,


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 93
Macro to save highlighted rows as a separate file 5 43
modThree challenge 4 89
Work with App store 7 51
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now