走进windows编程的世界-----入门篇
2014-05-19 22:13
585 查看
1 Windows编程基础
1.1Win32应用程序基本类型
1) 控制台程序不需要完善的windows窗口,可以使用DOS窗口方式显示
2) Win32窗口程序
包含窗口的程序,可以通过窗口与程序进行交互
3) Win32库程序
提供已有的代码,供其他程序使用
动态库(DLL):是在执行的时候可以加载的。
静态库(LIB):是在编译链接是使用的程序,成为当前程序的一部分。
1.2头文件和库
1.2.1头文件
基本的头文件windows.h包含了windows常用的定义等,其他,还包含了一些其他的头文件,比如:1、 windef.h: 定义个中数据类型
2、 winbase.h:定义了kernel的相关函数
3、 wingdi.h:定义了绘图和文件
4、 winuser.h:窗口及空间
5、 winnt.h:提供了Unicode的支持
1.2.2库
1、Kernel32.lib :提供进程线程内存等等API函数定义2、User32.lib :窗口及界面的API函数
3、Gdi32.lib :提供绘图、文字等API函数支持
1.1.1实例helloworld
开发环境VS2010,编译过程中可能会报错误,可能是编码的问题:需要进行一些配置的设置:项目--属性---配置属性-常规:字符集 设置为未设置
/*File : helloworld.cpp *Auth : sjin *Date : 20140519 *Mail : 413977243@qq.com */ #include <iostream> #include <Windows.h> int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { MessageBox(NULL,"hello world..","first win32 test",MB_OK); return 0; }
运行结果:
1.1编写第一个窗口程序
处理相关的流程如下:1、窗口入口函数WinMain
2、窗口处理函数
3、注册窗口类型
4、创建窗口
5、显示窗口
6、消息处理
7、程序退出
看下面的代码:
/*File : helloworld.cpp *Auth : sjin *Date : 20140519 *Mail : 413977243@qq.com */ #include <iostream> #include <Windows.h> using namespace std; HINSTANCE g_hInst = NULL; //2窗口处理函数 /*函数功能:该函数是一个应用程序定义的函数。它处理发送给窗口的消息 * 当窗口处理消息消息时,系统调用该函数 *参数: *hwnd:指向窗口的句柄。 *uMsg:指定消息类型。 *wParam:指定其余的、消息特定的信息。该参数的内容与UMsg参数值有关。 *IParam:指定其余的、消息特定的信息。该参数的内容与uMsg参数值有关。 */ LRESULT CALLBACK WndProc(HWND hwnd,/**/ UINT nMsg, WPARAM wParam, LPARAM IParam) { switch( nMsg ){ case WM_DESTROY://窗口销毁的消息 //发送消息退出函数 PostQuitMessage(0); return 0; default: break; } //调用 return DefWindowProc(hwnd,nMsg,wParam,IParam); } //3注册窗口类型 /* * */ BOOL MyRegister( LPSTR pszClassName) { WNDCLASS wc = {'\0'}; wc.style = CS_VREDRAW | CS_HREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = g_hInst; wc.hIcon = LoadIcon(g_hInst,MAKEINTRESOURCE(100));; wc.hCursor = NULL; wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE); wc.lpszMenuName = NULL; wc.lpszClassName = pszClassName; ATOM natom = RegisterClass(&wc); if(0 == natom){ MessageBox(NULL,"Register Failed","Error!",MB_OK | MB_ICONWARNING); }else{ //MessageBox(NULL,"Register Successed","Successed!",MB_OK); } return TRUE; } //4 窗口创建 HWND myCreateWindow(LPSTR pszClassName) { //创建窗口 HWND hwnd =CreateWindow(pszClassName, "HelloWnd", WS_OVERLAPPEDWINDOW, 100, 100, 300, 500, NULL, NULL, g_hInst, NULL); if(0 == hwnd){ MessageBox(NULL,"CreateWindow Failed","Error!",MB_OK | MB_ICONWARNING); }else{ //MessageBox(NULL,"CreateWindow Successed","Successed!",MB_OK); } return hwnd; } //5 显示窗口 void Displaywnd(HWND hwnd) { //显示 ShowWindow(hwnd,SW_SHOW); //刷新 UpdateWindow(hwnd); } //6 消息处理 void Message() { MSG msg = {'\0'}; //消息循环处理,获取消息 while(GetMessage(&msg,NULL,0,0)){ //派发消息 DispatchMessage(&msg);/*会执行窗口处理函数*/ } } //1、入口函数 int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { g_hInst = hInstance; //注册窗口类型 MyRegister("my first win32"); //常见注册类型的窗口 HWND hwnd = myCreateWindow("my first win32"); //显示窗口 Displaywnd(hwnd); //消息处理 Message(); return 0; }
资源文件
/*File : helloworld.rc
*Auth : sjin
*Date : 20140519
*Mail : 413977243@qq.com
*/
100 ICON "2222.ico"
图片如下:
相关文章推荐
- 走进windows编程的世界-----入门篇
- 走进windows编程的世界-----消息处理函数(2)
- 走进windows编程的世界-----对话框、文本框、按钮
- 走进windows编程的世界-----消息处理函数(3)
- GDI+入门——带你走进Windows图形的世界
- 走进windows编程的世界-----消息处理函数(2)
- 走进windows编程的世界-----绘图相关
- 走进windows编程的世界-----对话框、文本框、button
- 走进windows编程的世界-----windows线程
- 走进windows编程的世界-----窗口的注册及创建
- 走进windows编程的世界-----位图及映射模式
- GDI+入门——带你走进Windows图形的世界
- 走进windows编程的世界-----windows进程
- 走进windows编程的世界-----窗体的注冊及创建
- 走进windows编程的世界-----画图相关
- GDI+入门――带你走进Windows图形的世界
- 走进windows编程的世界-----windows进程
- 走进windows编程的世界-----消息处理函数(4)
- 走进windows编程的世界-----消息处理函数(1)
- GDI+入门——带你走进Windows图形的世界