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

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 :)
0
jiada
Asked:
jiada
  • 4
  • 3
  • 2
1 Solution
 
eetayCommented:
Hello.

The code would be something like:

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

-- Eetay
0
 
eetayCommented:
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
    );
0
 
jiadaAuthor Commented:
Adjusted points to 50
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

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

      }

0
 
ExtremeCommented:
sorry :o), the m_PB_OOPS up there is supposed to be m_PB_FIRST, (my fat fingers)
0
 
ExtremeCommented:
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);

0
 
jiadaAuthor Commented:
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.


0
 
ExtremeCommented:
the MoveWindow function:

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


0
 
jiadaAuthor Commented:
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.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now