I am trying to create radio buttons in Dev-C++ using C. I am new to Win32 API programming also. What I have done so far is create the main app window, added textboxes, check boxes and a few radio buttons. The only issue I have now is that You cannot select the radio buttons during runtime. I am just wonder what I am missing to make the radio buttons work.

main.cpp

#include <windows.h>
#include <stdio.h>
#include "resource.h"

char ConvertTime[8], ETime[8], UKTime[8], HKTime[8], Hour[8], Min[8], AMPM[8];

int intHour;

HWND hWndConvertTime, hWndETTime, hWndUKTime, hWndHKTime, hWndBetween1, 
       hWndBetween2, hWndETSelect, hWndUKSelect, hWndHKSelect;

HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
{
  DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLS_DLG), hWnd,
              reinterpret_cast<DLGPROC>(DlgProc));

  hInst = hInstance;

  return 0;
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  hWndConvertTime = GetDlgItem(hWndDlg, IDC_CONVERT_TIME);
  hWndETTime = GetDlgItem(hWndDlg, IDC_EASTERN_TIME);
  hWndUKTime = GetDlgItem(hWndDlg, IDC_UK_TIME);
  hWndHKTime = GetDlgItem(hWndDlg, IDC_HK_TIME);
  hWndBetween1 = GetDlgItem(hWndDlg, IDC_BETWEEN1);
  hWndBetween2 = GetDlgItem(hWndDlg, IDC_BETWEEN2);
  hWndETSelect = GetDlgItem(hWndDlg, IDC_EASTERN_SELECT);
  hWndUKSelect = GetDlgItem(hWndDlg, IDC_UK_SELECT);
  hWndHKSelect = GetDlgItem(hWndDlg, IDC_HK_SELECT);

  switch(Msg)
  {
    case WM_INITDIALOG:
      SetWindowText(hWndConvertTime, "");
      SetWindowText(hWndETTime, "");
      SetWindowText(hWndHKTime, "");
      SetWindowText(hWndUKTime, "");
    return TRUE;

    case WM_COMMAND:
      switch(wParam)
      {
        case IDC_CONVERT:
          // Gets the user entered time and converts it to usable format
          GetWindowText(hWndConvertTime, ConvertTime, 9);
          strncpy(Hour, ConvertTime, 2);
          strncpy(Min, ConvertTime + 3, 2);
          strncpy(AMPM, ConvertTime + 6, 2);
          intHour = atoi(Hour);          

         if (strchr(ConvertTime, 'A') == NULL || strchr(ConvertTime, 'P') == NULL ||
            strchr(ConvertTime, 'a') == NULL || strchr(ConvertTime, 'p') == NULL)
          {
            // converting to 24hr format
            if (strchr(AMPM, 'P') != NULL && strchr(AMPM, 'p') != NULL)
            {
              if (intHour != 12)
              {
                intHour = intHour + 12;
              }
            }
            else if (strchr(AMPM, 'A') != NULL && strchr(AMPM, 'a') != NULL)
            {
              if (intHour == 12)
              {
                intHour = 0;
              }
            }
          }

          // Compares and Calculations
          if (IsDlgButtonChecked(hWnd, IDC_EASTERN_SELECT) == BST_CHECKED)
          {
          }

          // ET Hour formating then printing to label
          sprintf(Hour, "%i", intHour);

          // UK Hour formating then printing to label
          sprintf(Hour, "%i", intHour);

          // HK Hour formating then printing to label
          sprintf(Hour, "%i", intHour);

        return TRUE;

        case IDCANCEL:
          EndDialog(hWndDlg, 0);
        return TRUE;
      }
    break;
  }

  return FALSE;
}

resource.h

