Solved

Dynamic CStatics in a dialog

Posted on 1998-05-20
4
527 Views
Last Modified: 2013-11-20
I'm porting an app from another system and have a function that adds strings to a stringlist then builds a dialog and displays it to the user (the original app is text based so it just does a setxy() then printf() to do the output). I figured I could do this in a dialog for the MFC version. So I created a dialog with a function to add a string to a stringlist. When it comes to displaying the dialog, I loop through the number of strings in the stringlist and display them (and resize the dialog based on the number of strings I have, 1 string per line in the dialog). Since I don't know up front how many lines of text I have, I assume I should create a CStatic for each string dynamically just before the dialog is created. I've tried doing this in InitDialog but it doesn't show up when the dialog is displayed. Any code samples would be appreciated. Thanks.
0
Comment
Question by:bsimser
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 100 total points
ID: 1313327
Hi, I have just created a dialog based app
and this worked fine
BOOL CStaticsDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      CRect r;
      GetClientRect(&r);
      
      r.bottom=20;
      CStatic      *Text=new CStatic;
      Text->Create( "SomeText",WS_CHILD|WS_VISIBLE,r, this,0x4711/*Unique ID for later reference*/);
      r.top+=20;
      r.bottom+=20;
      Text=new CStatic;
      Text->Create( "Some other Text",WS_CHILD|WS_VISIBLE,r, this,0x4712/*Unique ID for later reference*/);
 

      // Add "About..." menu item to system menu.

      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      CString strAboutMenu;
      strAboutMenu.LoadString(IDS_ABOUTBOX);
      if (!strAboutMenu.IsEmpty())
      {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}
 See followin comment
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1313328
Hi again
as I said I created a Dialog base App from the scratch and in
the OnInitDialog I created dynamicly 2 CStatics with different
strings
The same should should work for you.
In the sample above I don't care about memory leaks
I simple create the CStatics without storing any reference them
you should hold the references to them to delete if your app
terminates
because you know the number of strings you can also
create the CStatics as
CStatic * MyTextStatics=new CStatic[NumberOfStrings];
and create them with
for(int i=0;i<NumberOfStrings;i++)
{
     MyTextStatics[i].Create(String[i],WS_CHILD,WS_VISIBLE,PositionRect,this,BaseId+i)
    CalcNextPosition;
}

hope that helps
    Norbert
0
 
LVL 1

Author Comment

by:bsimser
ID: 1313329
Thanks! I used your second idea of creating an array of CStatics and calling create for each one. Works perfectly.
0
 
LVL 1

Author Comment

by:bsimser
ID: 1313330
Thanks! I used your second idea of creating an array of CStatics and calling create for each one. Works perfectly.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
MFC Dialog 9 51
Smart Camera scanning and reading information 3 114
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 354
Problem to Office 1 15
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. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 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