Link to home
Start Free TrialLog in
Avatar of jiada
jiada

asked on

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

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 :)
Avatar of eetay
eetay

Hello.

The code would be something like:

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

-- Eetay
Create(
    "MyButtonCaption",
    WS_CHILD|WS_VISIBLE,
    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
    );
Avatar of jiada

ASKER

Adjusted points to 50
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;
      m_PB_FIRST.GetWindowRect(&rOriginalButton);
      lButtonHeight = (rOriginalButton.bottom - rOriginalButton.top);

      CButton* pButton = new CButton;

      RECT rDialog;
      this->GetWindowRect(&rDialog);
      RECT rNewButton;
      m_PB_FIRST.GetWindowRect(&rNewButton);

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

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

            pButton->SetFont( pFont);

            pButton->ShowWindow( SW_NORMAL);

      }

sorry :o), the m_PB_OOPS up there is supposed to be m_PB_FIRST, (my fat fingers)
screen size can then be calculated using:

            SetWindowPos(      &wndTopMost,
                                    rDialog.left,
                                    rDialog.top,
                                    rDialog.right - rDialog.left,
                                    rDialog.bottom - rDialog.top + rNewButton.bottom - rNewButton.top,
                                    SWP_SHOWWINDOW);

Avatar of jiada

ASKER

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.


ASKER CERTIFIED SOLUTION
Avatar of Extreme
Extreme

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jiada

ASKER

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 jiada@hotmail.com by July 23, or I will award the points to Extreme.