#define IDD_CONTROLS_DLG                101
#define IDC_CONVERT_TIME                1002
#define IDC_EASTERN_TIME                1003
#define IDC_UK_TIME                     1004
#define IDC_HK_TIME                     1005
#define IDC_CONVERT                     1006
#define IDC_BETWEEN1                    1007
#define IDC_BETWEEN2                    1008
#define IDC_EASTERN_SELECT              1009
#define IDC_UK_SELECT                   1010
#define IDC_HK_SELECT                   1011

GUI.rc

#include "resource.h"
#include <afxres.h>

IDD_CONTROLS_DLG DIALOG DISCARDABLE  0, 0, 160, 230
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "HSBC Conversion Clock"
FONT 8, "MS Sans Serif"
BEGIN
    LTEXT           "Conversion Time: ",IDC_STATIC,20,20,80,8
    EDITTEXT        IDC_CONVERT_TIME,100,20,40,12,ES_RIGHT | ES_AUTOHSCROLL
    CHECKBOX        "Between 3/8 - 3/29?", IDC_BETWEEN1, 40, 40, 100, 8, BS_AUTOCHECKBOX
    CHECKBOX        "Between 10/25 - 11/1?" IDC_BETWEEN2, 40, 60, 100, 8, BS_AUTOCHECKBOX
    RADIOBUTTON     "Eastern Time", IDC_EASTERN_SELECT, 40, 80, 100, 8, BS_RADIOBUTTON | WS_GROUP 
    RADIOBUTTON     "UK Time", IDC_UK_SELECT, 40, 100, 100, 8, BS_RADIOBUTTON
    RADIOBUTTON     "HK Time", IDC_HK_SELECT, 40, 120, 100, 8, BS_RADIOBUTTON
    LTEXT           "Eastern Time: ",IDC_STATIC,20,140,80,8
    EDITTEXT        IDC_EASTERN_TIME,100,140,40,12,ES_RIGHT | ES_AUTOHSCROLL
    LTEXT           "UK Time: ",IDC_STATIC,20,160,80,8
    EDITTEXT        IDC_UK_TIME,100,160,40,12,ES_RIGHT | ES_AUTOHSCROLL
    LTEXT           "HK Time: ",IDC_STATIC,20,180,80,8
    EDITTEXT        IDC_HK_TIME,100,180,40,12,ES_RIGHT | ES_AUTOHSCROLL
    PUSHBUTTON      "Convert",IDC_CONVERT,25,200,30,15
    PUSHBUTTON      "Quit",IDCANCEL,100,200,30,15
END

Recommended Answers

All 8 Replies

>>#include <afxres.h>

That header file is only useful in Microsoft MFC programs. For Dev-C++ you might as well delete it.

I would start by changing your radio resource styles from BS_RADIOBUTTON to BS_AUTORADIOBUTTON. The BS_RADIOBUTTON style requires you to do more of the work yourself.

>>#include <afxres.h>

That header file is only useful in Microsoft MFC programs. For Dev-C++ you might as well delete it.

If I remove "#include <afxres.h>" it gives syntax errors in the rest of the resource.h file.

I would start by changing your radio resource styles from BS_RADIOBUTTON to BS_AUTORADIOBUTTON. The BS_RADIOBUTTON style requires you to do more of the work yourself.

I have changed it to BS_AUTORADIOBUTTON but now all of the Radio Buttons have disappeared.

I have changed it to BS_AUTORADIOBUTTON but now all of the Radio Buttons have disappeared.

Eh?:confused:
You should only change BS_RADIOBUTTON to BS_AUTORADIOBUTTON, not RADIOBUTTON to AUTORADIOBUTTON. Did you change that by mistake?

Try changing the relevant part of the rc file to this:

CONTROL "Eastern Time", IDC_EASTERN_SELECT,
        "button", BS_AUTORADIOBUTTON | WS_GROUP,
        40, 80, 100, 8

    CONTROL "UK Time", IDC_UK_SELECT,
        "button", BS_AUTORADIOBUTTON,
        40, 100, 100, 8

    CONTROL "HK Time", IDC_HK_SELECT,
        "button", BS_AUTORADIOBUTTON,
        40, 120, 100, 8

