Solved

CButton

Posted on 2000-02-25
19
669 Views
Last Modified: 2013-11-20
Hi!
I have CDialog. I need show 10 buttons.
How I can in programm, use CButton, show its?
Thanks.
Bye
0
Comment
Question by:gashev
  • 9
  • 5
  • 2
  • +3
19 Comments
 
LVL 4

Expert Comment

by:inpras
ID: 2560500
Hi
What U want to do ?
Simply showing 10 CButton means U can directly put them during design time.
If U want them initially to be invisible then put their prperty as invisible then use ShowWindow to show them..
Hope this helps
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2560574
Hi,

Be bit more clear, so that we can understand Ur prob.

VinExpert
0
 

Author Comment

by:gashev
ID: 2560598
How I can in program dynamically create 10,20,30 buttons use class CButton?
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2560714
Hi,

Have the array of the CButton class as
CButton m_buttons[10];

Then while creating

//For first button
CRect rect(10,10,20,20);//specify the rectangle for each buttons
m_buttons[0].Create("Button1", WS_CHILD|BS_PUSHBUTTON, rect, this, 1111);
m_buttons[0].ShowWindow(SW_SHOW);

//For second button
CRect rect(30,30,50,50);//specify the rectangle for each buttons
m_buttons[1].Create("Button2", WS_CHILD|BS_PUSHBUTTON, rect, this, 1112);
m_buttons[2].ShowWindow(SW_SHOW);

......
so on. U can place this in a loop too with some logic and intelligence.

Try it out.
VinExpert
0
 
LVL 3

Expert Comment

by:akalmani
ID: 2560717
HI
  U can create buttons dynamically. Maintain a array of pointers to CButton class and allocate memory with new CButton() when u require them, make sure to delete them back when u are done.
 Then use the CButton::Create() member function which
This is for one button only like this u have to create them in a for loop or any other method that is suitable for u...

//In the dialog class .cpp file
#define IDC_MYBUTTON1

//In the dialog class .h file
CButton *m_pcButtons[10];

//Allocate memory and call in  the dialog class .cpp file
//Assign the rect with values where u want to place it in a dialog
m_pcButtons[1] = new CButton();
m_pcButtons[1]->Create( "Button1",BS_PUSHBUTTON|WS_VISIBLE,rect,                                                                                                                                                                    this,ID_MYBUTTON1 );
m_pcButtons[1]->ShowWindow(SW_SHOW);

//Delete them after u are done
delete m_pcButtons[];

    Hope this helps u.....
0
 

Author Comment

by:gashev
ID: 2561045
I create in CDialog in resource button and create function
void CExampleDlg::OnButton()
{
      CButton myButton;
      CRect rect(100,100,130,130);
      myButton.Create(.....);
      myButton.ShowWindow(SW_SHOW);
}

Excuse, but no button on the Dialog.
     
0
 

Expert Comment

by:maknight
ID: 2561372
Put your code in CDialog::OnInitDlg.  CDialog::OnButton sounds like you're using the button click notification. Without a button, there is nothing to click <g>.

MAK
0
 
LVL 3

Expert Comment

by:ghimireniraj
ID: 2561455
implement and put the buttons all in the DIALOG at the design of the dialog

In
IninitialUpdate()
{

hide the buttons you dont want to show initially;

}

Show them when needed

Niraj GHimire


0
 

Author Comment

by:gashev
ID: 2562861
You detect code your????

Not Buttons in Dialog.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:gashev
ID: 2562869
You check code your?
0
 

Author Comment

by:gashev
ID: 2572514
Adjusted points to 100
0
 

Author Comment

by:gashev
ID: 2572515
Know you answer?
0
 

Expert Comment

by:maknight
ID: 2572607
Are you using Visual C++? Version?
0
 

Author Comment

by:gashev
ID: 2576998
Yes. Visual C++ 6.0
0
 

Expert Comment

by:maknight
ID: 2577211
Are you creating an entire dialog dynamically, or just the buttons?

If not, use the ClassWizard, Message Maps tab, select the proper class from the drop list, select the dialog class name in the left-hand list and add the WM_INITDIALOG message. Add this message to your project.

You can then use your code above, and it should work. Below is the code for a dialog completely created at run-time.
I called it a "template dialog". Look through it and you may can find what you need.

