Windows普通窗口程序
2015-10-09 12:55:38
KWindow.h
#pragma once #include <windows.h> class KWindow { virtual void OnDraw(HDC hdc) { } virtual void OnKeyDown(WPARAM wParam, LPARAM lParam) { } virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual void GetWndClassEx(WNDCLASSEX& wc); public: HWND m_hWnd; KWindow() { m_hWnd = NULL; } virtual ~KWindow() { } virtual bool CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hParent, HMENU hMenu, HINSTANCE hInst); bool RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst); virtual WPARAM MessageLoop(); BOOL ShowWindow(int nCmdShow) const { return ::ShowWindow(m_hWnd, nCmdShow); } BOOL UpdateWindow() const { return ::UpdateWindow(m_hWnd); } void CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point); };
KWindow.cpp
#define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <tchar.h> #include <assert.h> #include "KWindow.h" LRESULT KWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_KEYDOWN: { OnKeyDown(wParam, lParam); ; } case WM_PAINT: { PAINTSTRUCT ps; BeginPaint(m_hWnd, &ps); OnDraw(ps.hdc); EndPaint(m_hWnd, &ps); ; } case WM_DESTROY: { PostQuitMessage(); ; } } return DefWindowProc(hWnd, uMsg, wParam, lParam); } LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { KWindow* pWindow; DWORD dwErr = ; if(uMsg == WM_NCCREATE) { assert( ! IsBadReadPtr((void*)lParam, sizeof(CREATESTRUCT))); MDICREATESTRUCT* pMDIC = (MDICREATESTRUCT*)((LPCREATESTRUCT)lParam)->lpCreateParams; pWindow = (KWindow*)(pMDIC->lParam); assert(!IsBadReadPtr(pWindow, sizeof(KWindow))); SetWindowLong(hWnd, GWL_USERDATA, (LONG)pWindow); } else { pWindow = (KWindow*)GetWindowLong(hWnd, GWL_USERDATA); } if(pWindow) { return pWindow->WndProc(hWnd, uMsg, wParam, lParam); } else { return DefWindowProc(hWnd, uMsg, wParam, lParam); } } bool KWindow::RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst) { WNDCLASSEX wc; GetWndClassEx(wc); wc.hInstance = hInst; wc.lpszClassName = lpszClass; if(!RegisterClassEx(&wc)) return false; return true; } bool KWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hParent, HMENU hMenu, HINSTANCE hInst) { if(!RegisterClass(lpszClass,hInst)) return false; MDICREATESTRUCT mdic; memset(&mdic, , sizeof(mdic)); mdic.lParam = (LPARAM)this; m_hWnd = CreateWindowEx(dwExStyle, lpszClass, lpszName, dwStyle, x, y, nWidth, nHeight, hParent, hMenu, hInst, &mdic); return m_hWnd != NULL; } void KWindow::GetWndClassEx(WNDCLASSEX& wc) { memset(&wc, , sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = ; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = ; wc.cbWndExtra = ; wc.hInstance = NULL; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = NULL; wc.hIconSm = NULL; } WPARAM KWindow::MessageLoop() { MSG msg; , )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void KWindow::CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point) { HFONT hFont = CreateFont(-point * GetDeviceCaps(hdc, LOGPIXELSY) / , , , , FW_BOLD, TRUE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, szFace); assert(hFont); HGDIOBJ hold = SelectObject(hdc, hFont); SetTextAlign(hdc, TA_CENTER | TA_BASELINE); SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(,,0xFF)); TextOut(hdc, x, y, szMessage, _tcslen(szMessage)); SelectObject(hdc, hold); DeleteObject(hFont); }
KHelloWindow.cpp
#define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <tchar.h> #include <assert.h> #include <conio.h> #include <iostream> #include "KWindow.h" #pragma warning( disable:4996 ) const TCHAR szMessage[] = _T("Hello,World!"); const TCHAR szFace[] = _T("Times New Roman"); const TCHAR szHint[] = _T("Press ESC to quit."); const TCHAR szProgram[] = _T("HelloWorld3"); class KHelloWindow : public KWindow { void OnKeyDown(WPARAM wParam, LPARAM lParam) { if(wParam == VK_ESCAPE) PostMessage(m_hWnd, WM_CLOSE, , ); } void OnDraw(HDC hdc) { TextOut(hdc, , , szHint, lstrlen(szHint)); CenterText(hdc, GetDeviceCaps(hdc, HORZRES) / , GetDeviceCaps(hdc, VERTRES) / , szFace, szMessage, ); } }; //* int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd ) { KHelloWindow win; , szProgram, szProgram, WS_POPUP, , , GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance); win.ShowWindow(nShowCmd); win.UpdateWindow(); return win.MessageLoop(); } /*/
Windows普通窗口程序的更多相关文章
- Cocos2dx集成于windows桌面窗口程序的步骤
2D游戏需要做编辑器,而编辑器总是希望可以复用游戏中的逻辑来运行场景试看效果. 对于cocos2dx开发的程序,这个需求可以描述为: 实现一种方法,在桌面窗口程序中的某个控件上显示cocos2dx的场 ...
- VC菜菜鸟:建立第一个基于Visual C++的Windows窗口程序
建立第一个基于VisualC++的Windows窗口程序: 发表于:http://blog.csdn.net/it1988888/article/details/10306585 a)执行命令:新建 ...
- Windows程序设计笔记(二) 关于编写简单窗口程序中的几点疑惑
在编写窗口程序时主要是5个步骤,创建窗口类.注册窗口类.创建窗口.显示窗口.消息环的编写.对于这5个步骤为何要这样写,当初我不是太理解,学习到现在有些问题我基本上已经找到了答案,同时对于Windows ...
- windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序
windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序 原因:C:\Windows\System32目录下没有telnet.exe,path系统变量的值包含了C:\W ...
- (Delphi)第一个Windows 32 API的窗口程序
program Project1; uses Winapi.Windows, Winapi.messages; {$R *.res} const className = 'MyDelphiWindow ...
- Windows窗口程序从创建到关闭产生的消息
Windows是消息驱动的,理解消息机制及消息循环是特别重要.知道在什么情况下产生什么消息会让我们对程序有更好的控制.Windows给应用程序发消息,有些会加入应用程序的消息队列,也是就是队列消息.有 ...
- 我的第一个 Windows 窗口程序(1)
一般来说,构建一个 Windows 程序可以分为如下几个步骤: 定义窗口类(WNDCLASS) 注册窗口类(RegisterClass) 创建窗口(CreateWindow) 更新显示窗口(Updat ...
- [MFC]_在vs2019中使用MFC快速构建简单windows窗口程序
微软基础类库(英语: Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发 ...
- Windows编程入门程序详解
引用:http://blog.csdn.net/jarvischu/article/details/8115390 1. 程序 /******************************* ...
随机推荐
- JSP数据交互
JSP数据交互 一.jsp中java小脚本 1.<% java代码段%> 2.<% =java表达式%>不能有分号 3.<%!成员变量和函数声明%>二.注释 1 ...
- Issue 7: 网络in action
网络运维基础 基础参数 配置:IP,子网掩码,网关,dns服务器,dhcp服务器 基础应用 在网关设置上搭建VPN组网 改host文件 单台主机原则上只能配置一个网关 协议 协议是全球都遵守的一套编码 ...
- 自定义EL表达式的函数
编写描述的tld文件放到web-inf/目录下,才能在jsp页面上调用 <?xml version="1.0" encoding="UTF-8" ?> ...
- could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...
- lintcode bugfree and good codestyle note
2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...
- How to use umbraco datetime property editor
When I was using Umbraco datetime property editor, I met with a problem that the editor must be firs ...
- HDU-1233 还是畅通工程
Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能 ...
- Datatypes translation between Oracle and SQL Server
Datatypes translation between Oracle and SQL Server part 1: character, binary strings Datatypes tran ...
- html5,单击显示详细信息
<details open=""> <summary>点击率</summary> <p>详细信息</p&g ...
- windows下利用virtual 安装 flask
出处: https://segmentfault.com/a/1190000002450878 本文介绍Windows下如何从零开始搭建Python + Flask开发环境. 安装Python 2.7 ...