windows程序设计读书笔记1——创建窗口
第一个win32程序,简单的创建窗口:
#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return ;
} hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ; switch (message)
{
case WM_CREATE:return ; case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawText (hdc, TEXT ("Hello, Windows Program!"), -, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ;
return ; case WM_DESTROY:
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
窗口是由窗口类创建的即WNDCLASS,窗口类确定了处理窗口消息的窗口过程。
MSDN中是这样定义的:
typedef struct {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS, *PWNDCLASS;
在程序中,我们一般这样定义和初始化
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.style字段表示类的风格,具体参加MSDN:
| Style | Action |
|---|---|
| CS_BYTEALIGNCLIENT |
Aligns the window's client area on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display. |
| CS_BYTEALIGNWINDOW |
Aligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display. |
| CS_CLASSDC | Allocates one device context to be shared by all windows in the class. |
| CS_DBLCLKS |
Sends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is within a window belonging to the class. |
| CS_DROPSHADOW |
Windows XP: Enables the drop shadow effect on a window. The effect is turned on and off through SPI_SETDROPSHADOW. |
| CS_GLOBALCLASS |
Specifies that the window class is an application global class. For more information, see Application Global Classes. |
| CS_HREDRAW | Redraws the entire window if a movement or size adjustment changes the width of the client area. |
| CS_NOCLOSE | Disables Close on the window menu. |
| CS_OWNDC | Allocates a unique device context for each window in the class. |
| CS_PARENTDC |
Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the system's cache of device contexts. |
| CS_SAVEBITS | Saves, as a bitmap, the portion of the screen image obscured by a window of this class. |
| CS_VREDRAW | Redraws the entire window if a movement or size adjustment changes the height of the client area. |
我们这里选择的是CS_HREDRAW | CS_VREDRAW,表示无论垂直还是水平尺寸的改变,窗口都将重绘。
wndclass.lpfnWndProc表示窗口过程设置,指向一个函数WndProc。
wndclass.cbClsExtra和cbWndExtra用于维护内部窗口结构中一些预留的空间,可以根据需要来使用这些预留的空间。
wndclass.hInstance表示应用程序的实例句柄即WinMain第一个参数。
wndclass.hIcon为该窗口类设定图标。若要使用本地的图标,第一个参数必须为hInstance实例句柄,第二个参数为图标标示,对于预定义的图标以IDI开头。
wndclass.hCursor与上面类似,载入鼠标指针。
wndclass.hbrBackground指定背景色,这里我们用白色画刷。
wndclass.lpszMenuName即窗口菜单。
wndclass.lpszClassName为窗口类指定名字。
这样窗口类的初始化完成,使用RegisterClass注册类,下面就是窗口的创建了,
使用createWindow函数,MSDN定义如下:
HWND CreateWindow(
__in LPCTSTR lpClassName, //窗口名称
__in LPCTSTR lpWindowName, //窗口标题
__in DWORD dwStyle, //窗口风格
__in int x, //x坐标
__in int y, //y坐标
__in int nWidth, //长度
__in int nHeight, //高度
__in HWND hWndParent, //父窗口句柄
__in HMENU hMenu, //窗口菜单句柄
__in HINSTANCE hInstance, //程序实例句柄
__in LPVOID lpParam //创建参数
);
CreateWindow只是在内部创建窗口,即windows分配了一片内存,要想显示窗口还要使用ShowWindow函数。
第一个参数是CreateWindow创建的窗口句柄,第二个是WinMain接受的iCmdShow值,该参数决定窗口初始显示格式。
UpdateWindow是重绘窗口,这是通过发送一条WM_PAINT消息完成的。
最后则是消息的循环,即不断接受消息,当有消息时调用WndProc函数进行处理。
windows程序设计读书笔记1——创建窗口的更多相关文章
- windows程序设计读书笔记2——字符显示1
本程序使用GetSystemMetrics获取windows各种图像选项,并输出字符到窗口中. #define WINVER 0x0500 #include <windows.h> #in ...
- windows程序设计读书笔记4——字符显示3
在之前的一章里我们使用InvalidateRect函数,生成窗口重绘消息进行重绘,但是并没有在处理滚动条消息时直接绘制,这样的代码效率并不高. 这里作者使用了UpdateWindow函数,直接进行窗口 ...
- windows程序设计读书笔记3——字符显示2
由于显示的字符可能会不全,我们很容易想到的一个解决办法是使用滚动条. 先看一下代码,再进行分析: /*------------------------------------------------- ...
- Spring读书笔记——bean创建(下)
有关Spring加载bean系列,今天这是最后一篇了,主要接上篇对于从Spring容器中获取Bean的一些细节实现的补充. <Spring读书笔记--bean加载>--Spring如何加载 ...
- 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记1——创建窗口&GDI
第1章 创建窗口 步骤: 窗口类的设计 窗口类的注册 窗口的正式创建 窗口的显示与更新 消息循环体系 窗口过程函数处理消息 1. 设计:使用WNDCLASSEX结构体,这里注意的是C++中的结构体中的 ...
- Windows内核读书笔记——Windows异常分发处理机制
本篇读书笔记主要参考自<深入解析Windows操作系统>和<软件调试>这两本书. IDT是处理异常,实现操作系统与CPU的交互的关口. 系统在初始化阶段会去填写这个结构. ID ...
- Windows程序设计学习笔记(1):一个简单的windows程序
<Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...
- Spring读书笔记——bean创建(上)
通过<Spring读书笔记--bean加载>和<Spring读书笔记--bean解析>,我们明白了两件事. Spring如何加载消化一个xml配置文件 Spring如何将xml ...
- Windows程序设计学习笔记(一)Windows内存管理初步
学习Windows程序设计也有一些时间了,为了记录自己的学习成果,以便以后查看,我希望自己能够坚持写下一系列的学习心得,对自己学习的内容进行总结,同时与大家交流.因为刚学习所以可能有的地方写不不正确, ...
随机推荐
- 使导出excle文档实现ALT+Enter的效果()
JAVA中输入什么转义字符,使导出excle文档实现ALT+Enter的效果?或者有没有其他方法可以实现. 20 JAVA中输入什么转义字符,使导出excle文档实现ALT+Enter的效果?或者有没 ...
- Json对象在JS里面的处理
1.遍历Json对象 myJson = {"name":"nikita", "password":"1111"}; fo ...
- PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma'--问题解决
学习python+selenium总是遇到各种问题 代码如下: from selenium import webdriverbrowser = webdriver.PhantomJS() #浏览器初始 ...
- eMMC的MMC模式与SPI模式
MMC存贮卡可以分为MMC和SPI两种工作模式,MMC模式是标准的默认模式,具有MMC的全部特性.而SPI模式则是MMC存贮卡可选的第二种模式,这个模式是MMC协议的一个子集,主要用于只需要小数量的卡 ...
- 数据的加密传输——单片机上实现TEA加密解密算法
各位大侠在做数据传输时,有没有考虑过把数据加密起来进行传输,若在串口或者无线中把所要传的数据加密起来,岂不是增加了通信的安全性.常用的加密解密算法比如DES.RSA等,受限于单片机的内存和运算速度,实 ...
- WPF笔记(1.6 数据绑定)——Hello,WPF!
原文:WPF笔记(1.6 数据绑定)--Hello,WPF! 这个一节都是在讲一个数据绑定的示例.功用:输入姓和名,点击Add按钮,ListBox增加一条记录,永远是字符串“name: nick”:L ...
- C语言的本质(36)——makefile基础
除了Hello World这种极简单的程序之外,一般的程序都是由多个源文件编译链接而成的,这些源文件的处理步骤通常用Makefile来管理.makefile带来的好处就是--"自动化编译&q ...
- HDOJ-1010 Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...
- 总结 XSS 与 CSRF 两种跨站攻击
前言 在那个年代,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式.在这个年代, 参数化查询 [1] 已经成了普遍用法,我们已经离 SQL 注入很远了 ...
- mime大全收集
{"ai", "application/postscript"}, {"aif", "audio/x-aiff" ...