代码改变世界

VC++深入详解(孙鑫)(第一章 Windows 程序内部运行机制)学习笔记

2011-11-05 17:59  江上渔者  阅读(239)  评论(0编辑  收藏  举报

 1.API(Application Programming Interface,应用程序编程接口);

 2.SDK(Software Development Kit,软件开发包);

 3.窗口:窗口是屏幕上的一块矩形区域,是Windows应用程序与用户进行交互的接口。

 4.句柄(HANDLE):句柄是Windows程序资源的标识号。

  常见的句柄:HWND(窗口句柄)、HICON(图标句柄)、HCURSOR(光标句柄)、HBRUSH(画刷句柄);

 5.消息与消息队列

  在Windows程序中,消息是由MSG结构体来表示的。MSG结构体的定义如下(参考MSDN):

typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG, *PMSG, *LPMSG;

  消息队列是在消息的传输过程中保存消息的容器。

  Windows将产生的消息依次放到消息队列中,而应用程序则通过一个消息循环不断地从消息队列中取出消息,并进行响应。这种消息机制,就是Windows程序运行的机制。

6.WinMain函数

  WinMain函数的原型声明如下: 

int WINAPI wWinMain(
HINSTANCE hInstance, // handle to an instance
HINSTANCE hPrevInstance, // has no meaning. It was used in 16-bit Windows, but is now always zero.
PWSTR pCmdLine, // contains the command-line arguments as a Unicode string.
int nCmdShow // a flag that says whether the main application window will be minimized, maximized, or shown normally.
);

7.窗口的创建过程

  1. 设计窗口类
  2. 注册窗口类
  3. 创建窗口
  4. 显示及更新窗口

  窗口的特征是由WNDCLASS结构体定义的,定义如下:

typedef struct tagWNDCLASS {
UINT style; // The class style(s).
WNDPROC lpfnWndProc; // A pointer to the window procedure.
int cbClsExtra; // The number of extra bytes to allocate following the window-class structure.
int cbWndExtra; // The number of extra bytes to allocate following the window instance.
HINSTANCE hInstance; // A handle to the instance that contains the window procedure for the class.
HICON hIcon; // A handle to the class icon.If this member is NULL, the system provides a default icon.
HCURSOR hCursor; // A handle to the class cursor.
HBRUSH hbrBackground; // A handle to the class background brush.
LPCTSTR lpszMenuName; // The resource name of the class menu, as the name appears in the resource file.
LPCTSTR lpszClassName; // A pointer to a null-terminated string or is an atom.
} WNDCLASS, *PWNDCLASS;

  窗口过程函数的定义如下:

LRESULT CALLBACK WindowProc(
__in HWND hwnd, // A handle to the window.
__in UINT uMsg, // The message.
__in WPARAM wParam, // Additional message information.
__in LPARAM lParam // Additional message information.
);

  注册窗口类的函数定义如下:

ATOM WINAPI RegisterClass(
__in const WNDCLASS *lpWndClass // A pointer to a WNDCLASS structure.
);

  创建窗口的函数定义如下:

HWND WINAPI CreateWindow(
__in_opt LPCTSTR lpClassName, // The registered class name.
__in_opt LPCTSTR lpWindowName, // The window name.
__in DWORD dwStyle, // The style of the window being created.
__in int x, // The initial horizontal position of the window.
__in int y, // The initial vertical position of the window.
__in int nWidth, // The width, in device units, of the window.
__in int nHeight, // The height, in device units, of the window.
__in_opt HWND hWndParent, // A handle to the parent or owner window of the window being created.
__in_opt HMENU hMenu, // A handle to a menu, or specifies a child-window identifier depending on the window style.
__in_opt HINSTANCE hInstance, // A handle to the instance of the module to be associated with the window.
__in_opt LPVOID lpParam // Pointer to window-creation data.
);

  显示窗口的函数定义如下:

BOOL WINAPI ShowWindow(
__in HWND hWnd, // A handle to the window.
__in int nCmdShow // Controls how the window is to be shown.
);

  更新窗口的函数定义如下:

BOOL UpdateWindow(
__in HWND hWnd // Handle to the window to be updated.
);

  从消息队列中获取消息示例:

BOOL bRet;
MSG msg;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


8.Windows程序示例

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK MyWndProc(
HWND hWnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
// 设计窗口类
WNDCLASS wndcls;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = MyWndProc;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hInstance = hInstance;
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR); // LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SAMPLE1));
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // (HBRUSH)(COLOR_WINDOW+1);
wndcls.lpszMenuName = NULL; // MAKEINTRESOURCE(IDC_SAMPLE1);
wndcls.lpszClassName = "MyClassName";

// 注册窗口类
RegisterClass(&wndcls);

// 创建窗口类
HWND hWnd;
hWnd = CreateWindow("MyClassName", "MyWindowName", WS_OVERLAPPEDWINDOW,
0, 0, 600, 400, NULL, NULL, hInstance, NULL);

// 显示和更新窗口
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);

// 消息循环
BOOL bRet;
MSG msg;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
return -1;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return 0;
}

LRESULT CALLBACK MyWndProc(
HWND hWnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
{
char szChar[20];
sprintf(szChar, "char is %c", char(wParam));
MessageBox(hWnd, szChar, "WM_CHAR", 0);
}
break;
case WM_LBUTTONDOWN:
{
MessageBox(hWnd, "mouse clicked", "WM_LBUTTONDOWN", 0);
HDC hDC;
hDC = GetDC(hWnd);
TextOut(hDC, 0, 50, "WM_LBUTTONDOWN", strlen("WM_LBUTTONDOWN"));
ReleaseDC(hWnd, hDC);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC;
hDC=BeginPaint(hWnd, &ps);
TextOut(hDC, 0, 0, "WM_PAINT", strlen("WM_PAINT"));
EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
{
if(IDYES == MessageBox(hWnd, "是否真的结束?", "WM_CLOSE", MB_YESNO))
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}