English 中文(简体)
登记册系统故障
原标题:RegisterClass failing
  • 时间:2024-02-15 01:14:06
  •  标签:
  • c++
  • windows

我正在制作一个简单的Windows方案,但出于某种原因,登记中心正在归还FLSE,我不肯定为什么。

I set the lpszClassName,hInstance, and lpfnWndProc members of my WNDCLASS structure.

我的守则是:


#include <windows.h>
#include <tchar.h>

template<typename T>
class BaseWindow {
public:
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam) {
        T* pThis = NULL;
        if (umsg == WM_NCCREATE) {
            CREATESTRUCT* pCreate = (CREATESTRUCT*)lparam;
            pThis = (T*)pCreate->lpCreateParams;
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
            pThis->m_hwnd = hwnd;
        } else {
            pThis = (T*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
        }
        return pThis->HandleMessage(umsg, wparam, lparam);
    }
    HWND m_hwnd;
    BOOL cls_state = FALSE;
    BOOL Init() {
        WNDCLASS wc;
        wc.lpszClassName = ClassName();
        wc.hInstance = GetModuleHandle(NULL);
        wc.lpfnWndProc = T::WndProc;

        return RegisterClass(&wc);

    }
    HWND Create(const char* szWindowName, DWORD dwStyle) {
        if (cls_state == FALSE) {
            if (!Init()) {
                MessageBox(NULL, _T("Init Failed. :("), _T("Error!"), MB_OK);
            }
            cls_state = TRUE;
        }
        return m_hwnd = CreateWindow(ClassName(), szWindowName, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), this);
    }
    const char* ClassName() { return _T("BaseWindow Class"); }
};

class MainWindow : public BaseWindow<MainWindow> {
public:
    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) {
        return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
    }
    const char* ClassName() { return _T("MainWindow Class"); }
};

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {


    MainWindow win;
    win.Create(_T("Mush!"), 0);

    ShowWindow(win.m_hwnd, nCmdShow);

    MSG msg;

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

    return msg.wParam;
}

问题回答

贵国法典存在许多问题:

  1. 页: 1 页: 1

    因此,没有田地被排除在外。 在Win32的APIC中,有时是建筑领域是选择性的(如果是,它们就有相应的记载),但通常不是。 这当然与<代码>有关。 WNDCLASS。 有许多<代码>。 WNDCLASS 页: 1

  2. <>RegisterClass(>>> 不能随附<>ERROR_INVALID_WINDOW_HANDLE,作为你的要求——但CreateWindow()。 如果你认为是<代码>RegisterClass()报告错误,那么你可能会在错误的地方打上Get LastError((但你没有表明在你的代码中)。

    注:WM_NCCREATE>:not第1条信息,即窗口可以接收,因此,<代码>WndProc(需要处理p>的可能性 该仍可改为NUL。 页: 1 This;HandleMessage(),从而导致未界定的行为<>。 在此情况下,你可能会通过一个无效的<代码>HWND至DefWindowProc(,从而导致其失败,从而导致CreateWindow(>。

  3. 页: 1

  4. 页: 1 这是非法的。 您需要将<条码>lpCreateParams投到您授予的<条码>。

    <编码>p (T*) (BaseWindow*) pCreate->lpCreateParams;

  5. 页: 1

With that said, try this instead:

#include <windows.h>
#include <tchar.h>
#include <stdexcept>
#include <string>

template<typename T>
class BaseWindow {
public:
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam) {
        T* pThis;
        if (umsg == WM_NCCREATE) {
            CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lparam);
            pThis = static_cast<T*>(static_cast<BaseWindow*>(pCreate->lpCreateParams));
            SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
            pThis->m_hwnd = hwnd;
        } else {
            pThis = reinterpret_cast<T*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
        }
        if (pThis) return pThis->HandleMessage(umsg, wparam, lparam);
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    HWND m_hwnd = nullptr;
    bool cls_state = false;

    void Init() {
        WNDCLASS wc = {};
        wc.lpszClassName = ClassName();
        wc.hInstance = GetModuleHandle(nullptr);
        wc.lpfnWndProc = T::WndProc;

        if (!RegisterClass(&wc)) {
            DWORD err = GetLastError();
            if (err != ERROR_CLASS_ALREADY_EXISTS) {
                throw std::runtime_error("Init Failed, Error " + std::to_string(err));
            }
        }
    }

    HWND Create(const TCHAR* szWindowName, DWORD dwStyle) {
        if (!cls_state) {
            Init();
            cls_state = true;
        }
        m_hwnd = CreateWindow(ClassName(), szWindowName, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, GetModuleHandle(nullptr), this);
        if (!m_hwnd) {
            DWORD err = GetLastError();
            throw std::runtime_error("Create Failed, Error " + std::to_string(err));
        }
        return m_hwnd;
    }

    virtual const TCHAR* ClassName() { return _T("BaseWindow Class"); }
};

class MainWindow : public BaseWindow<MainWindow> {
public:
    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) {
        return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
    }

    const TCHAR* ClassName() override { return _T("MainWindow Class"); }
};

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {
    try {
        MainWindow win;
        win.Create(_T("Mush!"), 0);

        ShowWindow(win.m_hwnd, nCmdShow);

        MSG msg;
        while (GetMessage (&msg, nullptr, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        return msg.wParam;
    }
    catch (const std::exception& ex) {
        MessageBoxA(NULL, ex.what(), "Error!", MB_OK);
        return 0;
    }
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签