win32学习之 -------- 面向对象的窗口类封装 代码记录

发布时间:2020-06-30 22:10:28 作者:hello_world007
来源:网络 阅读:740

---创建stdafx.h头文件

#include<Windows.h>
#include<tchar.h>
#include<assert.h>
#include "resource.h"

---创建QWnd类==》窗口基类

    ---QWnd.h文件

#pragma once
#include "stdafx.h"

class QWnd
{
public:
	QWnd(void);
	~QWnd(void);
	virtual BOOL CreateEx(
		DWORD dwExStyle,
		LPCTSTR lpszClassName,
		LPCTSTR lpszWindowName,
		DWORD dwStyle,
		int x,
		int y,
		int nWidth,
		int nHeight,
		HWND hWndParent,
		HMENU nIDorHMenu,
		LPVOID lpParam = NULL
	);
	virtual BOOL PreCreateWindow(CREATESTRUCT & cs);
public:
	BOOL ShowWindow(int nCmdShow);
	BOOL UpdateWindow();
	BOOL DestroyWindow();
public:
	static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
	virtual LRESULT  WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
	virtual LRESULT  Default(UINT uMsg, WPARAM wParam, LPARAM lParam);
public:
	//message handle
	virtual LRESULT onClose(WPARAM wParam, LPARAM lParam);
	virtual LRESULT onDestroy(WPARAM wParam, LPARAM lParam);
	virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam);
public:
	BOOL SubClassWindow(HWND hWnd);
	static QWnd * FromHandle(HWND hWnd);
public:
	HWND m_hWnd;
	WNDPROC m_lpfnOldWndProc;
};

   ---QWnd.cpp文件

#include "QWnd.h"

QWnd::QWnd() {
	m_hWnd = NULL;
	m_lpfnOldWndProc = NULL;
}

QWnd::~QWnd() {

}

 LRESULT CALLBACK QWnd::WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) {
	 if (uMsg == WM_CREATE || uMsg == WM_NCCREATE) {
		 LPCREATESTRUCT pCs = (LPCREATESTRUCT)lParam;
		 if (pCs) {
			 QWnd* qWnd = (QWnd*) pCs->lpCreateParams;//这个就是之前CreateWindowEx 传过来的this  这里的处理原因是因为CreateWindowEx在SetWindowLong之前 所以用这种方式
			 if (qWnd) {
				 qWnd->m_hWnd = hWnd;
				 qWnd->m_lpfnOldWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);
				 return qWnd->WindowProc(uMsg, wParam, lParam);
			 }

		 }
	 }
	QWnd * qWnd = (QWnd*)GetWindowLong(hWnd, GWL_USERDATA);
	if (qWnd) {

		return qWnd->WindowProc(uMsg, wParam, lParam);
	}
	
	return ::DefWindowProc(hWnd,uMsg,wParam,lParam);
}

LRESULT  QWnd::WindowProc( UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_CLOSE:
	{
		return onClose(wParam, lParam);
	}
	break;
	case WM_DESTROY:
	{
		return onDestroy(wParam, lParam);
	}
	break;
	case WM_CREATE:
	{
		return onCreate(wParam, lParam);
	}
	break;
	}
	return Default( uMsg, wParam, lParam);
}
LRESULT   QWnd::Default(UINT uMsg, WPARAM wParam, LPARAM lParam) {
	if (m_lpfnOldWndProc == QWnd::WndProc) {
		return ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
	}
	return m_lpfnOldWndProc(m_hWnd, uMsg, wParam, lParam);
}
//msg handle 
LRESULT QWnd::onClose(WPARAM wParam, LPARAM lParam) {
	return Default( WM_CLOSE, wParam, lParam);
	
}

LRESULT QWnd::onDestroy(WPARAM wParam, LPARAM lParam) {
	return Default(WM_DESTROY, wParam, lParam);
	
}

LRESULT QWnd::onCreate(WPARAM wParam, LPARAM lParam) {
	return Default(WM_CREATE, wParam, lParam);
}

