简体   繁体   中英

Batting Average Calculator with WIN32 API

I am try to make a simple Batting Average Calculator with a GUI using the WIN32 API. The program is supposed to Take the hits and times at bat and divide them to get the Batting Average(Formula Batting Average = Hits / Times at Bat). I already have all the gui elements I just need to be able to get input from the user and ouput the answer. I am also having this weird problem where I am typing text into the text boxs and the text is invisble.

#include <windows.h>
#define CALC_BUTTON 1

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

HWND hHits,hTimesAtBat,hOut;

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"Pedro's Batting Average Calculator",WS_OVERLAPPEDWINDOW | WS_VISIBLE ,100,100,500,500,NULL,NULL,NULL,NULL);

    MSG msg ={0};

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

    return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch(wp)
        {
        case CALC_BUTTON: 
        //Insert Funtionally here
            break;

        }

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

void AddControls(HWND hWnd)
{
    CreateWindowW(L"Static",L"Hits:",WS_VISIBLE|WS_CHILD,150,70,98,38,hWnd,NULL,NULL,NULL); //Hits Text
    hHits = CreateWindowW(L"Edit",L"",WS_VISIBLE|WS_CHILD|WS_BORDER,200,50,98,38,hWnd,NULL,NULL,NULL); //Enter Hits

    CreateWindowW(L"Static",L"TimesAtBat:",WS_VISIBLE|WS_CHILD,100,110,98,38,hWnd,NULL,NULL,NULL); //Bats Text
    hTimesAtBat = CreateWindowW(L"Edit",L"",WS_VISIBLE|WS_CHILD|WS_BORDER,200,90,98,38,hWnd,NULL,NULL,NULL); //Enter Bats

    CreateWindowW(L"Static",L"Batting Average:",WS_VISIBLE|WS_CHILD,70,140,128,38,hWnd,NULL,NULL,NULL); //Batting Avg
    hOut = CreateWindowW(L"Edit",L"",WS_VISIBLE|WS_CHILD|WS_BORDER,200,130,98,38,hWnd,NULL,NULL,NULL);  //Answer Output

    HWND hBut = CreateWindowW(L"Button",L"Calculate",WS_VISIBLE|WS_CHILD|WS_BORDER,150,190,98,38,hWnd,(HMENU)CALC_BUTTON,NULL,NULL); //Calculate Button
}

Missing break before WM_CREATE .

case WM_COMMAND:
    switch(wp)
    {
    case CALC_BUTTON: 
    //Insert Funtionally here
        break;

    }
    break; // If you omit this, it falls trough to WM_CREATE.
case WM_CREATE:
    AddControls(hWnd);
    break;

Reading numeric values from edit control can be complicated with GetWindowTextLength , GetWindowText , _wtoi , or a lot easier with GetDlgItemInt (this needs identifier for each control, small number as hMenu - 9-th argument in CreateWindow call).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM