-1

I am trying to create a simple Tic-Tac-Toe game for a school project. I don't really know how to work with windows and I just want to work with others' code, using the default "Windows Desktop Application" (C++) in Visual Studio, but it keeps giving me this error message:

identifier "hWnd" is undefined

I don't really know how to work with this (at school, we work with some basic console application stuff) and I don't know how to define hWnd.

Can somebody help me?

// 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);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Visual Studio will tell you what line the error occurs on. Also you may want to look at the Output Tab instead of the Errors List and look at the first error in the output first. Often 1 error causes many others. – drescherjm Jan 09 '20 at 16:59
  • 1
    In `void palya()` there is no `hWnd` variable defined so Visual Studio is correct with this error. You had defined `hWnd` in several functions but they are local to the functions. – drescherjm Jan 09 '20 at 17:02
  • 1
    If you just want to paint a simple Tic Tac Toe game into a window i highly suggest to not bother with the Windows API. There are libraries like SDL or SFML that handle the window creation and the rendering part for you. They are MUCH easier to use and cross platform if you just want to open a window and draw stuff 2D stuff into it. Or even go for Windows Forms if you want to quickly piece together a GUI. Unless youre forced to use the Windows API by your school project youre making it more complicated than it has to be. – Eric Jan 09 '20 at 17:15
  • In general, problems caused when you copy and paste code you don't understand and try to run it do not make for good-quality questions on Stack Overflow. – Govind Parmar Jan 09 '20 at 21:23

1 Answers1

0
  • bejeloles() isn't used, remove it.
  • Your void tabla_kirajzol(); should have this signature: void tabla_kirajzol(HWND hWnd); to enable passing the hWnd variable to the function.
  • Your void palya(); should, for the same reason as above, be void palya(HWND hWnd);
  • In the message loop, do this to pass hWnd to tabla_kirajzol:

    case WM_PAINT:
          tabla_kirajzol(hWnd);
          break;
    
  • In tabla_kirajzol, call palya like this: palya(hWnd);

Disclaimer: The above is untested because you didn't make a Minimal, Reproducible Example, so there could be more errors

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108