如何使用类方法作为WIN32应用程序回调方法(WINPROC)...错误静态结构HINSTANCE__

时间:2010-05-23 12:59:40

标签: c++ c visual-studio

我收到错误,同时尝试完成这项工作,请阅读我要说的内容。感谢....

我正在创建一个c ++应用程序,并且大部分应用程序都封装在一个类中。这意味着我的WinProc函数是一个静态类方法,我用作win32应用程序的回调方法。问题是,因为我创建了它作为win32应用程序,我在该方法中使用的每个类成员也必须是静态的。包括我在其中使用的HINSTANCE类。现在我收到此错误

error LNK2001: unresolved external symbol "public: static struct HINSTANCE__

我需要弄清楚如何使这一切工作,下面是我的班级声明。我的静态成员静态HINSTANCE m_hInst导致了我认为的错误。

#pragma once
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100

class RenderEngine {
protected:
    int m_width;
    int m_height;

    ATOM RegisterEngineClass();

public:
    static HINSTANCE m_hInst;   //<------ This is causing the error 
    HWND m_hWnd;
    int m_nCmdShow;
    TCHAR m_szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR m_szWindowClass[MAX_LOADSTRING];          // the main window class name

    bool InitWindow();
    bool InitDirectX();
    bool InitInstance();

    //static functions
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

    int Run();

};

以下是实施

#include "stdafx.h"
#include "RenderEngine.h"

bool RenderEngine::InitWindow()
{
    RenderEngine::m_hInst = NULL;
    // Initialize global strings
    LoadString(m_hInst, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
    LoadString(m_hInst, IDC_RENDERENGINE, m_szWindowClass, MAX_LOADSTRING);

    if(!RegisterEngineClass())
    {
        return false;
    }

    if(!InitInstance())
    {
        return false;
    }

    return true;
}

ATOM RenderEngine::RegisterEngineClass()
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = RenderEngine::WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = m_hInst;
    wcex.hIcon          = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_RENDERENGINE));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_RENDERENGINE);
    wcex.lpszClassName  = m_szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

LRESULT CALLBACK RenderEngine::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(m_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

bool RenderEngine::InitInstance()
{
   m_hWnd = NULL;
   m_hWnd = CreateWindow(m_szWindowClass, m_szTitle, WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, m_hInst, NULL);

   if (!m_hWnd)
   {
      return FALSE;
   }

   if(!ShowWindow(m_hWnd, m_nCmdShow))
   {
        return false;
   }
   UpdateWindow(m_hWnd);

   return true;
}

// Message handler for about box.
INT_PTR CALLBACK RenderEngine::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;
}

int RenderEngine::Run()
{
    MSG msg;
    HACCEL hAccelTable;

    hAccelTable = LoadAccelerators(m_hInst, MAKEINTRESOURCE(IDC_RENDERENGINE));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

及以下是WinMain中使用的代码

RenderEngine go;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    RenderEngine::m_hInst = hInstance;
    go.m_nCmdShow = nCmdShow;
    if(!go.InitWindow())
    {
        return 0;
    }
        go.Run();

    return 0;
}

如果什么都没有任何意义,那么我道歉,我是个新手。谢谢你的帮助!!

1 个答案:

答案 0 :(得分:2)

您在头文件中有一个静态变量的声明,但在任何地方都没有定义

在对象(cpp)文件中的某个位置,您需要在全局范围内为变量“分配”内存:

HINSTANCE RenderEngine::m_hInst = NULL;

尝试将其放在InitWindow实施之前。

静态变量与正常变量相反,即使没有分配其持有者类型的类,也可以从程序的开头存在。因此,您需要为编译器指向一个位置,就像在使用extern时需要为全局变量指定空间一样。