代码
Enums.hpp
#ifndef _WENUMS_HPP_
#define _WENUMS_HPP_
namespace Windows
{
enum WINDOW_STATE
{
WS_NO_ERROR,
WS_WRF,
WS_WCF
};
}
#endif
Window.hpp
#ifndef _WWINDOW_HPP_
#define _WWINDOW_HPP_
#include <windows.h>
#include <string>
#include "Enums.hpp"
namespace Windows
{
struct WindowInfo
{
DWORD dwExStyle;
LPCTSTR lpClassName;
LPCTSTR lpWindowName;
DWORD dwStyle;
int x;
int y;
int width;
int height;
HWND hWndParent;
HMENU hMenu;
HINSTANCE hInstance;
LPVOID lpParam;
HWND create()
{
return CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,width,height,hWndParent,hMenu,hInstance,lpParam);
}
};
WNDCLASSEX getDefaultWndClass(HINSTANCE hInstance,LRESULT (CALLBACK *WndProc)(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam))
{
WNDCLASSEX wc;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
return wc;
}
struct WindowErrorInfo
{
int error;
std::string errorStr;
};
class Window
{
private:
HWND m_hwnd;
MSG m_msg;
void (*m_main)();
LRESULT (CALLBACK *m_onProcess)(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
WindowErrorInfo m_errorInfo;
void m_setError(int error,std::string errorStr)
{
m_errorInfo.error = error;
m_errorInfo.errorStr = errorStr;
}
Window()
{
m_hwnd = NULL;
m_main = NULL;
m_onProcess = NULL;
m_errorInfo.error = WS_NO_ERROR;
m_errorInfo.errorStr = std::string();
}
public:
Window(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow,LRESULT (CALLBACK *WndProc)(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam),std::string title = "",int w = 640,int h = 480)
{
WNDCLASSEX wc = getDefaultWndClass(hInstance,m_onProcess);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
m_setError(WS_WRF,"Window Registration Failed!");
return;
}
m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass",title.data(),WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
w,
h,
NULL,NULL,hInstance,NULL);
if(m_hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
m_setError(WS_WCF,"Window Creation Failed!");
return;
}
}
Window(WNDCLASSEX wc,WindowInfo winfo)
{
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
m_setError(WS_WRF,"Window Registration Failed!");
return;
}
m_hwnd = winfo.create();
if(m_hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
m_setError(WS_WCF,"Window Creation Failed!");
return;
}
}
WindowErrorInfo getError()
{
return m_errorInfo;
}
int run()
{
while(GetMessage(&m_msg, NULL, 0, 0) > 0)
{
m_main();
TranslateMessage(&m_msg);
DispatchMessage(&m_msg);
}
return m_msg.wParam;
}
};
}
#endif
额…
运行时连个错误日志都没输出,我也不知道哪里有问题,各位借鉴一下就好
|