简单 Window WinAPI 面向对象

Simple Window WinAPI Object-oriented

我尝试以面向对象的方式启动最简单的window:

main.cpp:

#include <windows.h>
#include "WinApp.h"


WinApp* p_app;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hThisInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszArgument,
    int nFunsterStill)
{
    MSG message;


    if (hPrevInstance == NULL)
        if (!p_app->InitApp(hThisInstance))
            return 0;

    if (!p_app->InitInst(lpszArgument, nFunsterStill))
        return 0;


    while (GetMessage(&message, NULL, 0, 0))
    {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }

    return (int)(message.wParam);

}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0;

}

WinApp.cpp:

#include "WinApp.h"
#include "windows.h"
#include <cstdlib>
using namespace std;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
const char szClassName[] = "WindowsApp";


WinApp::WinApp()
{
}

WinApp::~WinApp()
{
}

BOOL WinApp::InitApp(HINSTANCE hThisInstance)
{

    WNDCLASSEX wincl;

    HINSTANCE m_hInstance = hThisInstance;

    wincl.hInstance = m_hInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

  if (!RegisterClassEx(&wincl))
  return FALSE;

  return TRUE;

}

BOOL WinApp::InitInst(LPSTR lpszArgument, int nFunsterStill)
{

     m_hwnd =   CreateWindowEx(
                0,
                szClassName,
                "Windows App",
                WS_OVERLAPPED,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                544,
                375,
                HWND_DESKTOP,
                NULL,
                m_hInstance,
                NULL);

    ::ShowWindow(m_hwnd, nFunsterStill);
    ::UpdateWindow(m_hwnd);

    return TRUE;

}

WinApp.h:

#include <windows.h>
using namespace std;

class WinApp
{
public:

    WinApp();
    ~WinApp();

    BOOL InitApp(HINSTANCE hThisInstance);
    BOOL InitInst(LPSTR lpszArgument, int nFunsterStill);


    HWND m_hwnd;
    HWND h_edit1;
    HINSTANCE m_hInstance;

};

我收到一条错误消息:

"Unhandled exception at 0x013F1ADA in _WinApi.exe: 0xC0000005: Access violation reading location 0x00000008."

黄色箭头指向 "m_hwnd" 处理程序的定义。

正如 Alan Stokes 所写,您必须初始化指针:

p_app = new WinApp;

p_app = new WinApp();

另一件事,在函数中:

BOOL WinApp::InitApp(HINSTANCE hThisInstance)

您正在创建新的临时变量:

HINSTANCE m_hInstance = hThisInstance;

并为其分配内存而不是您的成员变量 (m_hInstance)。尝试:

m_hInstance = hThisInstance;