Hello,

I am just messin around with this "A Simple Win32 GUI Introduction" snippet I found on this site. I am able to paste it in to a simple project in my Visual C++ application and run it. Basically it is just a blank window. It says you can play around with it, and I did figure out how to change the Title. But what else can we do with this? I have a simple console application that I wrote that calculates two resistors in parallel.

#include <iostream>
using namespace std;

int main()
{
    int R1, R2;
    float Rpar;
    cout << "Enter the value for R1: " ;
    cin >> R1;
    cout << "Enter the value for R2: ";
    cin >> R2;
    Rpar = (R1 * R2) / (R1 + R2);
    cout <<"The value of parallel resistance is: " << Rpar << "\n";
    system ("pause");
    return 0;
    
   }

Here is the Snippet

#include <windows.h> /* Windows header */

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";   /* Class ID */

int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;

        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure; /* See end of code */
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Default windows background colour */

        if (!RegisterClassEx (&wincl))
        return 0;

        hwnd = CreateWindowEx (
        0,
        szClassName,         /* Classname */
        "Windows App",       /* Title Text */
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,       /* Default x... */
        CW_USEDEFAULT,       /* ...and default y position of window */
        640,                 /* The programs width... */
        480,                 /* ...and height in pixels */
        HWND_DESKTOP,
        NULL,
        hThisInstance,
        NULL
        );

        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0))
        {
                TranslateMessage(&messages);
                DispatchMessage(&messages);
        }

        /* Return: wParam from a quit message usually = 0 */
        return messages.wParam;
}

LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ /* Called when window processes a message */
        switch (message)
        {
                case WM_DESTROY: /* Destoy message: called if you press the "x" in the top right corner */
                PostQuitMessage (0); /* Send a message to quit */
                break;
                default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
}

Can this be incorporated into this window, and not disappear after hitting any key?

Just playin around. Oh Yes! How do you change the background color?

any help would be appreciated

ThanX BandM

Dani AI

Generated

A few practical steps to turn the blank Win32 template into a GUI resistor calculator (based on 's snippet and 's suggestion to add edit boxes and a button).

Create two single-line edit controls for R1 and R2, a push button labelled "Calculate", and a static (or read-only edit) for the result. Create the controls in WM_CREATE with CreateWindowEx and give them control IDs; catch the button click in WM_COMMAND, read the edit text with GetDlgItemText (or GetWindowText), convert the strings to float/double, check for zero, compute the parallel resistance, and show the formatted result with SetDlgItemText or SetWindowText. See CreateWindowEx and the button/edit control docs for the API details. (learn.microsoft.com)

Example WM_COMMAND logic (minimal):

// assume IDs: IDC_R1=1001, IDC_R2=1002, IDC_CALC=1003, IDC_OUT=1004
case WM_COMMAND:
  if (LOWORD(wParam) == IDC_CALC) {
    char s1[64], s2[64], out[64];
    GetDlgItemTextA(hwnd, IDC_R1, s1, sizeof s1);
    GetDlgItemTextA(hwnd, IDC_R2, s2, sizeof s2);
    float r1 = (float)strtof(s1, NULL), r2 = (float)strtof(s2, NULL);
    if (r1 <= 0.0f || r2 <= 0.0f) SetDlgItemTextA(hwnd, IDC_OUT, "Invalid input");
    else {
      float rpar = (r1 * r2) / (r1 + r2);
      snprintf(out, sizeof out, "%.4f", rpar);
      SetDlgItemTextA(hwnd, IDC_OUT, out);
    }
  }
  break;

To change the window background set the WNDCLASSEX.hbrBackground before RegisterClassEx: use a system brush via (HBRUSH)(COLOR_WINDOW+1) or GetSysColorBrush, or supply a CreateSolidBrush(RGB(...)) (if creating a brush, track and delete it appropriately). See the WNDCLASSEX docs and SetClassLongPtr notes for correct usage. (learn.microsoft.com)

Troubleshooting tips: declare R1/R2 as float/double to avoid integer division (integer operands produce a truncated integer result). Validate input and guard against divide-by-zero. For faster layout, a dialog resource + DialogBox can simplify control placement; for larger projects consider higher-level frameworks or Visual Studio resource editors (as noted, they move code into separate files). For API details consult the control, text, and message documentation linked above. (cppreference.net)

Recommended Answers

All 2 Replies

Just a hint, on your parallel resistor calculator declare both R1 and R2 as a float or something like:

R1 = 1
R2 = 2

will give you a parallel resistance of 0

BTW, the Windows snippet you have is the one that comes up with DevCpp when you create a Windows Application. You need to add EditBoxes for data entry and output, also a button to click to do your calculations. The easiest way to do all of this is with a form builder in visual studio. The problem there is, that your total code is now scattered across several goofy sounding files.

Plotzki schmotzki!

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.