windows编程介绍、windows 库和头文件介绍
exe文件:.exe,可执行文件
静态库文件:*.lib,调用时,源代码嵌入到调用位置
动态库文件:*.dll,调用时,函数地址被传入
--写编译软件,要懂编译原理.
windows 库和头文件
kernel32.dll-提供了核心的API,例如进程、线程、内存管理等
user32.dll-提供了窗口消息等API
gdi32.dll-绘图相关的API
windows.h-所有windows头文件的集合
windef.h-windows数据类型
winbase.h-kernel32的API
wingdi.h-gdi32的API
winuser.h-user32的API
winnt.h-UNICODE字符集支持
命令行编译:
cl.exe /c *.cpp -编译成*.obj 文件
手写.rc文件,格式:100 ICON *.ico
rc.exe *.rc (将资源编译成.res文件)
link.exe *.obj *.res user32.lib -链接成*.exe文件
makefile 文件编译,可以进行依赖,先编译依赖项
1、编辑:***.mak,写入内容示例:
main:clean
cl.exe /c ***.cpp
rc.exe ***.rc
link.exe ***.obj ***.res user32.lib
clean:
del * obj
del *.res
del *.exe
2、执行***.mak进行编译链接:
nmake ***.mak
3、执行删除中间文件命令
nmake /f ***.mak clean
编写窗口程序步骤:
1、定义WinMain函数
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hinstance=hInstance;
// TODO: Place code here.
if(Register("main",WinProc)==0)
{
MessageBox(NULL,"注册失败","提示",MB_OK);
return 0;
}
HWND hwnd= CreateMain("main","pro1");
Display(hwnd);
MSGdeal();
return 0;
}
2、定义窗口处理函数 WindowProc
LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
3、注册窗口类 RegisterClass
BOOL Register(LPSTR lpClassName,WNDPROC wndproc)
{
WNDCLASSEX wce={0};
wce.cbSize=sizeof(wce);
wce.cbClsExtra=0;
wce.cbWndExtra=0;
wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wce.hCursor=NULL;
wce.hIcon=NULL;
wce.hIconSm=NULL;
wce.hInstance=g_hinstance;
wce.lpfnWndProc=wndproc;
wce.lpszClassName=lpClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW;
ATOM atom= RegisterClassEx(&wce);
if(atom==0){
MessageBox(NULL,"注册失败","info",MB_OK);
return FALSE;
}
return TRUE;
}
4、创建窗口 CreateWindow
HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
{
HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hinstance,NULL);
return hwnd;
}
5、显示窗口 ShowWindow/UpdateWindow
void Display(HWND hwnd)
{
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
}
6、消息循环 GetMessage/TranslateMessage/DispatchMessage
void MSGdeal()
{
MSG msg={0};
//GetMessage 第二个参数为句柄,限制获取消息的范围,为空则不限制
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);//翻译消息
DispatchMessage(&msg);//派发给 WinProc 处理消息
}
}
7、消息处理,在WinProc 中switch-case 处理不同消息
代码示例:
新建-工程-选择“win32 application”-输入 工程名-选择“一个简单的win32程序” -点击“完成”
修改主要CPP文件:
// win32app.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <stdio.h> HINSTANCE g_hinstance=0;//全局句柄 HANDLE g_houtput=0; void OnCreate(HWND hwnd,LPARAM lparam) { // CREATESTRUCT *pcs=(CREATESTRUCT*)lparam; // char * txt=(char *)pcs->lpCreateParams; // MessageBox(NULL,txt,"info",MB_OK); // char txt[200]={0}; // ScreenToClient(hwnd,&pt); // sprintf(txt,"%d,%d ; %d,%d,%d,%d\n",pt.x,pt.y,rc.top,rc.left,rc.right,rc.bottom); // // WriteConsole(g_houtput,txt,strlen(txt),NULL,NULL); } void oncommand(HWND hwnd,WPARAM wparam,LPARAM lparam) { UINT id=LOWORD(wparam); int result=0; HWND dlg=0; switch(id) { case 0: break; } } //回调函数 LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) { switch(msg) { case WM_SYSCOMMAND: if(wparam==SC_CLOSE){ /* int ret=MessageBox(NULL,"是否退出","info",MB_YESNO); if(ret==IDYES){ //下面代码会自动关闭和销毁 //PostQuitMessage(0); } else return 0;//不执行下面代码 */ } break; //在创建窗口之后还未显示的时候 case WM_CREATE: OnCreate(hwnd,lparam); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_COMMAND: oncommand(hwnd,wparam,lparam); break; } return DefWindowProc(hwnd,msg,wparam,lparam); } //注册窗口类 BOOL Register(LPSTR lpClassName,WNDPROC wndproc) { WNDCLASSEX wce={0}; wce.cbSize=sizeof(wce); wce.cbClsExtra=200; wce.cbWndExtra=200; wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wce.hCursor=NULL; wce.hIcon=NULL; wce.hIconSm=NULL; wce.hInstance=g_hinstance; wce.lpfnWndProc=wndproc; wce.lpszClassName=lpClassName; wce.lpszMenuName=NULL; wce.style=CS_HREDRAW|CS_VREDRAW; ATOM atom= RegisterClassEx(&wce); if(atom==0){ MessageBox(NULL,"注册失败","info",MB_OK); return FALSE; } return TRUE; } //创建窗口 HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName) { HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hinstance,"hello create"); return hwnd; } //创建子窗口 HWND CreateChild(HWND phwnd,LPSTR lpClassName,LPSTR lpWndName) { if(Register(lpClassName,DefWindowProc)==0) { MessageBox(phwnd,"创建子窗口失败","info",MB_OK); return NULL; } //子窗口风格,都要 WS_CHILD|WS_VISIBLE HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, 200,200,phwnd,NULL,g_hinstance,NULL); return hwnd; } //显示窗口 void Display(HWND hwnd) { ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); } //处理消息 void MSGdeal() { MSG msg={0}; while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//派发给 WinProc 处理消息 } } void SetExtra(HWND hwnd) { SetClassLong(hwnd,0,301); SetWindowLong(hwnd,0,401); } void GetExtra(HWND hwnd) { long nc= GetClassLong(hwnd,0); long nw=GetWindowLong(hwnd,0); char txt[200]={0}; sprintf(txt,"%d,%d",nc,nw); MessageBox(NULL,txt,"info",MB_OK); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { AllocConsole();//调试程序方法 g_houtput=GetStdHandle(STD_OUTPUT_HANDLE); g_hinstance=hInstance; // TODO: Place code here. //SysReg(); //AppReg(); if(Register("main",WinProc)==0) { MessageBox(NULL,"注册失败","提示",MB_OK); return 0; } HWND hwnd= CreateMain("main","pro1"); //HWND hchild=CreateChild(hwnd,"child1","child1");//创建子窗口 //HWND hchild2=CreateChild(hwnd,"child2","child2"); Display(hwnd); //MoveWindow(hchild,300,200,200,200,TRUE); //MoveWindow(hchild2,500,200,200,200,TRUE); //SetExtra(hwnd); //GetExtra(hwnd); MSGdeal(); return 0; }
//编译运行,会出来一个窗口程序,但内容是空白的。