Visual C ++ Win32 C ++应用程序。如何将变量打印到主屏幕?

时间:2018-07-19 12:59:00

标签: variables visual-c++ printing

我尝试过使用C ++ 11方法和C方法在打印之前转换字符串,它们要么:返回相同字符的字符串,要么根本不打印任何内容。我只是想真正了解三件事:在线教程和示例中是否存在错误?我对代码的解释和实施是否存在错误?我可以做些什么才能使它正常工作?

// Yu-Gi-Oh! LP Calculator.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Yu-Gi-Oh! LP Calculator.h"

#define MAX_LOADSTRING  100
#define LP              UINT
#define ID              CHAR

// Global Variables:
HINSTANCE hInst;                            // current instance
WCHAR szTitle[MAX_LOADSTRING];              // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];        // the main window class name

// Global Life Point Variables
struct Player {
    LP  Lp;
    ID  Name[MAX_LOADSTRING];
};
struct Player1, Player2, Player3, Player4;

// Forward declarations of functions included in this code module:
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    // TEMP: Remove these initializations when preferences are implemented.
    Player1.Lp = 8000;
    Player2.Lp = 8000;
    Player3.Lp = 8000;
    Player4.Lp = 8000;
    LoadStringA(hInstance, (UINT)"John\0", Player1.Name, MAX_LOADSTRING);
    LoadStringA(hInstance, (UINT)"Phil\0", Player2.Name, MAX_LOADSTRING);
    LoadStringA(hInstance, NULL, Player3.Name, MAX_LOADSTRING);
    LoadStringA(hInstance, NULL, Player4.Name, MAX_LOADSTRING);
    // END TEMP
    // TODO: Place code here.
    // TODO: Load preferences file.
    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_YUGIOHLPCALCULATOR, szWindowClass, MAX_LOADSTRING);
    WNDCLASSEXW wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YUGIOHLPCALCULATOR));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_YUGIOHLPCALCULATOR);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance,  MAKEINTRESOURCE(IDI_SMALL));
    RegisterClassExW(&wcex);
    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_YUGIOHLPCALCULATOR));
    MSG msg;
    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
         }
     }

     return (int) msg.wParam;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            RECT rect;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            GetClientRect(hWnd, &rect);
            CHAR b[] = { NULL };
            sprintf((char* const)&b, "%s", Player1.Name);
            DrawTextA(hdc, b, ARRAYSIZE(b), &rect, DT_SINGLELINE | DT_CENTER | DT_TOP);
            //TextOutA(hdc, rect.left, rect.top, s, ARRAYSIZE(Player1.Name));
            SelectClipPath(hdc, RGN_AND);
            EndPaint(hWnd, &ps);
        }
    break;
    case WM_DESTROY:
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

通过更改:

#define ID              CHAR

收件人:

#define ID              LPCSTR

并进行更改:

ID  Name[MAX_LOADSTRING];

收件人:

ID Name;

我使Player1.Name与DrawTextA函数的第二个参数的类型兼容。 然后,我将第3个参数更改为-1而不是使用ARRAYSIZE()。这意味着该函数检查字符串本身的大小吗?因此,该函数的调用方式如下:

DrawTextA(hdc, Player1.Name, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_TOP);

然后我要做的就是用我的值初始化字符串,而不是使用LoadStringA,所以我进行了更改:

LoadStringA(hInstance, (UINT)"John\0", Player1.Name, MAX_LOADSTRING);

收件人:

Player1.Name = "John";
相关问题