Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Win32 API application glitching

I am new to the Win32 API, and I tried making my own calculator, but when I run the code, the edit control starts to glitch, and I can’t see what has been inputted.

I tried adding a button which stores the value 1 into the edit control (where the text of the calculator will be displayed), but whenever I use the button, or try to type directly, the text starts to glitch.

#include <windows.h>
#include <stdlib.h>

#define ONE_BUTTON 1
/*#define TWO_BUTTON 2
#define THREE_BUTTON 3
#define FOUR_BUTTON 4
#define FIVE_BUTTON 5
#define SIX_BUTTON 6
#define SEVEN_BUTTON 7
#define EIGHT_BUTTON 8
#define NINE_BUTTON 9
#define TEN_BUTTON 0*/




void AddControls(HWND);

HWND hOut;

HWND hOne;

LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst , LPSTR args, int ncmdshow)
{
    WNDCLASSW wc = {0};
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"mywindowclass";
    wc.lpfnWndProc = WindowProcedure;
    if(!RegisterClassW(&wc))
        return -1;

    CreateWindowW(L"mywindowclass",L"calculatorGUi",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,500,350,NULL,NULL,NULL,NULL);

    MSG msg = {0};
    while(GetMessage(&msg,NULL,NULL,NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch(msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case ONE_BUTTON:
            char one[50];
            char out[15];
            GetWindowText(hOne,one,50);

            strcpy(out,one);

            SetWindowText(hOut,out);
        }


    case WM_CREATE:
        AddControls(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

void AddControls(HWND hWnd)
{
    hOut = CreateWindowW(L"Edit",(int)NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,2,2,480,106,hWnd,NULL,NULL,NULL);

    hOne= CreateWindowW(L"Button",L"1",WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON|BS_DEFPUSHBUTTON 2,110,98,50,hWnd,(HMENU)ONE_BUTTON,NULL,NULL);

    /*CreateWindowW(L"Button",L"2",WS_VISIBLE|WS_CHILD,102,110,99,50,hWnd,(HMENU)TWO_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"3",WS_VISIBLE|WS_CHILD,202,110,99,50,hWnd,(HMENU)THREE_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"4",WS_VISIBLE|WS_CHILD,302,110,99,50,hWnd,(HMENU)FOUR_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"5",WS_VISIBLE|WS_CHILD,402,110,80,50,hWnd,(HMENU)FIVE_BUTTON,NULL,NULL);

    CreateWindowW(L"Button",L"6",WS_VISIBLE|WS_CHILD,2,165,99,50,hWnd,(HMENU)SIX_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"7",WS_VISIBLE|WS_CHILD,102,165,99,50,hWnd,(HMENU)SEVEN_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"8",WS_VISIBLE|WS_CHILD,202,165,99,50,hWnd,(HMENU)EIGHT_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"+",WS_VISIBLE|WS_CHILD,302,165,99,50,hWnd,(HMENU)NINE_BUTTON,NULL,NULL);
    CreateWindowW(L"Button",L"-",WS_VISIBLE|WS_CHILD,402,165,99,50,hWnd,(HMENU)TEN_BUTTON,NULL,NULL);

    CreateWindowW(L"Button",L"9",WS_VISIBLE|WS_CHILD,2,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"0",WS_VISIBLE|WS_CHILD,102,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"*",WS_VISIBLE|WS_CHILD,202,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"/",WS_VISIBLE|WS_CHILD,302,220,99,50,hWnd,NULL,NULL,NULL);
    CreateWindowW(L"Button",L"=",WS_VISIBLE|WS_CHILD,402,220,99,50,hWnd,NULL,NULL,NULL);*/


}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your WM_COMMAND and ONE_BUTTON cases are both missing break statements, so whenever the Edit sends update notifications, or the button is clicked, your code falls through to call AddControls(), creating more and more child controls on top of the previous child controls.

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch(msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case ONE_BUTTON:
            char one[50];
            char out[15];
            GetWindowText(hOne, one, 50);
            strcpy(out, one);
            SetWindowText(hOut, out);
            break; // <-- add this!
        }
        break; // <-- add this!

    case WM_CREATE:
        AddControls(hWnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break; // <-- add this!

    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }

    return 0; // <-- add this!
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading