1、windows程序设计是种事件驱动方式的程序设计,主要基于消息的。当用户需要完成某种功能时,需要调用OS某种支持,然后OS将用户的需要包装成消息,并投入到消息队列中,最后应用程序从消息队列中取走消息并进行响应。
2、消息结构:
typedef struct tagMSG {     // msg
    HWND   hwnd;     //接收消息的窗口句柄。和哪个窗口相关联。
    UINT   message;  //消息标识。消息本身是什么。
    WPARAM wParam;   //消息的附加信息。具体取决于消息本身。  
    LPARAM lParam;
    DWORD  time;     //消息投递时间。 
    POINT  pt;       //消息投递时,光标在屏幕上的位置。 
} MSG; <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
3、消息队列:
每 个应用程序OS都为它建立一个消息队列,消息队列是个先进先出的缓冲区,其中每个元素都是一个消息,OS将生成的每个消息按先后顺序放进消息队列中,应用 程序总是取走当前消息队列中的第一条消息,应用程序取走消息后便知道用户的操作和程序的状态,然后对其处理即消息响应,消息响应通过编码实现。
4、使用VC编程除了良好的C基础外还需要掌握两方面:
一,消息本身。不同消息所代表的用户操作和应用程序的状态。
二,对于某个特定的消息来说,要让OS执行某个特定的功能去响应消息。
5、Window程序入口:
int WINAPI WinMain(
  HINSTANCE hInstance,  // 当前事例句柄。
  HINSTANCE hPrevInstance,  // 先前事例句柄。
  LPSTR lpCmdLine,      // 命令行指针
  int nCmdShow          // (窗口)显示的状态
);
说明:WinMain函数是Windows程序入口点函数,由OS调用,当OS启动应用程序的时候,winmain函数的参数由OS传递的。
6、创建一个完整的窗口需要经过下面四个操作步骤:
一、设计一个窗口类;如:WNDCLASS wndcls;
二、注册窗口类;    如:RegisterClass(&wndcls);
三、创建窗口;      如:CreateWindow(),CreateWindowEX();
四、显示及更新窗口。如:ShowWindow(),UpdateWindow();
说明:创建窗口的时候一定要基于已经注册的窗口类.
7、Windows提供的窗口类:
typedef struct _WNDCLASS {
    UINT    style;        //窗口的类型
    WNDPROC lpfnWndProc;  //窗口过程函数指针(回调函数)
    int     cbClsExtra; //窗口类附加字节,为该类窗口所共享。通常0。
    int     cbWndExtra; //窗口附加字节。通常设为0。
    HANDLE  hInstance;  //当前应用程序事例句柄。
    HICON   hIcon;      //图标句柄 LoadIcon();
    HCURSOR hCursor;    //光标句柄 LoadCursor();
    HBRUSH  hbrBackground; //画刷句柄 (HBRUSH)GetStockObject();
    LPCTSTR lpszMenuName;  //菜单名字
    LPCTSTR lpszClassName; //类的名字
} WNDCLASS;
8、窗口类注册:
ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass   // address of structure with class
                               // data
);
9、创建窗口:
HWND CreateWindow(
  LPCTSTR lpClassName,  // pointer to registered class name
  LPCTSTR lpWindowName, // pointer to window name
  DWORD dwStyle,        // window style
  int x,                // horizontal position of window
  int y,                // vertical position of window
  int nWidth,           // window width
  int nHeight,          // window height
  HWND hWndParent,      // handle to parent or owner window
  HMENU hMenu,          // handle to menu or child-window identifier
  HANDLE hInstance,     // handle to application instance
  LPVOID lpParam        // pointer to window-creation data
);
10、显示和更新窗口窗口:
BOOL ShowWindow(
  HWND hWnd,     // handle to window
  int nCmdShow   // show state of window
);
BOOL UpdateWindow(
  HWND hWnd   // handle of window
);
11、消息循环:
MSG msg;
while(GetMessage(&msg,...))    //从消息队列中取出一条消息
{
 TranslateMessage(&msg); //进行消息(如键盘消息)转换
 DispatchMessage(&msg); //分派消息到窗口的回调函数处理,(OS调用窗口回调函数进行处理)。
}
其中:
//**The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure.
//**If the function retrieves a message other than WM_QUIT, the return value is nonzero.If the function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return value is -1.
BOOL GetMessage(
  LPMSG lpMsg,         // address of structure with message
  HWND hWnd,           // handle of window
  UINT wMsgFilterMin,  // first message
  UINT wMsgFilterMax   // last message
);

