windows.h 简单窗口类

代码

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

额…

运行时连个错误日志都没输出,我也不知道哪里有问题,各位借鉴一下就好

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值