Set the initial radio button while handling WM_INITDIALOG:

// Init radio button group.
    CheckRadioButton (hWndDlg,
                      // First and last of consecutive list
                      // of radio button id's.
                      IDC_EASTERN_SELECT, IDC_HK_SELECT,
                      // Id to set; all others are reset.
                      IDC_EASTERN_SELECT);

Eh?
You should only change BS_RADIOBUTTON to BS_AUTORADIOBUTTON, not RADIOBUTTON to AUTORADIOBUTTON. Did you change that by mistake?

This is what the GUI.rc file looks like now..

#include "resource.h"
#include <afxres.h>

IDD_CONTROLS_DLG DIALOG DISCARDABLE  0, 0, 160, 230
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "HSBC Conversion Clock"
FONT 8, "MS Sans Serif"
BEGIN
    LTEXT           "Conversion Time: ",IDC_STATIC,20,20,80,8
    EDITTEXT        IDC_CONVERT_TIME,100,20,40,12,ES_RIGHT | ES_AUTOHSCROLL
    CHECKBOX        "Between 3/8 - 3/29?", IDC_BETWEEN1, 40, 40, 100, 8, BS_AUTOCHECKBOX
    CHECKBOX        "Between 10/25 - 11/1?" IDC_BETWEEN2, 40, 60, 100, 8, BS_AUTOCHECKBOX
    RADIOBUTTON     "Eastern Time", IDC_EASTERN_SELECT, 40, 80, 100, 8, BS_AUTORADIOBUTTON  | WS_GROUP
    RADIOBUTTON     "UK Time", IDC_UK_SELECT, 40, 100, 100, 8, BS_AUTORADIOBUTTON
    RADIOBUTTON     "HK Time", IDC_HK_SELECT, 40, 120, 100, 8, BS_AUTORADIOBUTTON
    LTEXT           "Eastern Time: ",IDC_STATIC,20,140,80,8
    EDITTEXT        IDC_EASTERN_TIME,100,140,40,12,ES_RIGHT | ES_AUTOHSCROLL
    LTEXT           "UK Time: ",IDC_STATIC,20,160,80,8
    EDITTEXT        IDC_UK_TIME,100,160,40,12,ES_RIGHT | ES_AUTOHSCROLL
    LTEXT           "HK Time: ",IDC_STATIC,20,180,80,8
    EDITTEXT        IDC_HK_TIME,100,180,40,12,ES_RIGHT | ES_AUTOHSCROLL
    PUSHBUTTON      "Convert",IDC_CONVERT,25,200,30,15
    PUSHBUTTON      "Quit",IDCANCEL,100,200,30,15
END

Set the initial radio button while handling WM_INITDIALOG:

Where would I put that code in what I already have.. That is where I get confused with C.

Resource file:

#include <windows.h>
#include "resource.h"

IDD_CONTROLS_DLG DIALOG DISCARDABLE  0, 0, 160, 230

STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "HSBC Conversion Clock"
FONT 8, "MS Sans Serif"