////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// TEMPLATE DIALOG CONSTRUCTION HEADER SECTION
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
#define SETRECT(rect, l, t, r, b) \
    rect.left = l; \
    rect.top = t; \
    rect.right = r; \
    rect.bottom = b;

// define the control IDs
#define TOTALITEMS              6
#define IDC_STATIC_PDLG_1 104
#define IDC_STATIC_PDLG_2 105
#define IDC_STATIC_PDLG_3 106
#define IDC_STATIC_PDLG_4 107
#define IDC_STATIC_PDLG_5 108

// define the dialog ids
#define KSPC_PRINTING_DLG   101
#define KSPC_CREATING_DLG   102

class CDialogItem
{
public:
      // define the enum with values to match whatever DLGITEMTEMPLATE requires
      DLGITEMTEMPLATE  m_dlgItemTemplate;

      enum                  controltype {BUTTON = 0x0080, EDITCONTROL, STATICTEXT};
      controltype            m_controltype;
      CString                  m_strCaption;

public:
      CDialogItem(enum controltype cType);  // default constructor will fill in default values
      CDialogItem() {};                     // default constructor, not to be called directly
      void InitializeTemplateDialog(enum controltype cType, UINT nID, CRect* prect = NULL, LPCTSTR pszCaption = NULL);
};

class CMyDialogTemplate
{
public:
      DLGTEMPLATE m_dlgTemplate;

      CDialogItem      m_rgDlgItem[TOTALITEMS];      // the 3 controls to be inserted
      CMyDialogTemplate(CString WindowTitle, CString Line1Str, CString Line2Str, CString Line3Str,
            CString Line4Str, CString Line5Str);
      BOOL ShowTemplateDialog(CDialog* dlg);    // build the template and run it
};

