2015-10-17 92 views
0

我試着火起來的面向對象的方式最簡單的窗口:簡單的窗口WINAPI面向對象

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; 

}; 

我收到一個錯誤或消息:

「_WinApi.exe中的0x013F1ADA未處理的異常:0xC0000005:訪問衝突讀取位置0x00000008」。

而黃色箭頭指向「m_hwnd」處理程序的定義。

+0

爲什麼不使用MFC? – DawidPi

+3

'p_app'似乎是未初始化的。 –

+0

Windows API已經是面向對象的。你想改善什麼? – IInspectable

回答

0

至於阿倫·斯托克斯寫道,你必須初始化指針:

p_app = new WinApp; 

p_app = new WinApp(); 

另一件事,在功能上:

BOOL WinApp::InitApp(HINSTANCE hThisInstance) 

你創建新的臨時變量:

HINSTANCE m_hInstance = hThisInstance; 

併爲其分配內存而不是您的成員變量(m_hInstance)。嘗試:

m_hInstance = hThisInstance;