//The TranslateMessage function translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.
BOOL TranslateMessage(
  CONST MSG *lpMsg   // address of structure with message
);
//The DispatchMessage function dispatches a message to a window procedure.
LONG DispatchMessage(
  CONST MSG *lpmsg   // pointer to structure with message
);

12、窗口过程函数(回调函数)原型:
The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a placeholder(占位符) for the application-defined function name.
LRESULT CALLBACK WindowProc(  //这里WindowProc是个代号名字。
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);
说明:两种函数调用约定(__stdcall 和 __cdecl):
#define CALLBACK    __stdcall
//__stdcall 标准调用预定,是PASCAL 调用约定,象DELPHI使用的就是标准调用约定
#define WINAPIV     __cdecl 
// __cdecl 是C 语言形式的调用约定。

主要区别:函数参数传递顺序 和 对堆栈的清除上。
问 题:除了那些可变参数的函数调用外,其余的一般都是__stdcall约定。但 C/C++编译默然的是__cdecl约定。所以如果在VC等环境中调用__stdcall约定的函数,必须要在函数声明的时加上 __stdcall 修饰符,以便对这个函数的调用是使用__stdcall约定(如使用DELPHI编写的DLL时候)。
(VC中可通过这途径修改:project|settings..|c/c++|...)

在窗口过程函数中通过一组switch语句来对消息进行处理:
如:
LRESULT CALLBACK WindowProc( 
  HWND hwnd,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam  
)
{
    switch(uMsg)
    {
 case WM_PAINT:
  ...
  break;
 case ...
  break;
 case WM_CLOSE:
  //DestroyWindow(hwnd);
   //销毁窗口,并发送WM_DESTROY消息。
  break;
 case WM_DESTROY:
  //PostQuitMessage(0);
  //发送WM_QUIT消息到消息队列中,请求终止。
         //GetMessage()取到WM_QUIT消息后,返回0,退出消息循                //   环,从而终止应用程序。
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 //用缺省的窗口过程处理我们不感兴趣的消息(其它消息)。
 //这是必须的。
    }//switch
 return 0;
}//WindowProc
13、DestroyWindow()函数和PostQuitMessage()函数原型:
//**The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages。
BOOL DestroyWindow(
  HWND hWnd   // handle to window to destroy
);
//**The PostQuitMessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.
//**The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates(预示,通知) to the system that the thread is requesting to quit at some time in the future.
When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system.
VOID PostQuitMessage(
  int nExitCode   // exit code
);
14、关于DC句柄获取:
a)使用BeginPaint(),EndPaint()对。注意只能在响应WM_PAINT消息时使用。
b)使用GetDc(),ReleaseDC()对。注意他们不能在响应WM_PAINT中使用。
 
 

本文来自CSDN博客,转载请标明出处:http://download.csdn.net/down/637008/s10086

转载于:https://blog.51cto.com/debugger/166716