//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// TEMPLATE DIALOG CONSTRUCTION CLASSES AND TEMPLATE DIALOG FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////
// this function is the contructor for the template dialog class
//
// Input Variables: WindowTitle - string text to show in the window's title bar
//                  Line1Str - string text to show on line 1 of dialog
//                  Line2Str - string text to show on line 2 of dialog
//                  Line3Str - string text to show on line 3 of dialog
//                  Line4Str - string text to show on line 4 of dialog
//                  Line5Str - string text to show on line 5 of dialog
//
// Output Variable: N/A for constructors
CMyDialogTemplate::CMyDialogTemplate(CString WindowTitle, CString Line1Str, CString Line2Str, CString Line3Str,
                                                       CString Line4Str, CString Line5Str)
{
      // initialize controls and text for dialog to be displayed
      CRect rect(39, 58, 81, 78);       // Cancel button -- CRect(left, top, right, bottom);
      m_rgDlgItem[0].InitializeTemplateDialog(CDialogItem::BUTTON, IDCANCEL, &rect, _T("&Cancel"));
      SETRECT(rect, 1, 5, 120, 14);     // "Printing"
      m_rgDlgItem[1].InitializeTemplateDialog(CDialogItem::STATICTEXT, IDC_STATIC_PDLG_1, &rect, Line1Str);
      SETRECT(rect, 1, 15, 120, 22);    // document name
      m_rgDlgItem[2].InitializeTemplateDialog(CDialogItem::STATICTEXT, IDC_STATIC_PDLG_2, &rect, Line2Str);
      SETRECT(rect, 1, 23, 120, 31);    // "on the"
      m_rgDlgItem[3].InitializeTemplateDialog(CDialogItem::STATICTEXT, IDC_STATIC_PDLG_3, &rect, Line3Str);
      SETRECT(rect, 1, 32, 120, 40);    // printer name
      m_rgDlgItem[4].InitializeTemplateDialog(CDialogItem::STATICTEXT, IDC_STATIC_PDLG_4, &rect, Line4Str);
      SETRECT(rect, 1, 41, 120, 49);    // "Page " #
      m_rgDlgItem[5].InitializeTemplateDialog(CDialogItem::STATICTEXT, IDC_STATIC_PDLG_5, &rect, Line5Str);

      // now initialize the DLGTEMPLATE structure
      m_dlgTemplate.cx = 120;   // 4 horizontal units are the width of one character
    m_dlgTemplate.cy = 80;    // 8 vertical units are the height of one character
      m_dlgTemplate.style = WS_CAPTION | WS_VISIBLE | WS_DLGFRAME | WS_POPUP | DS_MODALFRAME | DS_SETFONT;
      m_dlgTemplate.dwExtendedStyle = 0;
      m_dlgTemplate.x = 0;
      m_dlgTemplate.y = 0;
    m_dlgTemplate.cdit = TOTALITEMS;  // # of dialog items (controls) in the template dialog
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// this function is the main creation and display portion for the template dialog
//
// Input Variables: dlg - pointer to a dialog object to use (from KPPrintClass)
//
// Output Variable: function status return (TRUE- success, FALSE- error)
BOOL CMyDialogTemplate::ShowTemplateDialog(CDialog* dlg)
{
      // set the dialog caption bar text and default font
      WCHAR szBoxCaption[] = L"Printing. . . .";
      WCHAR szFontName[] = L"MS Sans Serif";

      // first convert the control captions to UNICODE
      int            nTotalLength = 0, i = 0;

      // catch all memory exceptions and don't worry about allocation failures
      TRY
          {
            int nBufferSize =  sizeof(DLGTEMPLATE) + (2 * sizeof(WORD))/*menu and class*/ + sizeof(szBoxCaption);
            nBufferSize += sizeof(WORD) + sizeof(szFontName); /* font information*/ +

            nBufferSize = (nBufferSize + 3) & ~3;  // adjust size to make first control DWORD aligned

            for (i = 0; i < TOTALITEMS; i++)
                {
                  int nItemLength = sizeof(DLGITEMTEMPLATE) + 3 * sizeof(WORD);
                  nItemLength += (m_rgDlgItem[i].m_strCaption.GetLength() + 1) * sizeof(WCHAR);

                  if (i != TOTALITEMS -1 )                   // note: the last control does not need extra bytes
                        nItemLength = (nItemLength + 3) & ~3;  // take into account gap so next control is DWORD aligned

                  nBufferSize += nItemLength;
                }

            HLOCAL hLocal = LocalAlloc(LHND, nBufferSize);
            if (hLocal == NULL)
                  AfxThrowMemoryException();

            BYTE*      pBuffer = (BYTE*)LocalLock(hLocal);
            if (pBuffer == NULL)
                {
                  LocalFree(hLocal);
                  AfxThrowMemoryException();
                }

            BYTE*      pdest = pBuffer;
            // transfer DLGTEMPLATE structure to the buffer
            memcpy(pdest, &m_dlgTemplate, sizeof(DLGTEMPLATE));
            pdest += sizeof(DLGTEMPLATE);
            *(WORD*)pdest = 0;           // dialog has no menu
            *(WORD*)(pdest + 1) = 0;     // use default window class
            pdest += 2 * sizeof(WORD);
            memcpy(pdest, szBoxCaption, sizeof(szBoxCaption));
            pdest += sizeof(szBoxCaption);
            *(WORD*)pdest = 8;          // dialog font pt size
            pdest += sizeof(WORD);
            memcpy(pdest, szFontName, sizeof(szFontName));
            pdest += sizeof(szFontName);

            // will now transfer the information for each one of the item templates
            for (i = 0; i < TOTALITEMS; i++)
                {
                  pdest = (BYTE*)(((DWORD)pdest + 3) & ~3);  // make the pointer DWORD aligned
                  memcpy(pdest, (void *)&m_rgDlgItem[i].m_dlgItemTemplate, sizeof(DLGITEMTEMPLATE));
                  pdest += sizeof(DLGITEMTEMPLATE);
                  *(WORD*)pdest = 0xFFFF;  // indicating atom value
                  pdest += sizeof(WORD);
                  *(WORD*)pdest = m_rgDlgItem[i].m_controltype;       // atom value for the control
                  pdest += sizeof(WORD);

                  // transfer the caption even when it is an empty string
                  WCHAR*      pchCaption;
                  int            nChars, nActualChars;

                  nChars = m_rgDlgItem[i].m_strCaption.GetLength() + 1;
                  pchCaption = new WCHAR[nChars];
                  nActualChars = MultiByteToWideChar(CP_ACP, 0, m_rgDlgItem[i].m_strCaption, -1, pchCaption, nChars);
                  ASSERT(nActualChars > 0);
                  memcpy(pdest, pchCaption, nActualChars * sizeof(WCHAR));
                  pdest += nActualChars * sizeof(WCHAR);
                  delete pchCaption;

                  *(WORD*)pdest = 0;  // How many bytes in data for control
                  pdest += sizeof(WORD);
                }
            ASSERT(pdest - pBuffer == nBufferSize); // just make sure we did not overrun the heap

            // now, create, and show the template dialog
            dlg->CreateIndirect((DLGTEMPLATE*)pBuffer);
            dlg->ShowWindow(SW_SHOWNORMAL);

            // unlock and free up allocated memory
            LocalUnlock(hLocal);
            LocalFree(hLocal);
          }
      CATCH(CMemoryException, e)
          {
            MessageBox(NULL, _T("Memory allocation for dialog template failed. Aborted!"),
                  _T("Allocation Failure"), MB_ICONEXCLAMATION | MB_OK);
          }
      END_CATCH

    return(TRUE);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// this function is the contructor for the template dialog controls
// this function is used by CMyDialogTemplate::ShowTemplateDialog to handle controls
//
// Input Variables: ctrlType - what type of control is it
//
// Output Variable: N/A for constructors
CDialogItem::CDialogItem(enum CDialogItem::controltype ctrlType)
{
      m_controltype = ctrlType;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// this function will initialize the controls for the template dialog
//
// Input Variables: ctrlType - what type of control is it
//                  nID - control resource id
//                  prect - control size and location
//                  lpszCaption - text to be placed on control (where applicable)
//
// Output Variable: N/A
void CDialogItem::InitializeTemplateDialog(enum controltype ctrltype, UINT nID, CRect* prect, LPCTSTR lpszCaption)
{
      // first fill in the type, location and size of the control
      m_controltype = ctrltype;
      if (prect != NULL)
            {
            // disable warning on conversion from long to short
#pragma warning(disable : 4244)
            m_dlgItemTemplate.x = prect->left;
            m_dlgItemTemplate.y = prect->top;
            m_dlgItemTemplate.cx = prect->Width();
            m_dlgItemTemplate.cy = prect->Height();
#pragma warning(default : 4244)
            }
      else
            {
            m_dlgItemTemplate.x = 0;
            m_dlgItemTemplate.y = 0;
            m_dlgItemTemplate.cx = 10;  // some useless default
            m_dlgItemTemplate.cy = 10;
            }

      m_dlgItemTemplate.dwExtendedStyle = 0;
      m_dlgItemTemplate.id = nID;

      // control styles for different types of controls
      switch(m_controltype)
            {
            case BUTTON:
                  m_dlgItemTemplate.style = WS_CAPTION | WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_PUSHBUTTON;
                  break;
            case STATICTEXT:
                  m_dlgItemTemplate.style = WS_CHILD | WS_VISIBLE | SS_CENTER;
                  break;
            default:
                  ASSERT(FALSE);  // should never get here, anyway.
            }

      m_strCaption = (lpszCaption != NULL)? lpszCaption : _T("");
}
0
 

Author Comment

by:gashev
ID: 2588340
It's all for creation button? I know what need 5-6 strings for creation button.
0
 

Expert Comment

by:maknight
ID: 2588368
This code will create a dynamic dialog as well as some various controls. It should give an idea of how to dynamically create a button, however. You can do it with about 4-8 lines of code inside the OnInitDialog override.
0
 

Accepted Solution

by:
maknight earned 100 total points
ID: 2588432
In your OnInitDlg function, you can create the buttons as follows (I'm showing three):

      myButton[0].Create(_T("Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            CRect(10, 10, 100, 30), this, 101);
      myButton[1].Create(_T("Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            CRect(10, 40, 100, 60), this, 102);
      myButton[2].Create(_T("Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            CRect(10, 70, 100, 90), this, 103);


Either as a GLOBAL or a class member declare your CButton variable:
CButton myButtons[3];
This MUST be GLOBAL or a class member, or it goes out of focus when the OnInitDlg function ends, and therefore your just-created buttons... poof! Disappear.
0
 

Author Comment

by:gashev
ID: 2588861
Thank You!
Bye.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 38
countEvens challenge 2 59
only14 challenge 19 64
Line meaning 9 77
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now