简体   繁体   English

使用 WIN32 API 的击球平均值计算器

[英]Batting Average Calculator with WIN32 API

I am try to make a simple Batting Average Calculator with a GUI using the WIN32 API.我尝试使用 WIN32 API 使用 GUI 制作一个简单的击球平均值计算器。 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.我已经拥有了所有的 gui 元素,我只需要能够从用户那里获取输入并输出答案。 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 .WM_CREATE之前缺少break

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).从编辑控件中读取数值可以使用GetWindowTextLengthGetWindowText_wtoi变得复杂,或者使用GetDlgItemInt更容易(这需要每个控件的标识符,小数字如hMenu - CreateWindow调用中的第 9 个参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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