BEGIN
    LTEXT           "Conversion Time: ",
                    -1,
                    20,  20, 80,   8

    EDITTEXT        IDC_CONVERT_TIME,
                    100,  20, 40,  12,    ES_RIGHT | ES_AUTOHSCROLL

    CHECKBOX        "Between 3/8 - 3/29?",
                    IDC_BETWEEN1,
                    40,  40, 100,  8,     BS_AUTOCHECKBOX

    CHECKBOX        "Between 10/25 - 11/1?"
                    IDC_BETWEEN2,
                    40,  60, 100,  8,     BS_AUTOCHECKBOX

    CONTROL         "Eastern Time",
                    IDC_EASTERN_SELECT,
                    "button",             BS_AUTORADIOBUTTON | WS_GROUP,
                    40, 80, 100, 8

    CONTROL         "UK Time",
                    IDC_UK_SELECT,
                    "button",             BS_AUTORADIOBUTTON,
                    40, 100, 100, 8

    CONTROL         "HK Time",
                    IDC_HK_SELECT,
                    "button",             BS_AUTORADIOBUTTON,
                    40, 120, 100, 8

    LTEXT           "Eastern Time: ",
                    -1,
                    20, 140,  80,  8

    EDITTEXT        IDC_EASTERN_TIME,
                    100, 140,  40, 12,    ES_RIGHT | ES_AUTOHSCROLL

    LTEXT           "UK Time: ",
                    -1,
                    20, 160, 80, 8

    EDITTEXT        IDC_UK_TIME,
                    100, 160, 40, 12,     ES_RIGHT | ES_AUTOHSCROLL

    LTEXT           "HK Time: ",
                    -1,
                    20, 180, 80, 8

    EDITTEXT        IDC_HK_TIME,
                    100, 180, 40, 12,     ES_RIGHT | ES_AUTOHSCROLL

    PUSHBUTTON      "Convert",
                    IDC_CONVERT,
                    25, 200, 30, 15

    PUSHBUTTON      "Quit",
                    IDCANCEL,
                    100, 200, 30, 15
END

Code file:

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "resource.h"


#define GETITEM(item) GetDlgItem(hWndDlg, item)


void convert (HWND hWndDlg) {

    char ConvertTime[8], ETime[8], UKTime[8], HKTime[8];
    char Hour[8], Min[8], AMPM[8];
    int intHour;

    // Gets the user entered time and converts it to usable format
    GetWindowText(GETITEM(IDC_CONVERT_TIME), ConvertTime, 9);
    strncpy(Hour, ConvertTime, 2);
    strncpy(Min, ConvertTime + 3, 2);
    strncpy(AMPM, ConvertTime + 6, 2);
    intHour = atoi(Hour);

    if (strchr(ConvertTime, 'A') == NULL ||
        strchr(ConvertTime, 'P') == NULL ||
        strchr(ConvertTime, 'a') == NULL ||
        strchr(ConvertTime, 'p') == NULL)
    {
        // converting to 24hr format
        if (strchr(AMPM, 'P') != NULL && strchr(AMPM, 'p') != NULL)
        {
            if (intHour != 12)
            {
                intHour = intHour + 12;
            }
        }
        else if (strchr(AMPM, 'A') != NULL && strchr(AMPM, 'a') != NULL)
        {
            if (intHour == 12)
            {
                intHour = 0;
            }
        }
    }

    // Compares and Calculations
    if (IsDlgButtonChecked(hWndDlg, IDC_EASTERN_SELECT) == BST_CHECKED)
    {
    }

    // ET Hour formating then printing to label
    sprintf(Hour, "%i", intHour);

    // UK Hour formating then printing to label
    sprintf(Hour, "%i", intHour);

    // HK Hour formating then printing to label
    sprintf(Hour, "%i", intHour);
}


LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
                         WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_INITDIALOG:
        // Init radio button group.
        CheckRadioButton (hWndDlg,
                          // First and last of list of consecutive
                          // radio button id's.
                          IDC_EASTERN_SELECT, IDC_HK_SELECT,
                          // Id to set; all others are reset.
                          IDC_EASTERN_SELECT);
        return TRUE;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDC_CONVERT:
            convert (hWndDlg);
            return TRUE;

        case IDCANCEL:
            EndDialog(hWndDlg, 0);
            return TRUE;
        }
        break;
    }

    return FALSE;
}


int APIENTRY
WinMain (HINSTANCE hInst, HINSTANCE unused, LPSTR cmdLine, int cmdShow)
{
  DialogBox(hInst, MAKEINTRESOURCE(IDD_CONTROLS_DLG), 0,
            (DLGPROC)(DlgProc));
  return 0;
}

Thanks.. It works now..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.