您可以在相应的按钮处理程序中进行计算,并使用SetWindowText 消息设置“屏幕”文本。
思路如下:
您有 2 个按钮 - 一个要加,一个要减。您可以像这样在 WM_CREATE 处理程序中创建它们:
case WM_CREATE:
{
HWND btnAdd = CreateWindowEx( 0, L"Button",
L"+1", //this is the text for your adding button
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
50, 150, 150, 25, hWnd, (HMENU)8004, hInst, 0 );
HWND btnSubtract = CreateWindowEx( 0, L"Button",
L"-1", //this is the text for your adding button
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
50, 250, 150, 25, hWnd, (HMENU)8005, hInst, 0 );
// since you want "calculator type" application
// here is your result window-edit control
HWND input = CreateWindowEx( 0, L"Edit",
L"", // no need for text
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL,
50, 450, 150, 25, hWnd, (HMENU)8006, hInst, 0 );
HWND result = CreateWindowEx( 0, L"Edit",
L"", // no need for text
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY | ES_AUTOHSCROLL,
50, 450, 150, 25, hWnd, (HMENU)8007, hInst, 0 );
// other stuff
}
return 0L;
用户点击您的按钮后,您可以在 WM_COMMAND 处理程序中使用 SetWindowText 设置 result 编辑控件的文本,如下所示:
case 8004: // add 1 to the number
{
// get the number from input edit control
wchar_t temp[10];
GetWindowText( GetDlgItem( hWnd, 8006 ), temp, 10 );
//convert text to nubmer
int InputedNumber = wtoi( temp );
// show the result in the result edit control
memset( temp, L'\0', sizeof(temp) ); //reuse variable to save memory
swprintf_s( temp, 10, L"%d", InputNumber+1 ); //convert result to text
SetWindowText( GetDlgItem( hWnd, 8007 ), temp ); //show the result
}
case 8005: // subtract 1 to the number
{
// get the number from input edit control
wchar_t temp[10];
GetWindowText( GetDlgItem( hWnd, 8006 ), temp, 10 );
//convert text to number
int InputedNumber = wtoi( temp );
// show the result in the result edit control
memset( temp, L'\0', sizeof(temp) ); //reuse variable to save memory
swprintf_s( temp, 10, L"%d", InputNumber-1 ); //convert result to text
SetWindowText( GetDlgItem( hWnd, 8007 ), temp ); //show the result
}
以上是C++ 的相关代码sn-ps。
这对你来说可能是一个大问题,所以我建议你通过this beginner tutorial。
祝你好运和最好的问候!