BOOL  QWnd::SubClassWindow(HWND hWnd) {
	assert(hWnd);
	if (m_hWnd == hWnd) {
		return true;
	}
	m_lpfnOldWndProc = (WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);
	if (m_lpfnOldWndProc != QWnd::WndProc) {
		m_hWnd = hWnd;
		SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);
		SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)QWnd::WndProc);
		return TRUE;
	}
	return FALSE;
}
QWnd * QWnd::FromHandle(HWND hWnd) {
	assert(hWnd);
	QWnd * qWnd = (QWnd*)GetWindowLong(hWnd, GWL_USERDATA);
	return qWnd;
}
BOOL QWnd::PreCreateWindow(CREATESTRUCT& cs) {

	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	BOOL bRet = ::GetClassInfoEx(cs.hInstance,cs.lpszClass,&wcex ); //检查窗口类是否被注册
	if (bRet) {
		return true;
	}

	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = cs.hInstance;
	wcex.hIcon = (HICON)::LoadIcon(NULL, (LPCTSTR)IDI_ICON1);
	wcex.hIconSm = (HICON)::LoadIcon(NULL, (LPCTSTR)IDI_ICON1);
	wcex.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
	wcex.hCursor = (HCURSOR)::LoadCursor(NULL, IDC_ARROW);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = cs.lpszClass;
	bRet = ::RegisterClassEx(&wcex);
	if (!bRet) {
		return FALSE;
	}

	return true;
}

BOOL QWnd::CreateEx(
	DWORD dwExStyle,
	LPCTSTR lpszClassName,
	LPCTSTR lpszWindowName,
	DWORD dwStyle,
	int x,
	int y,
	int nWidth,
	int nHeight,
	HWND hWndParent,
	HMENU nIDorHMenu,
	LPVOID lpParam 
) {
	HINSTANCE hInstance = (HINSTANCE)::GetModuleHandle(NULL);
	assert(hInstance);
	CREATESTRUCT cs;
	cs.cx = nWidth;
	cs.cy = nHeight;
	cs.dwExStyle = dwExStyle;
	cs.hInstance = hInstance;
	cs.hMenu = nIDorHMenu;
	cs.hwndParent = hWndParent;
	cs.lpCreateParams = lpParam;
	cs.lpszClass = lpszClassName;
	cs.lpszName = lpszWindowName;
	cs.style = dwExStyle;
	cs.x = x;
	cs.y = y;


	BOOL bRet = PreCreateWindow(cs);
	if (!bRet) {
		MessageBox(NULL, _T("注册窗口类失败"),_T("注册窗口类"), 0);
		return false;
	}

	HWND hWnd = CreateWindowEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle,x,y,nWidth,nHeight, hWndParent, nIDorHMenu, hInstance,this);//这个函数执行完 会发送WM_CREATE消息  this的作用是因为提前传递
	if (NULL == hWnd) {
		return FALSE;
	}
	m_hWnd = hWnd;
	SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);

	m_lpfnOldWndProc = (WNDPROC)GetWindowLong(m_hWnd, GWL_WNDPROC);//原类的处理程序
	if (m_lpfnOldWndProc != QWnd::WndProc) {
		//子类化
		SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)QWnd::WndProc);
		WindowProc(WM_CREATE,0,0); //这里重新发送WM_CREATE消息
		WindowProc(WM_NCCREATE, 0, 0); //这里重新发送WM_CREATE消息
	}
	return TRUE;
}

BOOL QWnd::ShowWindow(int nCmdShow) {
	assert(m_hWnd);
	return ::ShowWindow(m_hWnd, nCmdShow);
}

BOOL QWnd::UpdateWindow() {
	assert(m_hWnd);
	return ::UpdateWindow(m_hWnd);
}

BOOL QWnd::DestroyWindow() {
	assert(m_hWnd);
	return ::DestroyWindow(m_hWnd);
}



---创建QWinApp基类==》应用程序基类

   ---QWinApp.h文件

#pragma once
#include "QWnd.h"
#include "QButton.h"
class QMainFrame :
	public QWnd
{
public:
	QMainFrame();
	~QMainFrame();
public:
	LRESULT onClose(WPARAM wParam, LPARAM lParam);
	LRESULT onDestroy(WPARAM wParam, LPARAM lParam);
	virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam);
public:
	QButton *m_wndButton;
};

   ---QWinApp.cpp文件

#include "QWinApp.h"
#include "QGlobal.h"



QWinApp::QWinApp()
{
	m_pMainWnd = NULL;
	g_pWinApp = this;

}


QWinApp::~QWinApp()
{
}

BOOL QWinApp::InitInstance() {
	return TRUE;
}

BOOL QWinApp::ExitInstance() {
	return TRUE;
}

