Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Create controls in run time and arrange it to certain position on screen.

Posted on 1999-06-30
Medium Priority
Last Modified: 2013-11-20
I used to create a control set (eg: buttons) on a screen in run time using Visual Basic ......BUTTON_NAME(index), where index = 0,1,2,.....n , Calculate the screen size and arrange them into columns and rows.

Can somebody tell me how to do that in VC++.
Thx for your time :)
Question by:jiada
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
  • 4
  • 3
  • 2

Expert Comment

ID: 1308433

The code would be something like:

CButton *pButtons = new CButton[10];
for (int i=0; i<10; i++)

-- Eetay

Expert Comment

ID: 1308434
    CRect(...), //the button position
    pParent, //the dialog's CWnd pointer
    i // the index of the 'for' statement, just index number for the button, so i can use GetDlgItem to get the buttons from the dialog if I want

Author Comment

ID: 1308435
Adjusted points to 50
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.


Expert Comment

ID: 1308436
eetay looks good to me, a similar approach I used in an app has one button already on screen (m_PB_FIRST) and each dynamic button added under the last as follows:

// in header
CArray< CWnd*, CWnd*>      m_aButtons;
long lButtonHeight

// in cpp
      RECT rOriginalButton;
      lButtonHeight = (rOriginalButton.bottom -;

      CButton* pButton = new CButton;

      RECT rDialog;
      RECT rNewButton;

      ScreenToClient( &rNewButton);
      long lPos = (m_aButtons.GetSize() +1) * lButtonHeight; += lPos;
      rNewButton.bottom += lPos;

      if( pButton->Create( strName,
                              BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                              BUTTON_START_ID + m_aButtons.GetSize()))
            m_aButtons.Add( pButton);
            CFont* pFont = m_PB_OOPS.GetFont();

            pButton->SetFont( pFont);

            pButton->ShowWindow( SW_NORMAL);



Expert Comment

ID: 1308437
sorry :o), the m_PB_OOPS up there is supposed to be m_PB_FIRST, (my fat fingers)

Expert Comment

ID: 1308438
screen size can then be calculated using:

            SetWindowPos(      &wndTopMost,
                                    rDialog.right - rDialog.left,
                                    rDialog.bottom - + rNewButton.bottom -,


Author Comment

ID: 1308439
Hello everybody. Thanks for all your ideas :)

I believe that the above codes can help us to create controls in run time. but how to change the button's size for different screen resolution in a always-maximized dialog?

eg: if screen size = dialog size = 1000X, 1000Y
    button1 will always set to position 200X 300Y,
        with width 200X and height 100Y;      
    button2 will always set to position 400X 300Y,
        with width 200X and height 100Y;        etc.


Accepted Solution

Extreme earned 240 total points
ID: 1308440
the MoveWindow function:

CWnd* pButton = whatever button you have :o)
RECT rButton;
ScreenToClient( &rButton);
// Calc coordinates for
rButton.bottom = ?; = ?;
rButton.left = ?;
rButton.right = ?;


Author Comment

ID: 1308441
I am newbie in vc++, sorry I don't how to apply your ideas in my code with my poor skill. Could anybody please send to me a sample project which can do all the functions discussed above to by July 23, or I will award the points to Extreme.

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

721 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