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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 92
Best tools for Backup and restore of Wordpress site 6 58
canBalance challenge 34 105
Excel file not created as expected 7 95
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

696 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