简单窗口WinAPI面向对象

时间:2015-10-17 21:14:33

标签: c++ oop winapi window

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

main.cpp中:

localhost:8081/index.ios.bundle
line: 2041
message: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Map`.'
2015-10-17 22:06:05.289 [warn][tid:com.facebook.React.JavaScript] 'devtools socket closed'

WinApp.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.h:

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

}

我收到一条错误消息:

“_WinApi.exe中0x013F1ADA处的未处理异常:0xC0000005:访问冲突读取位置0x00000008。”

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

1 个答案:

答案 0 :(得分:0)

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

p_app = new WinApp;

p_app = new WinApp();

另一件事,功能:

BOOL WinApp::InitApp(HINSTANCE hThisInstance)

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

HINSTANCE m_hInstance = hThisInstance;

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

m_hInstance = hThisInstance;
相关问题