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. 程序 /******************************* ...
随机推荐
- Magento Connector: Error: Please check for sufficient write file permissions
请确保下面的文件是可写的: /path/to/your/magento-install/path/to/your/magento-install/downloader/path/to/your/mag ...
- C# MVC 页面静态化导致的问题
在设置页面静态化的路由,代码如 //静态路由 routes.MapRoute( name: "html", url: "{controller}/{action}.htm ...
- Instsrv.exe和Srvany.exe的使用方法
要把应用程序添加为服务,你需要两个小软件:Instsrv.exe和Srvany.exe.Instsrv.exe可以给系统安装和删除服务,Srvany.exe可以让程序以服务的方式运行.这两个软件都包含 ...
- nuget github host
191.236.146.247 www.nuget.org191.236.146.247 nuget.org 192.30.253.112 github.com192.30.253.113 githu ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- Spring boot mybatis项目启动后一直刷日志的bug修复……
最近接手一个项目,使用的框架是springboot+mybatis: 其中持久层是使用mybatis集成的,sql是配置在mapper.xml文件中: 然后呢,有时候做新功能的时候,往xml文件中增加 ...
- python中常用的模块的总结
1. 模块和包 a.定义: 模块用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件.(例如:文件名:test.py,对应的模块名:test) ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件介绍
RDIFramework.NET ━ .NET快速信息化系统开发框架 工作流程组件介绍 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部 ...
- 使用nginx-http-concat添加nginx资源请求合并功能
web项目中有时候一个页面会加载多个js或css资源请求,导致页面加载耗时较长,这时优化的方向可以采用资源合并,可以在客户端事先合并,也可以在服务端进行资源合并,服务端合并的方式使用起来更灵活. ng ...
- linux/unix 编程手册 fork()函数
父进程通过fork()函数创建子进程,将父进程数据段和栈的内容拷贝到子进程中,子进程执行程序execve创建新程序,调用exit函数退出到等待wait(),挂起父进程, 父子进程享用相同的程序文本段. ...