【问题标题】:How can I make integer to display in window?如何使整数显示在窗口中?
【发布时间】:2014-02-03 00:21:16
【问题描述】:

我想在窗口中有类似的东西:

int a;
cout<<a;

但我不知道我该怎么做。一开始,我想在屏幕上显示数字,并有一个添加 +1 的按钮和另一个添加 -1 到这个数字的按钮。我希望在没有下一次编译的情况下更新这个数字。你知道我该怎么做吗?

我希望它是一个简单计算器的原型。

【问题讨论】:

  • 你在问如何创建一个 GUI 应用程序。那太宽泛了。阅读 Petzold 的书。

标签: winapi calculator


【解决方案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;

用户点击您的按钮后,您可以在 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

祝你好运和最好的问候!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2014-05-12
    相关资源
    最近更新 更多