您的位置:首页 > 编程语言

走进windows编程的世界-----入门篇

2017-06-23 14:36 543 查看

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"


图片例如以下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: