【问题标题】:Simple Window WinAPI Object-oriented简单窗口 WinAPI 面向对象
【发布时间】:2023-03-03 10:19:01
【问题描述】:

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

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”处理程序的定义。

【问题讨论】:

  • p_app 似乎未初始化。
  • Windows API 已经是面向对象的。你想改进什么?
  • @IInspectable 已经是面向 Windows API 的对象了,从什么时候开始?你可能在谈论那些框架和 c++ 类,也许是 java。它们都只是封装了扁平的 Windows API。
  • @milevyo:Windows API 自几十年前问世以来就一直是面向对象的。它实现了通过不透明句柄、数据封装和多态性引用的对象。所有这些都可以使用 C 来实现,而 Windows API 偶然会这样做。
  • 来自维基百科:Windows NT是微软出品的操作系统家族(包括Windows 7、8、Phone 8、Xbox),第一版于1993年7月发布。 - 基于语言的、独立于处理器的、多处理、多用户的操作系统。最好将其描述为基于对象而不是面向对象,因为它不包含面向对象语言的完整继承属性。

标签: c++ oop winapi window


【解决方案1】:

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

p_app = new WinApp;

p_app = new WinApp();

另一件事,在功能上:

BOOL WinApp::InitApp(HINSTANCE hThisInstance)

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

HINSTANCE m_hInstance = hThisInstance;

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

m_hInstance = hThisInstance;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2015-02-20
    相关资源
    最近更新 更多