Windows通信机制和DLL机制

DLL

<1>event.h

#include<windows.h>
#include<math.h>
#ifndef LIB_H
#define LIB_H
extern "C" void  _declspec(dllexport)setCalculate(HWND  hwnd,HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4);
extern "C" void  _declspec(dllexport)setClearAll(HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4);
#endif


 

<2>event.cpp

#include"event.h"
char Money[] = "";
char Rate[] = "";
char Year[] = "";
char Total[] = "";
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *主函数:Check                                              + 
+         *功能:  输入判断函数                                       +
+         *参数:  a[]             输入的字符串                       +
+         *返回值:全是数字返回TRUE 否则返回FALSE                     +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
BOOL Check(char a[])                                                            
{
	int i;
	int len = strlen(a);
	for(i=0;i<len;i++)
	{
		if(a[i]>'9'||a[i]<'0')
		{
			return FALSE;
		}
	}
	return TRUE;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *主函数:setCalculate                                       + 
+         *功能:  为CalculateButton添加响应                          +
+                  输入本金,利率,年 输出总钱数                      +
+         *参数:   hwnd             父窗口句柄                       +
+                   hwnd1            本金输入框句柄                   +
+					hwnd2            利率输入框句柄                   +
+					hwnd3            年数输入框句柄                   +  
+					hwnd4            输出框句柄                       +
+         *返回值:                                                   +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void setCalculate(HWND hwnd,HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4)
{
	GetWindowText(hwnd1,Money,20);
    GetWindowText(hwnd2,Rate,20);
	GetWindowText(hwnd3,Year,20);
    double totalMoney;
	if(Check(Money)&&Check(Rate)&&Check(Year))
	{
		totalMoney = atof(Money)*pow(1/atof(Rate)+1,atoi(Year));
		gcvt(totalMoney,10,Total);
		SetWindowText(hwnd4,Total);
	}
	else
	{
		MessageBox(hwnd,TEXT("对不起,你输入的格式不对"),TEXT("提示"),MB_OK);
		SetWindowText(hwnd1,"");
		SetWindowText(hwnd2,"");
		SetWindowText(hwnd3,"");
		SetWindowText(hwnd4,"");
	}
	
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *主函数:setClearAll                                        + 
+         *功能:  为ClearButton添加响应                              +
+                   清空所有输入框输出框                              +
+         *参数:   hwnd1            本金输入框句柄                   +
+					hwnd2            利率输入框句柄                   +
+					hwnd3            年数输入框句柄                   +  
+					hwnd4            输出框句柄                       +
+         *返回值:                                                   +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void setClearAll(HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4)
{
	SetWindowText(hwnd1,"");
	SetWindowText(hwnd2,"");
	SetWindowText(hwnd3,"");
	SetWindowText(hwnd4,"");
}


 

window

<1>window.h

//windows程序的头文件
#include<windows.h>
extern char szTitle[];                                           //标题条字符串
extern char szWindowClass[];                                     //窗口类名
extern  char szMenuName[] ; 
extern  int ID,ID_1;

//函数原型声明
ATOM    InitApplication(HINSTANCE);                              //用来注册窗口类
BOOL     InitInstance(HINSTANCE ,  int);                          //用来创建主窗口
LRESULT CALLBACK WndProc( HWND,  UINT,  WPARAM,  LPARAM);       //消息处理函数
int   APIENTRY WinMain(  HINSTANCE ,  HINSTANCE ,  LPSTR ,  int);  //主函数
BOOL  CreateControl(HINSTANCE,HWND, LPSTR);                       //创建文本框                                            


 

<2>window.cpp

#include "window.h"
#include<math.h>
//全局变量
char szTitle[] = "Windows通信机制和DLL机制";                           //标题条字符串
char szWindowClass[] = "w32";                                                  //窗口类名
char szMenuName[] = "NULL";                                                  //菜单名


typedef void (*lpsetCalculateFun)(HWND ,HWND ,HWND ,HWND ,HWND);
typedef void (*lpsetClearAllFun)(HWND ,HWND ,HWND ,HWND);


HWND hwnd1,hwnd2,hwnd3,hwnd4,calculateButton,clearButton;
HWND hwnd;    

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                               +
+         *主函数:WinMain                                                      + 
+         *功能:   初始化及注册窗口类,创建窗口,进入消息循环及终止应用程序    +
+         *参数:   hInstance             当前实例                              +
+                       hPrevInstance       前一个实例                          + 
+                       lpCmdLine          命令行                               + 
+                       nCmdShow           选择显示窗口或图标                   +  
+         *返回值:msg.wParam                                                   +
+                                                                               +
++++++++++++++++++++++++++++++++++++++++++++++++*+++++++++++++++++++++++++++++++*/


int APIENTRY WinMain(HINSTANCE     hInstance,    HINSTANCE  hPrevInstance ,    LPSTR    lpCmdLine ,    int   nCmdShow )
{
	MSG msg;                                                                         //声明消息结构对象
	InitApplication( hInstance);                                                  //注册窗口

	if(   !InitInstance(  hInstance,  nCmdShow  )   )                   //初始化
	{
		return FALSE;
	}


	while (  GetMessage(&msg, (HWND) NULL, 0, 0)  )             //消息循环
	{
		TranslateMessage(&msg);                                              //检索消息                                              
		DispatchMessage(&msg);                                              //将消息间接传给函数指针
	}
	return  msg.wParam;                                                          //结束消息循环传回PostQuitMesage 
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                          +
+         *主函数:InitApplication                                         + 
+         *功能:   用来注册窗口类                                         +
+         *参数:   hInstance             当前实例                         +
+         *返回值:如果成功,返回一个唯一的整数,如果失败,返回0           +
+                                                                          +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

ATOM InitApplication(HINSTANCE     hInstance)
{
	WNDCLASS wc;                                                                                //声明窗口结构对象
	//初始化对象的域值
	wc.style                      =              CS_HREDRAW | CS_VREDRAW;                       // 如果大小改变了重绘窗口 
	wc.lpfnWndProc                =             WndProc;                                        //窗口消息处理函数
	wc.cbClsExtra                 =              0;                                             // 无附加窗口类内存 
	wc.cbWndExtra                 =              0;                                             // 无附加窗口内存
	wc.hInstance                  =              hInstance;                                     //当前实例
	wc.hIcon                      =              LoadIcon(NULL,  IDI_APPLICATION);              // 图标
	wc.hCursor                    =              LoadCursor(NULL, IDC_ARROW);                   // 鼠标指针
	wc.hbrBackground              =              (HBRUSH)GetStockObject(WHITE_BRUSH);           // 背景画刷
	wc.lpszClassName              =               szWindowClass;                                // 窗口类名
	wc.lpszMenuName               =               szMenuName;                                   //菜单名

	return  RegisterClass(&wc);                                                                 //注册
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                      +
+         *主函数:InitInstance                                        + 
+         *功能:   用来创建主窗口                                     +
+         *参数:   hInstance             当前实例                     +
+                       nCmdShow           选择显示窗口或图标          +
+         *返回值:如果成功,返回一个非零值,如果失败,返回0           +
+                                                                      +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


BOOL InitInstance(HINSTANCE     hInstance,      int   nCmdShow)
{                                                       //声明窗口句柄
	hwnd   =  CreateWindow(
			 szWindowClass,                                              // 窗口类名,使用之前注册的主窗口类
		     szTitle,                                                    // 窗口名,显示在窗口标题栏上的字符串
		     WS_OVERLAPPEDWINDOW |WS_VSCROLL|WS_HSCROLL,                 //显示方式    具有水平和垂直滚动
		     CW_USEDEFAULT,                                              // x起点坐标
		     CW_USEDEFAULT,                                              // y起点坐标
		    400,                                                         // 宽
		     300,                                                        // 高
		    NULL,                                                        // 无父窗口
		    NULL,                                                        // 菜单句柄
		    hInstance,                                                   //实例句柄
		    NULL                                                         // 无窗口创建数据
		);
	if(!hwnd)                                                            // 判断是否创建成功
	{
		return FALSE;
	}
    CreateControl( hInstance,  hwnd, NULL);
	ShowWindow(hwnd,  nCmdShow);                                          //创建成功便显示窗口
	UpdateWindow(hwnd);                                                    //送出WM_PAINT消息
	return  TRUE;
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                  +
+         *主函数:CreateControl                                   + 
+         *功能:   创建文本框                                     +
+         *参数:   hwndp            父窗口句柄                    +
+                   szTitle          窗口标题                      +
+         *返回值:                                                +
+                                                                  +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
BOOL  CreateControl(HINSTANCE     hInstance,    HWND hwndp,    LPSTR szTitle)
{

	hwnd1   =  CreateWindow( "edit",NULL,  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 50, 40,100, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

	calculateButton   =  CreateWindow( "BUTTON","计算",  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 180, 40,60, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

	clearButton   =  CreateWindow( "BUTTON","清除",  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 180, 70,60, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

   hwnd2   =  CreateWindow( "edit",NULL,  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 50, 70,100, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

 

    hwnd3   =  CreateWindow( "edit",NULL,  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 50, 100,100, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);
    
	hwnd4   =  CreateWindow( "EDIT",NULL,  WS_CHILD |WS_VISIBLE|WS_VSCROLL|WS_BORDER|ES_LEFT|ES_MULTILINE|ES_AUTOVSCROLL,                       
			 50, 130,300, 100,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

	ShowWindow(hwnd1,  SW_SHOW);   
	ShowWindow(calculateButton,  SW_SHOW); 
	ShowWindow(clearButton,  SW_SHOW); 
	ShowWindow(hwnd2,  SW_SHOW);  
	ShowWindow(hwnd3,  SW_SHOW);  
	ShowWindow(hwnd4,  SW_SHOW);  
	return  TRUE;
}



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *主函数:WndProc                                            + 
+         *功能:   消息处理函数                                      +
+         *参数:  hwnd              实例句柄                         +
+                  message          消息                              +
+                  wParam           MSG消息结构中的域                 +
+                  lParam           MSG消息结构中的域                 +
+         *返回值:0                                                  +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


LRESULT  CALLBACK WndProc(HWND  hwnd , UINT   message, WPARAM   wParam, LPARAM    lParam)
{
	PAINTSTRUCT    ps;
	HDC  hdc;
	

	switch(message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		TextOut(hdc, 150 , 10, "利息计算器", strlen("利息计算器"));
		TextOut(hdc, 10 , 40, "本金:", strlen("本金:"));
		TextOut(hdc, 10 , 70, "利率:", strlen("利率:"));
		TextOut(hdc, 10 , 100, "年:", strlen("年:"));
		EndPaint(hwnd,&ps);
		break;
	case WM_COMMAND:
		HINSTANCE hDll;
	    lpsetCalculateFun setCalculateFun;
		lpsetClearAllFun setClearAllFun;
	    hDll = LoadLibrary("..\\DLL\\Debug\\DLL.dll");                                 //获得DLL模块句柄
	    if(hDll!=NULL)
		{
			setCalculateFun =  (lpsetCalculateFun)GetProcAddress(hDll,"setCalculate");//获取函数的导出地址
			setClearAllFun =  (lpsetClearAllFun)GetProcAddress(hDll,"setClearAll");   //获取函数的导出地址
			if(setCalculateFun!=NULL)
			{
				if(LOWORD(lParam)==LOWORD(calculateButton))
				{
					setCalculateFun(hwnd,hwnd1,hwnd2,hwnd3,hwnd4);
				}
			}
			if(setClearAllFun!=NULL)
			{
				if(LOWORD(lParam)==LOWORD(clearButton))
				{
					setClearAllFun(hwnd1,hwnd2,hwnd3,hwnd4);
				}
			}
			FreeLibrary(hDll);
		}
		else
		{
			MessageBox(hwnd,TEXT("hDll创建不成功"),TEXT("提示"),MB_OK);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}


 

你可能感兴趣的:(windows,null,dll,callback,button,border)