我希望在窗口中有这样的内容:
int a;
cout<<a;
但我不知道如何做到这一点。一开始,我希望在屏幕上显示数字,并且有一个添加+1的按钮和另一个按钮,该按钮将-1添加到此数字。我希望在没有下一次编译的情况下更新此号码。你知道我怎么做吗?
我希望它成为简单计算器的原型。
答案 0 :(得分:1)
您可以在相应的按钮处理程序中进行计算,并使用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;
用户点击按钮后,您可以在result
处理程序中使用SetWindowText
设置WM_COMMAND
编辑控件的文字:
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++
的相关代码段。
这对您来说可能是一个很大的问题,所以我建议您通过this beginner tutorial。
祝你好运和最好的问候!