-2

I used win32 GUI and managed to make some sort of interface after some googling but i don't know how to make a button do something or to make an integer get value from a textbox in the GUI.

This is the main

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

HINSTANCE hInst;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
    }
    return TRUE;

    case WM_CLOSE:
    {
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

This is the resource.rc


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




//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 125, 64
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Numar Factorial"
FONT 8, "Ms Shell Dlg"
{
    EDITTEXT        0, 82, 10, 23, 16, ES_AUTOHSCROLL, WS_EX_LEFT
    LTEXT           "Introdu numarul\r\n", 0, 20, 13, 50, 17, SS_LEFT, WS_EX_LEFT
    PUSHBUTTON      "Confirma\r\n", 0, 40, 34, 45, 14, 0, WS_EX_LEFT
}



//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\\manifest.xml"

Resource.h

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define DLG_MAIN                                100

manifest.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

This is the main GUI It says Factorial number, Insert a number and confirm and i want the program to take a number through that text box and after you hit confirm it returns the factorial of that number. I need to figure out how to make the buttons do things cause i'm a beginner and my project is due in 2 days.Please help.

  • [Programming Windows®, Fifth Edition](https://www.amazon.com/dp/157231995X). – IInspectable Jun 01 '20 at 05:21
  • If you have web coding skills (HTML/JS) then switch to modern GUI, search in google how to use "WebView" or simply use WebUI.me or any similar web technologies based GUI. – Albert Shown Feb 23 '23 at 20:57

1 Answers1

1

You PUSHBUTTON needs a control id. Instead of this:

PUSHBUTTON      "Confirma\r\n", 0, 40, 34, 45, 14, 0, WS_EX_LEFT

This:

PUSHBUTTON      "Confirma\r\n", IDC_BUTTON1, 40, 34, 45, 14, 0, WS_EX_LEFT

Where IDC_BUTTON1 is an id in your resource.h file. (e.g. #define IDC_BUTTON1 101)

Give your edit box similar treatment.

EDITTEXT        IDC_EDIT1, 82, 10, 23, 16, ES_AUTOHSCROLL, WS_EX_LEFT

So that you an use GetDlgItemText to fetch the string in that control:

Then you can handle click events in your dialog handler as follows:

case WM_COMMAND:
{
    switch(LOWORD(wParam))
    {
        case IDC_BUTTON1:
        {
             wchar_t str[200];
             GetDlgItemText(hwndDlg, IDC_EDIT1, str, 200);
             HandleClick(str);
             return TRUE;
        }
    }
}
selbie
  • 100,020
  • 15
  • 103
  • 173