void QWinApp::run() {
	MSG msg;
	while (::GetMessage(&msg, NULL, NULL, NULL)) {
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}



---创建应该程序类的全局变量

    ---创建QGlobal.h头文件

#pragma once
#include "QWinApp.h"
extern QWinApp* g_pWinApp;
extern QWinApp* GlbGetApp();

   ---创建QGlobal.cpp文件

#include "QGlobal.h"
QWinApp* g_pWinApp = NULL;
QWinApp* GlbGetApp() {
	return g_pWinApp;
}


---创建QWnd的子类--子控件Button

    ---创建文件QButton.h

#pragma once
#include "QWnd.h"
class QButton :
	public QWnd
{
public:
	QButton();
	~QButton();
public:
	BOOL CreateEx(
		LPCTSTR lpszWindowName,
		DWORD dwStyle,
		int x,
		int y,
		int nWidth,
		int nHeight,
		HWND hWndParent,
		HMENU nIDorHMenu
	);
};


    ---创建文件QButton.cpp

#include "QButton.h"



QButton::QButton()
{
}


QButton::~QButton()
{
}

BOOL QButton::CreateEx(
	LPCTSTR lpszWindowName,
	DWORD dwStyle,
	int x,
	int y,
	int nWidth,
	int nHeight,
	HWND hWndParent,
	HMENU nIDorHMenu
) {
	return QWnd::CreateEx(0, _T("BUTTON"), lpszWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, nIDorHMenu);
}



---创建QWnd的子类---QMainFrame

    ---创建QMainFrame.h文件

#pragma once
#include "QWnd.h"
#include "QButton.h"
class QMainFrame :
	public QWnd
{
public:
	QMainFrame();
	~QMainFrame();
public:
	LRESULT onClose(WPARAM wParam, LPARAM lParam);
	LRESULT onDestroy(WPARAM wParam, LPARAM lParam);
	virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam);
public:
	QButton *m_wndButton;
};

   ---创建QMainFrame.cpp文件

#include "QMainFrame.h"
#define IDC_BUTTON 10001


QMainFrame::QMainFrame()
{
	m_wndButton = new QButton();
}


QMainFrame::~QMainFrame()
{
}

LRESULT QMainFrame::onClose(WPARAM wParam, LPARAM lParam) {
	return DestroyWindow();

}

LRESULT QMainFrame::onDestroy(WPARAM wParam, LPARAM lParam) {
	::PostQuitMessage(0);
	return true;
}
LRESULT QMainFrame::onCreate(WPARAM wParam, LPARAM lParam) {
	if (NULL == m_wndButton->m_hWnd) {
		m_wndButton->CreateEx(_T("www.itxuba.org"),WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,0,0,200,120,m_hWnd, (HMENU)IDC_BUTTON);
	}
	return TRUE;
}



---创建QWinApp的子类---QDemowinApp

   ---创建QDemowinApp.h文件

#pragma once
#include "QWinApp.h"
class QDemowinApp :
	public QWinApp
{
public:
	QDemowinApp();
	~QDemowinApp();
public:
	virtual BOOL InitInstance();
	virtual BOOL ExitInstance();
};
extern QDemowinApp theApp;

  

   ---创建QDemowinApp.cpp文件

#include "QDemowinApp.h"
#include "QMainFrame.h"

QDemowinApp::QDemowinApp()
{
}


QDemowinApp::~QDemowinApp()
{
}
QDemowinApp theApp;
BOOL QDemowinApp::InitInstance() {
	QMainFrame *pMainWnd = new QMainFrame();
	assert(pMainWnd);
	m_pMainWnd = pMainWnd;
	BOOL bRet = pMainWnd->CreateEx(0,_T("www.itxueba.org"),_T("www.itxueba.org"),WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL,NULL);
	if (!bRet) {
		MessageBox(NULL, _T("create window failed!"), _T("create window!"), 0);
	}
	pMainWnd->ShowWindow(SW_SHOW);
	pMainWnd->UpdateWindow();
	return true;
}
BOOL QDemowinApp::ExitInstance() {
	if (m_pMainWnd) {
		delete m_pMainWnd;
		m_pMainWnd = NULL;
	}
	return TRUE;
}








---创建入口程序

#include "QDemowinApp.h"
#include "QGlobal.h"
int WINAPI _tWinMain(
	IN HINSTANCE hInstance,
	IN HINSTANCE hPrevInstance,
	IN LPTSTR lpCmdLine,
	IN int nShowCmd
) {
	QWinApp* pWinApp = GlbGetApp();
	assert(pWinApp);
	pWinApp->InitInstance();
	pWinApp->run();
	pWinApp->ExitInstance();
	return TRUE;
}


推荐阅读:
  1. win中如何安装,配置及操作mysql
  2. win中php安装的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

win 对象 面向

上一篇:本地kubeadm搭建kubernetes集群

下一篇:Nagios 监控mongodb

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》