You are here:

C++/how to create a button??

Advertisement


Question
hi
i want to know the source code of the  function that is  used it to create a button in c++ win32 application.
thanx in advance

Answer
here is a simple example for edit control, Button  will also work in the similar way.
class SimpleControl
{
public:
  SimpleControl (HWND hwndParent, int id)
       : _hWnd (GetDlgItem (hwndParent, id))
   {}

   void SetFocus ()
   {
       ::SetFocus (_hWnd);
   }

   HWND Hwnd () const { return _hWnd; }

protected:
   HWND _hWnd;
};
Here's an example of an edit control

class Edit: public SimpleControl
{
public:
   Edit (HWND hwndParent, int id)
       : SimpleControl (hwndParent, id)
   {}

   void SetString (char* buf)
   {
       SendMessage (Hwnd (), WM_SETTEXT, 0, (LPARAM) buf);
   }

   // code is the HIWORD (wParam)
   static BOOL IsChanged (int code)
   {
       return code == EN_CHANGE;
   }

   int GetLen ()
   {
       return SendMessage (Hwnd (), WM_GETTEXTLENGTH, 0, 0);
   }

   void GetString (char* buf, int len)
   {
       SendMessage (Hwnd (), WM_GETTEXT,
         (WPARAM) len, (LPARAM) buf);
   }

   void Select ()
   {
       SendMessage (Hwnd (), EM_SETSEL, 0, -1);
   }
};

This is how the edit control may be used:



class Controller
{
public:
   Controller(HWND hwnd);
   ...
private:
   Edit    _edit;
   char    _string [maxLen];
};

Controller::Controller (HWND hwnd)
   : _edit (hwnd, IDC_EDIT)
{
   _edit.SetFocus ();
   ...
}

void Controller::Command (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
   switch (LOWORD(wParam))
   {
       case IDC_EDIT:
         if (_edit.IsChanged(HIWORD (wParam)))
         {
         _edit.GetString (_string, maxLen);
         }
         break;
       ...
   }
}

Also, Please go through the web site:
http://www.relisoft.com/win32/controls.html
There is an example to create different kind of controls.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.