我尝试使用WIN32 API通过GUI创建一个简单的击球平均计算器。该程序应该将击球次数和击球次数除以得到击球平均值(公式击球平均值=击球次数/击球次数)。我已经拥有了所有gui元素,我只需要能够从用户那里获得输入并输出答案即可。我也遇到了一个奇怪的问题,即我在文本框中输入文本而文本不可见。
#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
}
答案 0 :(得分:1)
在rotate!
之前缺少break
。
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;
,GetWindowTextLength
,GetWindowText
到编辑控件的读取数值可能很复杂,或者在_wtoi
的情况下更容易(这需要每个控件的标识符,很小数字为GetDlgItemInt
-hMenu
调用中的第9个参数)。