c++ - Win32 Window Wrapper 错误(参数不正确)

标签 c++ winapi this wrapper

我正在为 win32 编写一个窗口包装器,以便更轻松地创建 gui。我有一个抽象显示类、一个显示类和一个显示类。最后,窗口没有出现。经过一些调试后,我的窗口类没有被正确注册。 GetLastError 后得到INVALID PARAMETER 的错误码

显示类.h:

#pragma once

#include <Windows.h>
#include "abstractdisplay.h"
class DisplayClass : protected WNDCLASSEX
        {
            public:
                // Public variables 

            private:
                // Private variables

            protected:
                // Protected variables

            public:
                // Public functions
                DisplayClass(HINSTANCE hInst, const TCHAR* className);
                DisplayClass();
                ~DisplayClass();

                // Registers the class

                // Get the class name
                virtual const TCHAR* getClassName() const { return lpszClassName; }
                virtual bool Register();

            private:
                // Private functions

            protected:
                    // Protected functions

                };

显示类.cpp:

 #include "displayclass.h"
 #include "abstractdisplay.h"
 #include <string>
 #include <cstring>

DisplayClass::DisplayClass(HINSTANCE hInst, const TCHAR* className) 
        {
            hInstance = hInst;

            // All messages for windows that belong to this Window Class will be sent to Message Router
            lpfnWndProc = AbstractDisplay::MessageRouter;
            lpszClassName = className;

            // Set values for the rest of the WNDCLASSEX structure
            ZeroMemory(this, sizeof(WNDCLASSEX));

            lpszMenuName = 0;
            cbSize = sizeof(WNDCLASSEX);
            cbClsExtra = 0;
            cbWndExtra = 0;
            hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
            hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);
            hCursor = ::LoadCursor(NULL, IDC_ARROW);
            style = CS_HREDRAW | CS_VREDRAW;
            hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        }

        DisplayClass::DisplayClass()
        {

        }

        DisplayClass::~DisplayClass()
        {

        }

        // Returns the last Win32 error, in string format. Returns an empty string if there is no error.
        std::string GetLastErrorAsString()
        {
            // Get the error message, if any.
            DWORD errorMessageID = ::GetLastError();
            if (errorMessageID == 0)
                return std::string(); // No error message has been recorded

            LPSTR messageBuffer = nullptr;
            size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

            std::string message(messageBuffer, size);

            // Free the buffer.
            LocalFree(messageBuffer);

            return message;
        }

        bool DisplayClass::Register()
        {   
            if (::RegisterClassEx(this) != 0)
            {
                return true;
            }
            else
            {
                OutputDebugString("ERROR CODE BE LIKE:");
                OutputDebugString(GetLastErrorAsString().c_str());
                OutputDebugString("\n");
                return false;
            }
        }

调试:

ERROR CODE BE LIKE:The parameter is incorrect.

(有点遵循此来源:http://www.infernodevelopment.com/c-win32-api-simple-gui-wrapper)

最佳答案

在分配一些结构成员(即 hInstancelpfnWndProclpszClassName 将全部为空)。

关于c++ - Win32 Window Wrapper 错误(参数不正确),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937798/

相关文章:

javascript - 将此对象存储在自定义私有(private)变量中

javascript - $(this) 在 .append() 中不起作用

c++ - 如何使用 BFS 算法只保留外部点?

c++ - 为 IShellItemArray 创建 PCIDLIST_ABSOLUTE_ARRAY

c++ - 具有两个线程的 STL 队列(生产者、消费者)

jquery - jquery 中的 .id 和 .attr ('id' ) 有什么区别

c++ - 获取具有计数的 vector 的不同 vector

c++ - 如何获取 C++ 中抽象(?)pimpl 的调试信息?

c++ - 如何根据其成员数据对结构/类数组进行排序?

c++ - 无需重启即可访问自定义环境变量(使用 C++)