对话框是一种很特殊的窗口,体现在消息的处理上
//普通窗口处理消息:自定义函数调用缺省消息处理函数
WndProc(...){
...
DefWindowProc(...);
}
//对话框窗口处理消息:缺省函数调用自定义函数
缺省函数(...){
...
自定义函数(...);
....
}
INT CALLBACK DialogProc(//函数名自定义
HWND hwndDlg, //窗口句柄
UINT uMsg, //消息ID
WPARAM wParam, //消息参数
LPARAM lParam//消息参数
);
//返回TRUE,缺省处理函数不需要处理
//返回FaLSE,交给缺省处理函数处理
//不需要调用缺省对话框处理函数
int DialogBox(
HINSTANCE hInstance,//应用程序实例句柄
LPCTSTR lpTemplate,//对话框资源ID
HWND hWndParent, //对话框窗口
DLGPROC lpDialogFunc//自定义函数
);
//DialogBox是一个阻塞函数,只有当对话框关闭后,才会返回,继续执行后续代码
//返回值是通过EndDialog设置
BOOL EndDialog(
HWND hDlg,//关闭的对话框窗口句柄
int nResult//关闭的返回值
);
//关闭模式对话框,只能适应EndDialog,不能使用DestoryWindow等函数
//nResult是DialogBox函数退出时的返回值
WM_INITDIALOG
对话框创建之后显示之前,通过对话框窗口处理函数,可以完成自己的初始化相关的操作#include
#include "resource.h"
HINSTANCE g_hInstance = 0;
INT CALLBACK DlgProc(HWND hWndlg,UINT msgID,WPARAM wParam,LPARAM lParam){
switch(msgID){
case WM_INITDIALOG:
MessageBox(hWndlg, "WM_INITDIALOG", "Infor", MB_OK);
break;
case WM_CREATE://为了验证对话框没有这个消息
MessageBox(hWndlg, "WM_CREATE", "Infor", MB_OK);
break;
case WM_SYSCOMMAND:
if(wParam == SC_CLOSE){
//销毁对话框
EndDialog(hWndlg,100);//两个功能销毁知名的对话框、解除DialogBox的阻塞
//DestroyWindow(hWndlg);//只是销毁对话框,但是并没有解除解除DialogBox的阻塞,所以不能用
}
break;
}
return FALSE;//将消息交给真正的对话框窗口系统处理函数的后续代码帮我们处理
}
void OnCommand(HWND hWnd, WPARAM wParam){
switch(LOWORD(wParam)){
case ID_MODEL:
int nRet = DialogBox(g_hInstance, (char*)IDD_DIALOG1,hWnd,DlgProc);
if(nRet == 100)
{
MessageBox(hWnd, "successful", "Infor", MB_OK);
}
break;
}
}
//窗口处理函数(自定义,处理函数)
LRESULT CALLBACK WndProc(HWND hWnd,UINT msgID,WPARAM wParam,LPARAM lParam)
{
switch(msgID){
case WM_COMMAND:
OnCommand(hWnd, wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);//可以使GetMessage函数返回0
break;
}
return DefWindowProc(hWnd, msgID, wParam, lParam);
}
//入口函数
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns,LPSTR lpCmdLine,int nCmdShow)
{
g_hInstance = hIns;
//注册窗口类
WNDCLASS wc = {0};
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hInstance = hIns;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Main";
//挂载菜单资源
wc.lpszMenuName = (char*)IDR_MENU1;
wc.style = CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wc);//将以上所有赋值全部写入操作系统内核
//在内存创建窗口
HWND hWnd = CreateWindowEx(0,"Main","window",WS_OVERLAPPEDWINDOW,100,100,500,500,NULL,NULL,hIns,NULL);
//显示窗口
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//消息循环
MSG nMsg = {0};
while(GetMessage(&nMsg,NULL,0,0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);//将消息交给窗口处理函数来处理
}
return 0;
}
HWND CreateDialog(
HINSTANCE hInstance,//应用程序实例句柄
LPCTSTR lpTemplate, //模板资源ID
HWND hWndParent,//父窗口
DLGPROC lpDialogFunc//自定义函数
);
//非阻塞函数,创建成功返回窗口句柄,需要使用ShowWindow函数显示对话框
BOOL ShowWindow(
HWND hWnd,
int nCmdShow
);
DestroyWindow
销毁窗口哦,不能使用EndDialog
关闭对话框BOOL DestroyWindow(HWND hWnd);
示例代码:
#include
#include "resource.h"
HINSTANCE h_gInstance = 0;
INT CALLBACK DlgProc(HWND hWndlg,UINT msgID,WPARAM wParam,LPARAM lParam){
switch(msgID){
case WM_SYSCOMMAND:
if(wParam == SC_CLOSE)
DestroyWindow(hWndlg);
break;
}
return FALSE;
}
void OnCommand(HWND hWnd,WPARAM wParam){
switch(LOWORD(wParam)){
case ID_UNMODEL:
HWND hWndlg = CreateDialog(h_gInstance, (char*)IDD_DIALOG1, hWnd, DlgProc);
ShowWindow(hWndlg, SW_SHOWNORMAL);
break;
}
}
//窗口处理函数(自定义,处理函数)
LRESULT CALLBACK WndProc(HWND hWnd,UINT msgID,WPARAM wParam,LPARAM lParam)
{
switch(msgID){
case WM_COMMAND:
OnCommand(hWnd, wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);//可以使GetMessage函数返回0
break;
}
return DefWindowProc(hWnd, msgID, wParam, lParam);
}
//入口函数
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns,LPSTR lpCmdLine,int nCmdShow)
{
h_gInstance = hIns;
//注册窗口类
WNDCLASS wc = {0};
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hInstance = hIns;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Main";
wc.lpszMenuName = (char*)IDR_MENU1;
wc.style = CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wc);//将以上所有赋值全部写入操作系统内核
//在内存创建窗口
HWND hWnd = CreateWindowEx(0,"Main","window",WS_OVERLAPPEDWINDOW,100,100,500,500,NULL,NULL,hIns,NULL);
//显示窗口
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//消息循环
MSG nMsg = {0};
while(GetMessage(&nMsg,NULL,0,0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);//将消息交给窗口处理函数来处理
}
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/002a6bcdc7964dcaaf19f2d7ac1ed96a.png#pic_center
//通知链接器到哪抓源码
#pragma comment(lib, "../Debug/Clib.lib")
int main(void){
int sum, sub;
sum = Clib_add(5,3);
sub = Clib_sub(5,3);
printf("sum=%d, sub=%d\n", sum, sub);
getchar();
return 0;
}
#include
using namespace std;
//添加函数声明
int CPPlib_add(int add1, int add2);
int CPPlib_sub(int sub1, int sub2);
//给连接器看的
#pragma comment(lib, "../Debug/CPPlib.lib")
int main(){
int sum = CPPlib_add(5, 4);
int sub = CPPlib_sub(5, 4);
cout << "sum=" << sum << ".sub=" << sub << endl;
getchar();
return 0;
}
#include
using namespace std;
//添加函数声明
int CPPlib_add(int add1, int add2);
int CPPlib_sub(int sub1, int sub2);
//给连接器看的
#pragma comment(lib, "../Debug/CPPlib.lib")
//使用C语言的Clib.lib库函数,extern "C"防止c++编译器进行换名操作
extern "C" int Clib_add(int add1, int add2);
extern "C" int Clib_sub(int sub1, int sub2);
#pragma comment(lib, "../Debug/Clib.lib")
int main(){
int sum = CPPlib_add(5, 4);//?CPPlib_add@@YAHHH@Z
int sub = CPPlib_sub(5, 4);//?CPPlib_sub@@YAHHH@Z
cout << "sum=" << sum << ".sub=" << sub << endl;
sum = Clib_add(5,3);//Clib_add
sub = Clib_sub(5,3);//Clib_sub
cout << "sum=" << sum << ".sub=" << sub << endl;
getchar();
return 0;
}
//_declspec(dllexport)加上这个是为了导出动态库函数
_declspec(dllexport) int CPPdll_add(int add1, int add2){
return add1 + add2;
}
_declspec(dllexport) int CPPdll_sub(int sub1, int sub2){
return sub1 + sub2;
}
_declspec(dllexport) int CPPdll_mul(int mul1, int mul2){
return mul1 + mul2;
}
_declspec(dllimport)
LIB
文件,#pragma comment(lib, "../Debug/CPPdll.lib")
#include
using namespace std;
//声明函数
_declspec(dllimport) int CPPdll_add(int add1, int add2);
_declspec(dllimport) int CPPdll_sub(int sub1, int sub2);
_declspec(dllimport) int CPPdll_mul(int mul1, int mul2);
//通知链接器到哪抓函数的 编号和dll文件名("CPPdll.dll")
#pragma comment(lib, "../Debug/CPPdll.lib")
int main(){
int sum = CPPdll_add(5,4);
int sub = CPPdll_sub(5,4);
int mul = CPPdll_mul(5,4);
cout << "sum=" << sum << ",sub=" << sub << ",mul=" << mul << endl;
system("pause");
return 0;
}
typedef
HMODULE LoadLibrary(
LPCTSTR lpFileName//动态库文件名或全路径
);//返回DLL的实例句柄(HINSTANCE)
真实
地址FARPROC GetProcAddress(
HMODULE hModule,//DLL句柄
LPCWSTR lpProcName//函数名称
); //成功返回函数地址
BOOL FreeLibrary(
HMODULE hLibModule//DLL的实例句柄
);
#include
#include
using namespace std;
typedef int(*ADD)(int m, int n);
typedef int(*SUB)(int m, int n);
typedef int(*MUL)(int m, int n);
int main(){
HINSTANCE hDll = LoadLibrary("CPPdll.dll");
cout << "hDll:" << hDll << endl;
//ADD myAdd = (ADD)GetProcAddress(hDll, "CPPdll_add");
ADD myAdd = (ADD)GetProcAddress(hDll, "?CPPdll_add@@YAHHH@Z");
cout << "myAdd:" << myAdd << endl;
int sum = myAdd(5,4);
cout << "sum=" << sum << endl;
//SUB mySub = (SUB)GetProcAddress(hDll, "CPPdll_sub");
SUB mySub = (SUB)GetProcAddress(hDll, "?CPPdll_sub@@YAHHH@Z");
cout << "mySub:" << mySub << endl;
int sub = mySub(5,4);
cout << "sub=" << sub << endl;
//MUL myMul = (MUL)GetProcAddress(hDll, "CPPdll_mul");
MUL myMul = (MUL)GetProcAddress(hDll, "?CPPdll_mul@@YAHHH@Z");
cout << "myMul:" << myMul << endl;
int mul = myMul(5,4);
cout << "mul=" << mul << endl;
FreeLibrary(hDll);
system("pause");
return 0;
}
为了使用不换名的函数名,重名生成导出动态库文件
#include
#include
using namespace std;
typedef int(*ADD)(int m, int n);
typedef int(*SUB)(int m, int n);
typedef int(*MUL)(int m, int n);
int main(){
HINSTANCE hDll = LoadLibrary("CPPdll.dll");
cout << "hDll:" << hDll << endl;
ADD myAdd = (ADD)GetProcAddress(hDll, "CPPdll_add");
//ADD myAdd = (ADD)GetProcAddress(hDll, "?CPPdll_add@@YAHHH@Z");
cout << "myAdd:" << myAdd << endl;
int sum = myAdd(5,4);
cout << "sum=" << sum << endl;
SUB mySub = (SUB)GetProcAddress(hDll, "CPPdll_sub");
//SUB mySub = (SUB)GetProcAddress(hDll, "?CPPdll_sub@@YAHHH@Z");
cout << "mySub:" << mySub << endl;
int sub = mySub(5,4);
cout << "sub=" << sub << endl;
//MUL myMul = (MUL)GetProcAddress(hDll, "CPPdll_mul");
MUL myMul = (MUL)GetProcAddress(hDll, "?CPPdll_mul@@YAHHH@Z");
cout << "myMul:" << myMul << endl;
int mul = myMul(5,4);
cout << "mul=" << mul << endl;
FreeLibrary(hDll);
system("pause");
return 0;
}
_declspec(dllexport)
定义,例如:class _declspec(dllexport) CMath{
//...
};
#ifdef DLLCLASS_EXPORTS
#define EXT_CLASS_declspec(dllexport)//DLL
#else
#define EXT_CLASS_declspec(dllimport)//使用者
#endif
class EXT_CLASS CMath{
//...
};
注意:封装类动态库一般只要声明导出,而不用定义文件导出,因为导出类真正是导出类中封装的函数相对地址
#ifndef _DLLCLASS_H
#define _DLLCLASS_H
#ifdef DLLCLASS_EXPORTS
#define EXT_CLASS _declspec(dllexport)
#else
#define EXT_CLASS _declspec(dllimport)
#endif
class EXT_CLASS CMath{
public:
int Add(int add1, int add2);
int Sub(int sub1, int sub2);
};
#endif //_DLLCLASS_H
#define DLLCLASS_EXPORTS
#include "ClassDll.h"
int CMath::Add(int add1, int add2){
return add1 + add2;
}
int CMath::Sub(int sub1, int sub2){
return sub1 - sub2;
}
#include
#include "../ClassDll/ClassDll.h"
#pragma comment(lib, "../Debug/ClassDll.lib")
using namespace std;
int main(){
CMath math;
int sum = math.Add(5, 6);
int sub = math.Sub(5, 6);
cout << "sum=" << sum << ",sub=" << sub << endl;
return 0;
}