当前位置:早雪网网络学院编程文档其他语言 → windowAPI笔记(1)

windowAPI笔记(1)

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:55:12
window class for subsequent use in calls to the CreateWindow or CreateWindowEx function.(RegisterClass函数来Register一个窗口类为后面的CreateWindow or CreateWindowEx 函数来调用)

大致意思是如果不Register那么CreateWindow or CreateWindowEx 函数就无法创建你想要的窗口

以前对CreateWindow or CreateWindowEx 也很熟悉了,这里就不在累赘了.

ShowWindow在MSDN的"注意"上上是这样说明的

To perform certain special effects when showing or hiding a window, use AnimateWindow.

The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter.

As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in the structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations:

  • Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set.
  • Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible.

这里重点要提的是UpdateWindow:

The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty. The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent.

当窗口收到WM_PAINT 消息的时候,如果窗口的更新区不是空的窗口被更新,WM_PAINT 消息被至于指定进程的窗口,放入该进程的消息队列.如果更新区是空的,那么将没有消息被发送,即WM_PAINT 不会被至于消息队列.1)WM_PAINT:

以前对这个WM_PAINT 的概念很模糊,现在感觉有个比较清醒的认识了

MSDN上是这样申明的

The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window. The message is sent when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage function when the application obtains a WM_PAINT message by using the GetMessage or PeekMessage function.

当系统或者应用程序发出一个重绘一个应用程序的一个窗口的时候WM_PAINT的消息被送出,即当UpdateWindow or RedrawWindow 函数被调用时,WM_PAINT

然后被GetMessage or PeekMessage 捕获.然后被DispatchMessage 处理,送入消息队列.

调用UpdateWindow之后,窗口就出现在视讯显示器上。程序现在必须准备读入使用者用键盘和鼠标输入的数据。Windows为当前执行的每个Windows程序维护一个「消息队列」。在发生输入事件之后,Windows将事件转换为一个「消息」并将消息放入程序的消息队列中。

while       (GetMessage (&msg, NULL, 0, 0))
       
{
    TranslateMessage (&msg) ;
       
    DispatchMessage (&msg) ;
}
注意这里的GetMessage (&msg, NULL, 0, 0)是从消息队列中取出一个消息

以前我错误地认为它是来接受消息的

那么消息是怎么样被放入消息队列的呢?

还是引用上面的一句话:The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window

//当系统或者应用程序发出一个重绘一个应用程序的一个窗口的时候WM_PAINT的消息被发出

接下来的问题又出现了,我们怎么知道什么时候应该重绘一个应用程序的窗口呢?

这个可以参看侯捷的<<windows程序设计>>的 输出文字 那章的第一节.

最后一点不太明白的就是退出函数

WM_DESTROY消息

程序通过PostQuitMessage以标准方式响应WM_DESTROY消息

该函数在程序的消息队列中插入一个WM_QUIT消息。前面提到过,GetMessage对于除了WM_QUIT之外的从消息队列中取出的所有消息都传回非0值。而当GetMessage得到一个WM_QUIT消息时,它传回0。这将导致WinMain退出消息循环

传回0以为着while (GetMessage (&msg, NULL, 0, 0))消息循环将被停止.

这段程序的函数调用:

HELLOWIN至少调用了18个Windows函数。下面以它们在HELLOWIN中出现的次序列出这些函数以及各自的简明描述:

  • LoadIcon 加载图标供程序使用。
     
  • LoadCursor 加载鼠标光标供程序使用。
     
  • GetStockObject 取得一个图形对象(在这个例子中,是取得绘制窗口背景的画刷对象)。
     
  • RegisterClass 为程序窗口注册窗口类别。
     
  • MessageBox 显示消息框。
     
  • CreateWindow 根据窗口类别建立一个窗口。
     
  • ShowWindow 在屏幕上显示窗口。
     
  • UpdateWindow 指示窗口自我更新。
     
  • GetMessage 从消息队列中取得消息。
     
  • TranslateMessage 转译某些键盘消息。
     
  • DispatchMessage 将消息发送给窗口消息处理程序。
     
  • PlaySound 播放一个声音文件。
     
  • BeginPaint 开始绘制窗口。
     
  • GetClientRect 取得窗口显示区域的大小。
     
  • DrawText 显示字符串。
     
  • EndPaint 结束绘制窗口。
     
  • PostQuitMessage 在消息队列中插入一个「退出程序」消息。
     
  • DefWindowProc 执行内定的消息处理。

 

上一页  [1] [2] 

[数据载入中...] [返回上一页] [打 印]