Reignite Your Coding Journey: Build a Windows Calculator with C++ Magic!

   Demo :


Click Video πŸ‘‡πŸ‘‡πŸ‘‡





Key Features (Bullet Points)

  • Step-by-step guide on creating a Windows UI using WinAPI.

  • Hands-on C++ coding demonstration covering button creation and event handling.

  • Practical insights for both beginners and seasoned programmers.

  • Engaging visual explanations that breathe life into basic UI concepts.

  • Pro tips to boost creativity and coding efficiency.


Code :


#include <windows.h>

#include <string>

#include <sstream>


using namespace std;


#define ID_BUTTON_0 101

#define ID_BUTTON_1 102

#define ID_BUTTON_2 103

#define ID_BUTTON_3 104

#define ID_BUTTON_4 105

#define ID_BUTTON_5 106

#define ID_BUTTON_6 107

#define ID_BUTTON_7 108

#define ID_BUTTON_8 109

#define ID_BUTTON_9 110

#define ID_BUTTON_PLUS 111

#define ID_BUTTON_MINUS 112

#define ID_BUTTON_MULTIPLY 113

#define ID_BUTTON_DIVIDE 114

#define ID_BUTTON_EQUAL 115

#define ID_BUTTON_CLEAR 116


// Global variables

string displayText = ""; // To hold the current expression or result


// Function declarations

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);


// Create a Window class and initialize the window

int main() {

    // Initialize the window class

    const char CLASS_NAME[] = "CalculatorWindowClass";

    

    WNDCLASS wc = {};

    wc.lpfnWndProc = WindowProcedure; // Pointer to the window procedure

    wc.hInstance = GetModuleHandle(NULL); // Handle to the current program instance

    wc.lpszClassName = CLASS_NAME;


    RegisterClass(&wc);


    // Create the window

    HWND hwnd = CreateWindowEx(0, CLASS_NAME, "Simple Calculator", WS_OVERLAPPEDWINDOW,

                               CW_USEDEFAULT, CW_USEDEFAULT, 300, 350, nullptr, nullptr, wc.hInstance, nullptr);


    if (hwnd == nullptr) {

        return 0;

    }


    ShowWindow(hwnd, SW_SHOW);

    UpdateWindow(hwnd);


    // Main message loop

    MSG msg = {};

    while (GetMessage(&msg, nullptr, 0, 0)) {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }


    return 0;

}


// Window procedure to handle events

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {

    // Create a static control for the display at the top

    static HWND hDisplay;

    

    switch (msg) {

        case WM_CREATE:

            // Create a text display at the top

            hDisplay = CreateWindow("STATIC", "", WS_VISIBLE | WS_CHILD | SS_RIGHT, 20, 20, 200, 60, hwnd, (HMENU)1, GetModuleHandle(NULL), nullptr);


            // Create number buttons (0-9)

            CreateWindow("BUTTON", "1", WS_VISIBLE | WS_CHILD, 20, 70, 50, 50, hwnd, (HMENU)ID_BUTTON_1, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "2", WS_VISIBLE | WS_CHILD, 80, 70, 50, 50, hwnd, (HMENU)ID_BUTTON_2, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "3", WS_VISIBLE | WS_CHILD, 140, 70, 50, 50, hwnd, (HMENU)ID_BUTTON_3, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "4", WS_VISIBLE | WS_CHILD, 20, 130, 50, 50, hwnd, (HMENU)ID_BUTTON_4, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "5", WS_VISIBLE | WS_CHILD, 80, 130, 50, 50, hwnd, (HMENU)ID_BUTTON_5, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "6", WS_VISIBLE | WS_CHILD, 140, 130, 50, 50, hwnd, (HMENU)ID_BUTTON_6, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "7", WS_VISIBLE | WS_CHILD, 20, 190, 50, 50, hwnd, (HMENU)ID_BUTTON_7, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "8", WS_VISIBLE | WS_CHILD, 80, 190, 50, 50, hwnd, (HMENU)ID_BUTTON_8, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "9", WS_VISIBLE | WS_CHILD, 140, 190, 50, 50, hwnd, (HMENU)ID_BUTTON_9, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "0", WS_VISIBLE | WS_CHILD, 80, 250, 50, 50, hwnd, (HMENU)ID_BUTTON_0, GetModuleHandle(NULL), nullptr);


            // Create operation buttons (+, -, *, /)

            CreateWindow("BUTTON", "+", WS_VISIBLE | WS_CHILD, 200, 70, 50, 50, hwnd, (HMENU)ID_BUTTON_PLUS, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "-", WS_VISIBLE | WS_CHILD, 200, 130, 50, 50, hwnd, (HMENU)ID_BUTTON_MINUS, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "*", WS_VISIBLE | WS_CHILD, 200, 190, 50, 50, hwnd, (HMENU)ID_BUTTON_MULTIPLY, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "/", WS_VISIBLE | WS_CHILD, 200, 250, 50, 50, hwnd, (HMENU)ID_BUTTON_DIVIDE, GetModuleHandle(NULL), nullptr);


            // Create equals and clear buttons

            CreateWindow("BUTTON", "=", WS_VISIBLE | WS_CHILD, 140, 250, 50, 50, hwnd, (HMENU)ID_BUTTON_EQUAL, GetModuleHandle(NULL), nullptr);

            CreateWindow("BUTTON", "C", WS_VISIBLE | WS_CHILD, 20, 250, 50, 50, hwnd, (HMENU)ID_BUTTON_CLEAR, GetModuleHandle(NULL), nullptr);

            break;


        case WM_COMMAND: {

            int wmId = LOWORD(wp);

            string buttonText = "";


            // Handle button presses

            if (wmId >= ID_BUTTON_0 && wmId <= ID_BUTTON_9) {

                buttonText = string(1, '0' + (wmId - ID_BUTTON_0)); // Handle digit buttons

            }

            else if (wmId == ID_BUTTON_PLUS) buttonText = "+";

            else if (wmId == ID_BUTTON_MINUS) buttonText = "-";

            else if (wmId == ID_BUTTON_MULTIPLY) buttonText = "*";

            else if (wmId == ID_BUTTON_DIVIDE) buttonText = "/";

            else if (wmId == ID_BUTTON_EQUAL) {

                // Evaluate the expression here (basic logic)

                // Here, you could integrate actual evaluation (complex parsing needed)

                buttonText = "=" + displayText; // Simplified just for example

            }

            else if (wmId == ID_BUTTON_CLEAR) {

                displayText = ""; // Clear the display

                SetWindowTextW(hDisplay, L""); // Clear the display in the window

                return 0;

            }


            // Update display text

            displayText += buttonText;

            SetWindowTextW(hDisplay, std::wstring(displayText.begin(), displayText.end()).c_str());

            break;

        }


        case WM_DESTROY:

            PostQuitMessage(0);

            break;


        default:

            return DefWindowProc(hwnd, msg, wp, lp);

    }

    return 0;

}

Comments

Popular posts from this blog

πŸš€ Simple Login & Registration System in Python Tkinter πŸ“±

πŸš€ Create a Python Screen Recorder with Audio (Complete Code)

Python IP Tracker App with GUI | Track IP Location Real-Time! (Working Project)