c++ - 标识符 "hWnd"未定义

标签 c++ c windows undefined hwnd

我正在尝试为学校项目创建一个简单的井字游戏。我真的不知道如何使用 Windows,我只想使用 Visual Studio 中默认的“Windows 桌面应用程序”(C++) 来处理其他人的代码,但它不断向我显示以下错误消息:

identifier "hWnd" is undefined

我真的不知道如何使用它(在学校,我们使用一些基本的控制台应用程序)并且我不知道如何定义 hWnd

有人可以帮助我吗?

// Proba2.cpp : Defines the entry point for the application.
//

#include <windows.h>
#include "framework.h"
#include "Proba2.h"


#define MAX_LOADSTRING 100

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

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(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);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_PROBA2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROBA2));

    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;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    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_PROBA2));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_PROBA2);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
   hInst = hInstance; // Store instance handle in our global variable

   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;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
int lepeskoz;
int hatar;
int tabla[10][10];
void kattintas(POINT mouse);
void tabla_kirajzol();
void init();
void bejeloles(int x, int y, int azonosito);
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 


{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_CREATE:
        init();
        return 0;
    case WM_LBUTTONDOWN:
        MousePos.x = LOWORD(lParam);
        MousePos.y = HIWORD(lParam);
        kattintas(MousePos);
        return 0;
    case WM_PAINT:
        tabla_kirajzol();
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;


    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

void kattintas(POINT mouse)
{
    int x_adat = (mouse.x / lepeskoz);
    int y_adat = (mouse.y / lepeskoz);
    if (x_adat < 3 && y_adat < 3)
    {
        if (tabla[y_adat][x_adat] > 0) return;
    }
}

void init()
{
    lepeskoz = (400 - 30) / 3;
    hatar = (4 * lepeskoz) + 5;
}

void bejeloles(int x, int y, int azonosito)
{
    tabla[y][x] = azonosito;
    tabla_kirajzol();
}
void palya()
{
    int i;
    PAINTSTRUCT ps;
    HBRUSH hBrush;
    RECT rect;
    HDC hdc2;
    HPEN vonalstilus;

    {
        vonalstilus = CreatePen(PS_SOLID, 3, RGB(0, 0, 0));

        hdc2 = GetDC(hWnd);
        BeginPaint(hWnd, &ps);
        hBrush = CreateSolidBrush(RGB(204, 204, 204));
        SetRect(&rect, 0, 0, 400, 600);
        FillRect(hdc2, &rect, hBrush);
        EndPaint(hWnd, &ps);

        SelectObject(hdc2, vonalstilus);
        for (i = 5; i < hatar; i += lepeskoz)
        {
            MoveToEx(hdc2, i, 5, NULL);
            LineTo(hdc2, i, hatar - lepeskoz);
        }
        for (i = 5; i < hatar; i += lepeskoz)
        {
            MoveToEx(hdc2, 5, i, NULL);
            LineTo(hdc2, hatar - lepeskoz, i);
        }
        ReleaseDC(hWnd, hdc2);
        DeleteObject(vonalstilus);
    }
}
void tabla_kirajzol()
{
    int x;
    int y;
    int korx, kory, kor_szelesseg;

    PAINTSTRUCT  ps;
    HBRUSH hBrush;
    HDC hdc2;
    HPEN vonalstilus;

    vonalstilus = CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
    hBrush = CreateSolidBrush(RGB(34, 204, 204));

    hdc2 = GetDC(hWnd);
    BeginPaint(hWnd, &ps);
    SelectObject(hdc2, vonalstilus);

    palya();
    for (y = 0; y < 10; ++y)
        for (x = 0; x < 10; ++x)
        {
            korx = x * lepeskoz + 5 + (lepeskoz / 2);
            kory = y * lepeskoz + 5 + (lepeskoz / 2);
            kor_szelesseg = (lepeskoz / 2) - 5;

            if (tabla[y][x] > 0)
            {
                if (tabla[y][x] == 1) {
                    hBrush = CreateSolidBrush(RGB(34, 204, 204)); SelectObject(hdc2, hBrush);
                    Ellipse(hdc2, korx - kor_szelesseg, kory - kor_szelesseg, korx + kor_szelesseg, kory + kor_szelesseg);
                }
                else if (tabla[y][x] == 2) {
                    hBrush = CreateSolidBrush(RGB(204, 204, 34)); SelectObject(hdc2, hBrush);
                    Rectangle(hdc2, korx - kor_szelesseg, kory - kor_szelesseg, korx + kor_szelesseg, kory + kor_szelesseg);
                }

            }
        }
    EndPaint(hWnd, &ps);
    ReleaseDC(hWnd, hdc2);
    DeleteObject(hBrush);
    DeleteObject(vonalstilus);
}

最佳答案

  • bejeloles() 未使用,请将其删除。
  • 您的 void tabla_kirajzol(); 应具有以下签名:void tabla_kirajzol(HWND hWnd); 以便将 hWnd 变量传递给功能。
  • 出于与上述相同的原因,您的 void palya(); 应为 void palya(HWND hWnd);
  • 在消息循环中,执行此操作将 hWnd 传递给 tabla_kirajzol:

    case WM_PAINT:
          tabla_kirajzol(hWnd);
          break;
    

  • tabla_kirajzol中,像这样调用palya:palya(hWnd);

免责声明:以上内容未经测试,因为您没有创建 Minimal, Reproducible Example ,因此可能会有更多错误

关于c++ - 标识符 "hWnd"未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668836/

相关文章:

c# - 在 C# 中使用运行时确定的 C++ DLL

c++ - 查询 Windows 显示缩放

c++ - 处理多线程清理的最佳方式

c - 收集未知数量的消息,然后继续执行程序

C++ 结构定义

c++ - Windows C++ CMD窗口切换

python-3.x - 无法 pip 安装 egenix-mx-base

c++ - 在创建动态二维数组时,A[i * c+ j]中的c有什么用,即c代表什么?

c - C语言中如何计算文件中的空行数?

c++ - 写入 .ini 文件 - SimpleIni SetValue 没有做任何事情,尽管看起来成功了