`
bcyy
  • 浏览: 1830360 次
文章分类
社区版块
存档分类
最新评论

Windows编程基础知识点

 
阅读更多

什么是窗口(window)?

对于从编程的角度解析,不要简单地认为是一个程序的窗口,而是如下面定义:

think of a window as a programming construct that:

  • Occupies a certain portion of the screen.
  • May or may not be visible at a given moment.
  • Knows how to draw itself.
  • Responds to events from the user or the operating system
----MSDN

什么是句柄(handle):

是一个号码,操作系统使用这一的号码识别对象。操作系统有一个很大的表,包含了所有创建的windows的句柄,操作系统就是通过查找这一的表找到windows的。

记住句柄不是指针。


Windows入口:

每个Windows程序都有一个入口点:WinMain或者wWinMain

为什么编译器知道激活wWinMain而不是main呢?因为Microsoft C runtime库(CRT)调用WinMain或者wMinMain函数,从而调用main。


什么是WindowProc :

这个是用户定义的函数,不过操作系统已经给好了接口,用户定义window的行为:外观,响应消息等等。

程序并不是直接调用这个函数的,操作系统是通过传递消息给这个函数来和我们的程序沟通的。

DispatchMessage函数使得系统激活WindowProc函数,每次去掉一个消息,由这个函数处理。


window class:

每一个窗口(window)都必须和一个window class关联。window class是一个提供给操作系统内部使用的数据结构。


事件(event)

分为:

1 用户事件

2 操作系统事件

操作系统使用消息传递模式和我们的程序沟通。一个消息其实就是一个数字代码指示着某一事件。

例如,如果用户点击鼠标右键,窗口接收到一个消息代码0x0201:

#define WM_LBUTTONDOWN    0x0201

消息过程例子:

For example, suppose the user presses the left mouse button. This causes a chain of events:

  1. The operating system places a WM_LBUTTONDOWN message on the message queue.
  2. Your program calls the GetMessage function.
  3. GetMessage pulls theWM_LBUTTONDOWN message from the queue and fills in theMSG structure.
  4. Your program calls the TranslateMessage and DispatchMessage functions.
  5. Inside DispatchMessage, the operating system calls your window procedure.
  6. Your window procedure can either respond to the message or ignore it.
----MSDN

Posted Messages versus Sent Messages(发送队列消息和直接发送消息)

  • Posting a message means the message goes on the message queue, and is dispatched through the message loop (GetMessage andDispatchMessage).
  • Sending a message means the message skips the queue, and the operating system calls the window procedure directly.
----MSDN


窗口关闭:

用户关闭窗口的时候,会引发一系列消息。如果不处理的话,window就没有真正关闭。

到底有什么一系列的消息呢?听起来很可怕,呵呵。

WM_CLOSEWM_DESTROY和 WM_QUIT三个消息;

其中WM_QUIT是一个特别的消息,让GetMessage返回0,表示消息循环结束。也因为它是最后发生的消息,所以我们的消息处理程序永远也接受不到WM_QUIT这个消息,也就无法在WindProc中扑捉这个消息。

当用户关闭窗口的时候,windows自动发送WM_CLOSE和WM_DESTROY消息;但是需要我们手动调用PostQuitMessage(0),指明要发送WM_QUIT消息,才能Exit wWinMain,否则系统会停留在wWinMain中。

下面的图来自MSDN,实在把三个消息的关系画的一目了然,不忍割爱,故此贴上来了:



下面是基本窗口的代码:

#include<Windows.h>

//wParam and lParam contain additional data that pertains to the message. The exact meaning depends on the message code.
/*
For example, the documentation for the WM_SIZE message states that:
wParam is a flag that indicates whether the window was minimized, maximized, or resized. 
lParam contains the new width and height of the window as 16-bit values packed into one 32- or 64-bit number. You will need to perform some bit-shifting to get these values. Fortunately, the header file WinDef.h includes helper macros that do this.
*/
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_SIZE:
		{
			int width = (unsigned long)lParam & 0xff;//LOWORD(lParam);
			int height = ((unsigned long)lParam >>16)& 0xff;//HIWORD(lParam);
		}
		return 0;
		
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);

			FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOWFRAME));

			EndPaint(hwnd, &ps);
		}
		return 0;

	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;
		//MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL);

	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		  PSTR lpCmdLine, INT nCmdShow)
{
	const wchar_t className[] = L"Fundamental Windows Programming";

	//1. Register Class
	WNDCLASS wc = {};
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.lpszClassName = className;

	RegisterClass(&wc);

	//2. Create Window
	HWND hwnd = CreateWindowEx(
		0,
		className,
		L"Fundamental Title",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (hwnd == NULL) return 0;

	//The nCmdShow parameter can be used to minimize or maximize a window.
	ShowWindow(hwnd, nCmdShow); 

	//Message Loop
	MSG msg = {};
	//This function removes the first message from the head of the queue. If the queue is empty, the function blocks until another message is queued. 
	while (GetMessage(&msg, NULL, 0, 0))
	{
		//The TranslateMessage function is related to keyboard input; it translates keystrokes (key down, key up) into characters. You don't really need to know how this function works; just remember to call it right before DispatchMessage.
		TranslateMessage(&msg);
		//The DispatchMessage function tells the operating system to call the window procedure of the window that is the target of the message. In other words, the operating system looks up the window handle in its table of windows, finds the function pointer associated with the window, and invokes the function.
		DispatchMessage(&msg);
	}

	return 0;
}









分享到:
评论

相关推荐

    windows编程基础

    本章目的是介绍WINDOWS编程基础。在本章结束时,你应该能够很好的工作了,虽燃可能是简单的WINDOWS程序。你需要有C语言的基础知识,我很少将C++的代码扩充到程序中。当然,由于WINDOWS本身就是面向对象的,一点类...

    Windows编程新手教程

    本教程主要包括了windows开发基础知识,以及注意点和技巧

    《楚狂人Windows驱动编程基础教程》

    楚狂人写的一本关于驱动开发的很好的学习教程,里面的知识点很多。对初学者很好帮助。

    [游戏开发新手入门之Windows编程].ant3000著

    本文目的是介绍Windows编程基础。在本文结束时,你应该能够很好 的工作了,虽然可能是简单的WIindows程序。你需要有C语言的基础知识,我很少将C++的代码扩充到程序中。当然,由于Windows本身就是面向 对象的,一点...

    C#网络应用编程基础(word版)

    该书深入浅出地介绍了C#语言基本知识和面向对象编程基础、C# Windows窗体客户端应用编程、C# Web窗体应用编程以及在文件管理、Internet应用、SQL Server数据库、图形图像和水晶报表等方面的应用。全书语言简洁,重点...

    Windows网络编程

    3457.5.1 枚举TCP和UDP连接状态 3457.5.2 Ping实例 3517.6 小结 357第8章 MFC Winsock高级编程 3598.1 Web基础知识 3598.1.1 客户端 3598.1.2 服务器 3608.1.3 HTTP 3628.2 HTTP服务器设计 3638.2.1 同步...

    win32课程知识点

    Windows编程 1 Windows编程基础 2 WIndows字符 3 窗口处理 4 消息 5 绘图 6 对话框 7 控件

    天书夜读:从汇编语言到Windows内核编程(完整版一)

    这一部分包括指令分析、硬件基础知识、内核Hook的实际开发练习,以及将完成一个用到内核Hook的有趣的实例,这个实例有助于计算机阻挡各种病毒和木马的侵袭。  此外,本部分还包括特殊的一章,涉及如何巧妙地编写...

    天书夜读:从汇编语言到Windows内核编程(完整版 二)

    这一部分包括指令分析、硬件基础知识、内核Hook的实际开发练习,以及将完成一个用到内核Hook的有趣的实例,这个实例有助于计算机阻挡各种病毒和木马的侵袭。  此外,本部分还包括特殊的一章,涉及如何巧妙地编写...

    寒江独钓-Windows内核安全编程(高清完整版).part7

    阅读本书,需要读者有C语言、数据结构、操作系统和计算机网络的基础知识。 目录: 封面 -25 扉页 -24 内容简介 -23 序 -22 关于本书作者和贡献者 -20 前言 -18 阅读注意 -16 目录 -12 正文 1 第1章 内核上机指导 1...

    寒江独钓-Windows内核安全编程(高清完整版).part4

    阅读本书,需要读者有C语言、数据结构、操作系统和计算机网络的基础知识。 目录: 封面 -25 扉页 -24 内容简介 -23 序 -22 关于本书作者和贡献者 -20 前言 -18 阅读注意 -16 目录 -12 正文 1 第1章 内核上机指导 1...

    寒江独钓-Windows内核安全编程(高清完整版).part1

    阅读本书,需要读者有C语言、数据结构、操作系统和计算机网络的基础知识。 目录: 封面 -25 扉页 -24 内容简介 -23 序 -22 关于本书作者和贡献者 -20 前言 -18 阅读注意 -16 目录 -12 正文 1 第1章 内核上机指导 1...

    java学习资源知识点整理

    本资源包涵盖了Java知识、Linux和Windows等多个领域的内容和知识点,旨在帮助开发者全面提升技术水平。内容涵盖了Java基础语法、面向对象编程、异常处理、集合框架等核心知识点,同时也提供了Linux、Windows系统需要...

    寒江独钓-Windows内核安全编程(高清完整版).part5

    阅读本书,需要读者有C语言、数据结构、操作系统和计算机网络的基础知识。 目录: 封面 -25 扉页 -24 内容简介 -23 序 -22 关于本书作者和贡献者 -20 前言 -18 阅读注意 -16 目录 -12 正文 1 第1章 内核上机指导 1...

    寒江独钓-Windows内核安全编程(高清完整版).part6

    阅读本书,需要读者有C语言、数据结构、操作系统和计算机网络的基础知识。 目录: 封面 -25 扉页 -24 内容简介 -23 序 -22 关于本书作者和贡献者 -20 前言 -18 阅读注意 -16 目录 -12 正文 1 第1章 内核上机指导 1...

    游戏编程起源

    有关游戏编程的东西,可以看看,有帮助,本章目的是介绍WINDOWS编程基础。在本章结束时,你应该能够很好的工作了,虽燃可能是简单的WINDOWS程序。你需要有C语言的基础知识,我很少将C++的代码扩充到程序中。当然,...

    从汇编语言到Windows内核编程

    这一部分包括指令分析、硬件基础知识、内核Hook的实际开发练习,以及将完成一个用到内核Hook的有趣的实例,这个实例有助于计算机阻挡各种病毒和木马的侵袭。 此外,本部分遂包括特殊的一章,涉及如何巧妙地编写代码...

    汉语编程基础教程(汉语程序设计----沈志斌)

    学习汉语编程必须明白几点: 1-中文和英文的地位是平等的,在计算机中也一样,不要褒英贬汉; 2-计算机的机器码是0和1,他既可以和英文对应也可以和中文对应; 3-英文编程之所以占主导地位,是因为世界体系格局...

    Qt4编程.pdf

    3.2.3Windows编程基础 3.3Linux编程基础 3.3.1你必须掌握的技能 3.3.2文件系统管理 3.3.3XWindow系统 3.3.4常用命令 3.3.5Shell应用 3.3.6使用库程序 3.3.7使用vi 3.3.8使用GCC 3.3.9使用GDB 3.4Mac编程基础 3.4.1你...

Global site tag (gtag.js) - Google Analytics