孙鑫VC++讲座笔记(一)相关推荐

  1. 孙鑫VC++讲座笔记-(6)菜单编程

    1,弹出菜单(Pop-up)是不能用来作命令响应的. 2,MFC中菜单项消息如果利用ClassWizard来对菜单项消息分别在上述四个类中进行响应,则菜单消息传递顺序:View类--Doc类--CMa ...

  2. 孙鑫VC++学习笔记(转载至程序员之家--虎非龙)[11--15] .

    第11课 1.创建4个菜单,为其添加消息响应,用成员变量保存绘画类型.添加LButtonDown和Up消息. 2.当窗口重绘时,如果想再显示原先画的数据,则需要保存数据.为此创建一个新类来记录绘画类型 ...

  3. 孙鑫VC++学习笔记(转载至程序员之家--虎非龙)[11--15]

    第11课 1.创建4个菜单,为其添加消息响应,用成员变量保存绘画类型.添加LButtonDown和Up消息. 2.当窗口重绘时,如果想再显示原先画的数据,则需要保存数据.为此创建一个新类来记录绘画类型 ...

  4. 孙鑫VC学习笔记:第七讲

    七.对话框 2006年8月5日 14:25 因为笔记是用OneNote做的,上传以后为看不到图片,于是我截图放到相册上面, 相册地址为:http://photo.163.com/photos/good ...

  5. mfc编程 孙鑫_孙鑫VC++视频教程笔记-(3)MFC程序框架的剖析 附1-SDI程序流程图

    1,寻找WinMain人口: 在安装目录下找到MFC文件夹下的SRC文件夹,SRC下是MFC源代码. 路径:MFC|SRC|APPMODUL.CPP: _tWinMain(HINSTANCE hIns ...

  6. 孙鑫VC++深入详解第三章学习笔记

    第三章 3.1创建MFC AppWizard 如何利用vs2019创建MFC应用见参考文献[1] 需要注意的地方有 [1] 创建MFC单文档应用程序 [2]开启类视图窗口 3.2基于MFC的程序框架剖 ...

  7. 孙鑫VC++深入详解第二章学习笔记

    第二章 掌握C++ 2.1 从结构到类 2.1.1 结构体的定义 C++相比于C的特性:封装性.继承性.多态性: 对象具有状态和行为,状态保存在成员变量中,行为通过函数实现: 标准输入输出流对象:ci ...

  8. 孙鑫VC++深入详解第一章学习笔记

    第一章 Windows程序内部运行机制 1.1 API和SDK API:Windows操作系统提供给应用程序编程的接口. SDK(软件开发包):用于开发的所有资源的集合. 1.2 窗口和句柄 窗口 句 ...

  9. 孙鑫VC++深入详解笔记

    前言:最近感觉技术提升提来很吃力,主要还是因为以前的基础没有打牢,特别是多线程和数据库方面,所有准备重新学习一下基础. 如下文章转载自:http://www.cnblogs.com/gaojun/ar ...

  10. 孙鑫VC学习系列教程

    教程简介 1.循序渐进 从Win32SDK编程开始讲解,帮助大家理解掌握Windows编程的核心 -- 消息循环机制. 2.通俗易懂 编程语言枯燥难懂,然而通过孙鑫老师形象化的讲解,Windows和M ...

最新文章

  1. java数据库视图工具_数据库视图工具类
  2. 【算法】一个简单的K近邻(KNN)原理
  3. linux -cpu
  4. 【汇编语言学习之路】第一章 汇编语言核心方法论
  5. django使用LDAP验证
  6. 程序员上班都在做什么?
  7. Qt文档阅读笔记-编写应用脚本解析与实例
  8. 基于TCP和多线程实现无线鼠标键盘-Socket(2)
  9. deepin应用分发_Deepin Dock隐藏功能等功能说明
  10. 节点大小可变的环形队列实现
  11. android sdk manager 更新失败
  12. 程序安全性之配置文件安全
  13. 基于STM32构建EtherCAT主站(SOEM方案)2
  14. 使用Ehome协议将设备接入EasyCVR无法注册成功原因排查
  15. 网络安全——终端安全
  16. Proteus 数字示波器
  17. 厉害了!小学生用大数据研究苏轼,结论出人意料
  18. 魔王语言解释器C语言,[数据结构]魔王语言解释c语言实现
  19. Linux基础3-实用进阶
  20. 小米2S手机开启开发者选项

热门文章

  1. oracle函数 TO_MULTI_BYTE(c1)
  2. BZOJ 1013: [JSOI2008]球形空间产生器sphere
  3. 在Unity中添加图标
  4. div+css格式规范
  5. NBA历史上50大巨星2
  6. 算法 判断多个点是否在同一圆周线上_广州灵活计费自动出盘机技术方案大盘点...
  7. DICOM医学图像处理:Orthanc Plugin SDK实现WADO服务
  8. 【坐在马桶上看算法】算法12:堆——神奇的优先队列(下)
  9. 第一次想真正的认识自己
  10. WinForm中的特殊窗体效果:渐变窗口和信息提示窗口