MFC应用程序编写实例—完整版(原创)

简介: 前段时间,将近花了一周至两周上班和上班后的闲余时间,做了一个用于调试和测试工作项目的应用软件,下面将实现软件的重要步骤及主要功能讲解一遍,方便日后查阅。 程序开始后,提示登录框,输入用户名,密码后,登录进去主窗体,效果图如下:   下面将主要实现的功能函数要点进行描述,具体实现如下:  ...

前段时间,将近花了一周至两周上班和上班后的闲余时间,做了一个用于调试和测试工作项目的应用软件,下面将实现软件的重要步骤及主要功能讲解一遍,方便日后查阅。

程序开始后,提示登录框,输入用户名,密码后,登录进去主窗体,效果图如下:

 

下面将主要实现的功能函数要点进行描述,具体实现如下:

 

一、设置主窗体大小

1、进入工程窗体初始化函数,OnInitDialog()中,在CDialog::OnInitDialog() 下面添加函数语句如下:

 SetWindowPos(NULL,0,0,600,400,SWP_NOMOVE);  ////设置主窗体大小,长为600,高为400

 

二、为主窗体添加背景图片:

1、首先,在工程头文件中,声明画刷变量如:

 CBrush m_brBk;

 

2、在工程OnInitDialog()中,添加如下代码:

 CBitmap bmp1;

   bmp1.LoadBitmap(IDB_BITMAP1);
 
  m_brBk.CreatePatternBrush(&bmp1);

 

3、添加消息函数OnCtlColor,代码如下所示:

HBRUSH CDebugDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 
//  if (pWnd == this)
//
//  {
//
//   return m_brBk;
//
//  }


 return(HBRUSH) m_brBk;
}

 

三、调用模态对话框和非模态对话框 (以菜单入口为例)

如菜单所属的主窗体类名为CDebugDlg,调用对话框的类名为CPing

 

实现调用非模态对话框方法:

选择调用菜单入口后,点击添加消息处理函数,在函数体内添中如下代码:

CPing *dlg = new CPing(); 
 dlg->Create(IDD_DIALOG_PING); //创建一个非模态对话框  
 dlg->ShowWindow(SW_SHOW); //显示非模态对话框   

实现调用模态对话框方法:

Cping dlg;

dlg.DoModal();

 

四、调用外部应用程序方法:

1、调用外部应用程序可采用WinExec函数

例如,调用一个Tcpview.exe外部程序,可在消息处理函数中添加如下代码:

WinExec(".\\dll\\TCPview\\Tcpview.exe",SW_SHOW);   // 其中.代表当前路径,此时需要用到\\来区分路径。

 

2、调有外部浏览器应用程序,可采用ShellExecuteA

HINSTANCE test = ShellExecuteA(NULL, "open", "http://192.168.18.201/TDBIN/start_a.htm", NULL, NULL, SW_SHOW);

 

 

五、工具栏的实现方法:

1、在添加代码前,在头文件中需声明变量,如:

 CToolBar m_ToolBar;   //工具栏变量
 CImageList m_ImageList;   //  图像列表变量

2、进入主窗体中的OnInitDialog()函数,在函数中(在return true前面任一位置添加即可,且需要在资源视图中添加对应的图标资源),添加如下代码:

 //创建图像列表
  m_ImageList.Create(32,32,ILC_COLOR24|ILC_MASK,1,1);
  //向图像列表中添加图标
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON5));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON6));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON7));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON8));
  m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON9));
  UINT Tool_array[17];
  for(int i=0;i<17;i++)
  {

   if(i==1 || i==3 || i==5 || i==7 || i==9 || i==11 || i==13|| i==15 )
    Tool_array[i] = ID_SEPARATOR; //第2、4、6、8、10、12、14、16个按钮为分隔条
   
   else
    //Tool_array[i] =ID_BUTTON1+i;
    
    Tool_array[0] =ID_32778; //美电
    Tool_array[2] =ID_32785;  //天视通
    Tool_array[4] =ID_32781;  //十五所
    Tool_array[6] =ID_32786;   //Ghost
    Tool_array[8] =ID_32800;   //TFTP
    Tool_array[10] =ID_32772;  //Debug
    Tool_array[12] =ID_Menu32803;  //MAC序列号
    Tool_array[14] =ID_32798;  //IST_升级
    Tool_array[16] =IDC_BUTTON_SHUTDOWN; //系统关机   
  }

  m_ToolBar.Create(this);
  m_ToolBar.SetButtons(Tool_array,17);  //设置工具栏按钮索引
  m_ToolBar.SetButtonText(0,"美电");
  m_ToolBar.SetButtonText(2,"天视通");
  m_ToolBar.SetButtonText(4,"十五所");
  m_ToolBar.SetButtonText(6,"Ghost");
  m_ToolBar.SetButtonText(8,"TFTP");
  m_ToolBar.SetButtonText(10,"Debug");
  m_ToolBar.SetButtonText(12,"MAC序列号");
  m_ToolBar.SetButtonText(14,"IST_升级");
  m_ToolBar.SetButtonText(16,"系统关机");
  //关联图像列表
  m_ToolBar.GetToolBarCtrl().SetImageList(&m_ImageList);
  m_ToolBar.SetSizes(CSize(57,60),CSize(32,32)); //设置按钮和图标的大小
  m_ToolBar.EnableToolTips(TRUE); //激活工具栏提示功能
  RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);  //显示工具栏
  
  //m_ToolBar.ModifyStyle(0,TBSTYLE_TRANSPARENT);//设置工具栏背景色透明    ---  (根据要求,可以自形选择)

 

3、在窗体源文件的消息映射BEGIN_MESSAGE_MAP中添加如下代码:

 ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify)

 

4、在窗体头文件中声明OnToolTipNotify函数,如下所示:

afx_msg BOOL OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult );

 

5、OnToolTipNotify函数的实现代码如下所示:

BOOL CDebugDlg::OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
 TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
 UINT nID =pNMHDR->idFrom; //获取工具栏按钮ID
 if(nID)
 {
  UINT nIndex = m_ToolBar.CommandToIndex(nID); //根据ID获取按钮索引
  if(nIndex != -1)
  {
   m_ToolBar.GetButtonText(nIndex,m_TipText);     //获取工具栏文本
   pTTT->lpszText = m_TipText.GetBuffer(m_TipText.GetLength()); //设置提示信息文本
   pTTT->hinst = AfxGetResourceHandle();
   return TRUE;
  }
 }
 return FALSE;
}

 

六、对话框标题栏图标添加或者修改方法:

1、在对话框的标准的构造函数中,添加如下代码: m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

修改IDR_MAINFRAME对应的图标资源即可。

详细如下:

CDebugDlg::CDebugDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CDebugDlg::IDD, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

 

注: 在修改对话框标题栏图标时,一定要注意,对话框要在OnInitDialog()函数,添加如下代码:

 SetIcon(m_hIcon, TRUE);   // 设置大图标
  SetIcon(m_hIcon, FALSE);  // 设置小图标

 

 

七、状态栏实现方法:

1、在对话框头文件中声明变量,如下所示:

CStatusBar  m_StatusBar;

 

2、在OnInitDialog()函数添加如下代码:

 UINT array[4];
  for(int i=0;i<4;i++) 
  {   array[i] = 1001 + i;  } 
  m_StatusBar.Create(this); //创建状态栏窗口
  m_StatusBar.SetIndicators(array,sizeof(array)/sizeof(UINT)); //添加面板 

//   for(int n=0;n<5;n++)
//   {  
//    m_StatusBar.SetPaneInfo(n,array[n],0,100); //设置面板宽度 
//   } 

  m_StatusBar.SetPaneInfo(0,array[0],0,130);
  m_StatusBar.SetPaneInfo(1,array[1],1,230);
  m_StatusBar.SetPaneInfo(2,array[2],2,80);
  m_StatusBar.SetPaneInfo(3,array[3],3,130);
//   m_StatusBar.SetPaneInfo(3,array[3],3,150);
//   m_StatusBar.SetPaneInfo(4,array[4],4,100);

  CTime time = CTime::GetCurrentTime();

  m_StatusBar.SetPaneText(0,_T("版权所有者:周金剑")); //设置面板文本
 
//   m_StatusBar.SetPaneText(1,_T("系统时间:")
//    + time.Format("%Y") + _T('年')
//    + time.Format("%m") + _T('月')
//    + time.Format("%d") + _T('日')
//    + time.Format("%H") + _T('时')
//    + time.Format("%M") + _T('分')
//    + time.Format('%S') + _T('秒'));

// m_StatusBar.SetPaneText(1,_T("系统时间:") + time.Format("%Y")+_T('年')+ time.Format("%m")+_T('月')+ time.Format("%d")+_T('日')+_T('  ')+ time.Format("%H:%M:%S")); 
  m_StatusBar.SetPaneText(1,("系统时间:")+ time.Format("%Y")+ ("年")+ time.Format("%m") + ("月") + time.Format("%d") + ("日") + (" ") + time.Format("%H:%M:%S"));

 // m_StatusBar.SetPaneText(1,time.Format("%Y-%m-%d %H:%M:%S"));


  m_StatusBar.SetPaneText(3,"当前用户:"+ m_user);

  RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

 // m_StatusBar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));   //状态栏背景色

 

上述注释代码可忽略不看,上述代码将状态栏分为四栏,第一栏,显示普通的字符串信息, 第二栏显示当前系统时间,第三栏显示 鼠标移动的坐标位置,第四栏显示登录的当前用户名。

第一栏,实现普通的字符串信息, 不作描述。

第二栏,实现当前系统时间:

在OnInitDialog()函数中,上述代码后面,添加一计时器,如: SetTimer(1,1000,NULL);

 

添加OnTimer消息函数,实现代码如下:

void CDebugDlg::OnTimer(UINT_PTR nIDEvent)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 if(nIDEvent==1)
 {
  CTime time;
  time=CTime::GetCurrentTime();

//   m_StatusBar.SetPaneText(1,_T("系统时间:")
//       + time.Format("%Y") + _T('年')
//    + time.Format("%m") + _T('月')
//    + time.Format("%d") + _T('日')
//    + time.Format("%H") + _T('时')
//    + time.Format("%M") + _T('分')
//    + time.Format("%S") + _T('秒'));

//  m_StatusBar.SetPaneText(1,_T("系统时间:") + time.Format("%Y")+_T('年')+ time.Format("%m")+_T('月')+ time.Format("%d")+_T('日')+_T('  ')+ time.Format("%H:%M:%S"));
  m_StatusBar.SetPaneText(1, ("系统时间:") + time.Format("%Y")+ ("年")+ time.Format("%m") + ("月") + time.Format("%d") + ("日") + (" ") + time.Format("%H:%M:%S"));
 }

 CDialog::OnTimer(nIDEvent);
}

 

第三栏、实现获取鼠标移动的X,Y坐标位置:

添加OnMouseMove函数,实现代码如下:

void CDebugDlg::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值

 CString s;
 s.Format(_T("X=%d Y=%d"),point.x,point.y);
 m_StatusBar.SetPaneText(2,s);

 CDialog::OnMouseMove(nFlags, point);
}

 

第四栏,获取当前系统登录用户名。

这个涉及到不同对话框之间变量值的赋值方法:

假如将登录对话框中,用户名编辑框中的值传主窗体中一个变量m_user,登录对话框编辑框对应的ID为:IDC_EDIT_USERNAME,变量名: m_username

在登录对话框源文件中,相应的地方添加代码如下:(在登录对话框的头文件中关联一对象:CDebugDlg dlg_confirm;)

UpdateData(TRUE);

  GetDlgItemText(IDC_EDIT_USERNAME,m_username);

 dlg_confirm.m_user=m_username;

 

此时只需要在主窗体的初始化函数中,及上述OnInitDialog()函数添加如下代码即可:

 m_StatusBar.SetPaneText(3,"当前用户:"+ m_user);

 

 

八、登录框设计实现方法:

1、创建一个对话框窗体,将窗体设置为默认启动第一窗口:

可在CWinApp::InitInstance();中修改。

2、设计一对话框,如下所示:

3、为对话框编辑框关联变量,如下所示:

CString m_username;
 CString m_password;

4、在头文件中关联记录集变量,如下所示:

_ConnectionPtr m_pConnection;
  _RecordsetPtr m_pRecordset;

 

5、在工程的stdafx.h头文件中,添加如下代码:

#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

 

6、登录按钮实现的主要代码:(其中加\\为调试中写入代码,用于调试用,可供参考)

void CConfirm::OnBnClickedButton1()
{
//  UpdateData(true);
//  if (m_username=="aebell"&&m_password=="aebell")
//  {
//   CPing dlg;
//   dlg.DoModal();
//  }
//  else
//  {
//   MessageBox("登录失败!");
//  }
//
 try
 {
  this->UpdateData(true);

  //  if (this->m_username.IsEmpty()||this->m_password.IsEmpty())
  //  {
  //   MessageBox("用户名,密码不能为空!","登录提示",MB_ICONQUESTION);
  //  }

  ::CoInitialize(NULL);

  this->m_pConnection.CreateInstance(__uuidof(Connection));

  this->m_pRecordset.CreateInstance(__uuidof(Recordset));


  // this->m_pConnection->Open("DSN=staff_dns","","",0);//上面四行为打开数据源连接,此方法使用本地DSN数据源
  // CString strJet ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=staff.mdb";  //访问不带密码的access数据库
  CString strJet ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=staff.mdb;Jet OLEDB:Database Password=sdoa0806";   //访问带密码的access数据库方法

  this->m_pConnection->Open(strJet.AllocSysString(),"","",adModeUnknown);

  CString str;

  str.Format("select * from tb_staff where username='%s' and password='%s'",this->m_username,this->m_password);

  BSTR bstrSQL=str.AllocSysString();

  this->m_pRecordset->Open(bstrSQL,(IDispatch*)this->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);

  if(!this->m_pRecordset->adoEOF)

  {


   //   CDebugDlg dlg1;
   //   UpdateData(false);
   //   GetDlgItemText(IDC_EDIT1,dlg1.m_user);
   //   MessageBox(dlg1.m_user);
   //   dlg1.m_StatusBar.SetPaneText(3,m_username);

   CDialog::OnOK();
   //MessageBox("调用成功!");
   OutputDebugString("登录成功!");

   UpdateData(TRUE);

   GetDlgItemText(IDC_EDIT_USERNAME,m_username);

   //MessageBox(m_username,"1");

   dlg_confirm.m_user=m_username;
   // MessageBox(dlg_confirm.m_user,"2");

   UpdateData(FALSE);

   dlg_confirm.DoModal();

  }

  else

   MessageBox("用户名,密码输入错误,登录失败!","登录提示",MB_ICONQUESTION);


 }
 catch (...)     //增加try...catch异常抛出
 {
  
   AfxMessageBox("数据库连接失败,确认数据库名称与路径是否正确!");  
   return ;
 } 


 this->m_pRecordset->Close();

 this->m_pConnection->Close();

 ::CoUninitialize();

}

 

九、ping 测试功能实现方法(主要实现单个IP与多个连续IP进行网络Ping 测试)

1、具体实现效果如下:

 

下面介绍功能实现的主要代码:

其中,“发送”按钮功能实现代码如下:

void CPing::OnBnClickedButton1()
{
 CString strText1=_T("");
 CString strText2=_T("ping");
 CString strText3=_T(" "); 
 CString strText4=_T("-t");
 CString strText5=_T("-n");
 CString strText6=_T("-l");
 CString strText7=_T("");
 CString strText8=_T("");

 CString strText9=_T(">>");
 CString strText10=_T("c:\\ping1.log");

 GetDlgItemText(IDC_EDIT1,strText1);  //得到编辑框文本,并保存至变量中
 GetDlgItemText(IDC_EDIT2,strText7);
 GetDlgItemText(IDC_EDIT3,strText8);
 //GetWindowText(strText1);  //得到窗口标题文本

 CTime time=CTime::GetCurrentTime();

 //MessageBox(strText1+strText7+strText8);
if (strText7==""||strText8=="")
{
 MessageBox("请先输入发送次数与字节数!","提示",MB_ICONQUESTION);
}

else

// if (strText7=="0")
// {
//  system(strText2+strText3+strText1+strText3+strText4);   //加-t无限次循环
//  
// }
//  
// else
//    
//  system(strText2+strText3+strText1+strText3+strText5+strText3+strText7);  //按指定次数循环

if (strText8!=""&& strText7=="0")
{

 system(strText2+strText3+strText1+strText3+strText6+strText3+strText8+strText3+strText4); //循环发送指定字节数
 
 OutputDebugString("开始生成Log起始时间:  "+time.Format("%Y-%m-%d %H:%M:%S  ")+strText2+strText3+strText1+strText3+strText6+strText3+strText8+strText3+strText4+strText3+strText9+strText10);   //输出Log到控制台


 system(strText2+strText3+strText1+strText3+strText6+strText3+strText8+strText3+strText4+strText3+strText9+strText10);  //生成log
 
}


else

 system(strText2+strText3+strText1+strText3+strText5+strText3+strText7+strText3+strText6+strText3+strText8); //按指定次数、字节数发送
 

 OutputDebugString("开始生成Log起始时间:  "+time.Format("%Y-%m-%d %H:%M:%S  ")+strText2+strText3+strText1+strText3+strText5+strText3+strText7+strText3+strText6+strText3+strText8+strText3+strText9+strText10);   //输出Log


 system(strText2+strText3+strText1+strText3+strText5+strText3+strText7+strText3+strText6+strText3+strText8+strText3+strText9+strText10);  //生成log

 
}

 

多个连续IP中,“确定”按钮的实现代码:

void CPing::OnBnClickedButton2()
{
 CString str1; //ip网段
 CString str2; //ip起始数
 CString str3; //ip结束数
 CString str4; //ip间隔数
 CString str5; //ip执行次数
 CString str6; //回复等待时间

 CString str7="for";
 CString str8="/l";
 CString str9="%i";
 CString str10="in";
 CString str11="do";
 CString str12="ping";
 CString str13="-n";
 CString str14="-w";
 CString st=" ";
 CString st1="(";
 CString st2=",";
 CString st3=")";
 CString st4=".";

 CString st5=">>";
 CString st6="c:\\ping2.log";
 
 
 GetDlgItemText(IDC_EDIT5,str1);
 GetDlgItemText(IDC_EDIT4,str2);
 GetDlgItemText(IDC_EDIT6,str3);
 GetDlgItemText(IDC_EDIT7,str4);
 GetDlgItemText(IDC_EDIT8,str5);
 GetDlgItemText(IDC_EDIT9,str6);

 //system("for /l %i in (1,1,255) do ping -n 3  -w 60 192.168.0.%i");
 
if (str1==""||str2==""||str3==""||str4==""||str5==""||str6=="")
{
 MessageBox("编辑框内值不能为空!","提示",MB_ICONQUESTION);
}
else
{
 system(str7+st+str8+st+str9+st+str10+st+st1+
  str2+st2+str4+st2+str3+st3+st+str11+st+
  str12+st+str1+st4+str9+st+str13+st+str5+st+str14+st+str6);  //执行命令

 OutputDebugString("输入的IP地址范围为:"+str7+st+str8+st+str9+st+str10+st+st1+
  str2+st2+str4+st2+str3+st3+st+str11+st+
  str12+st+str1+st4+str9+st+str13+st+str5+st+str14+st+str6);   //输出log信息

 system(str7+st+str8+st+str9+st+str10+st+st1+
  str2+st2+str4+st2+str3+st3+st+str11+st+
  str12+st+str1+st4+str9+st+str13+st+str5+st+str14+st+str6+st+st5+st6);  //生成Log
}

 

十、计算器实现的方法:

1、效果如图下所示:

该功能是通过封装到动态链接中,通过调用dll来实现的,具体实现过程,请参照下面动态链接库的实现方法。

 

十一、增加打印信息输出至调试工具

1、使用OutputDebugString函数来完成。

如:OutputDebugString("系统登录成功");

 

十二、关机、重启功能实现方法(且使用到messagebox功能)

1、具体实现代码,如下所示:

void CDebugDlg::OnBnClickedButtonShutdown()
{
 if (MessageBox(_T("是否现在退出电脑?"),_T("系统提示"),MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
 {
  system("shutdown -f -s -t 0");
 }
}

void CDebugDlg::OnBnClickedButtonReboot()
{
 if (MessageBox(_T("是否现在重启电脑?"),_T("系统提示"),MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
 {
  system("shutdown -f -r -t 0");
 }
}

 

十三、动态链接库dll使用方法:

 1、创建一动态链接库工程,下面介绍使用一计算器对话框功能。

2、在动态链接库工程头文件中,添加如下代码:

#include "stdafx.h"
#define EXPORT __declspec(dllexport)
extern "C"  EXPORT  void __stdcall  Showdialg(char* pText);
extern "C"  EXPORT  void __stdcall  Showdialg1();

 

3、在源文件中,实现上述两个函数的实现功能代码:

 void __stdcall  Showdialg(char* pTex)
 {
  MessageBox(NULL,pTex,"提示一",MB_OKCANCEL);
 }
 
 void __stdcall  Showdialg1()
 {
  AFX_MANAGE_STATE(AfxGetStaticModuleState());   //在调用对话框时,该语句一定要添加

 

  CTest1 dlg1;
  dlg1.DoModal();
 // OnCancel();

 
 }

 

 4、CTest1为新建计算机对话框所属的类名。

创建如上述所示的一个计算器对话框程序,在头文件中声明相应的变量,如下所示:

CEdit m_ret;
 

 CString num1;       //数值计算符号前面的数值
 CString num2;       //数值计算后面的数值
 BOOL isresult;        //是否按下加、减、乘、除符号
 int witch;                //是加、减、乘、除哪种计算

 CString clear;

CString m_result;

 

5、在源文件中,实现主要核心代码,如下所示:

void CTest1::OnBnClickedButton1()
{
//  CString st1;
//  CString ret;
//  UpdateData(true);
//  GetDlgItemText(IDC_BUTTON_1,st1);
//  //MessageBox(st1);
//  ret=st1;
//  //ret.Format("%s",st1);
//  SetDlgItemText(IDC_EDIT_RET,ret);
//  //m_ret.SetWindowText(ret);
//  //MessageBox(ret);

 
 


 if(isresult==FALSE)
 {
  
  num1+="1";
  m_result=num1;
 // MessageBox(m_result,NULL,0);
  UpdateData(false);
  
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="1";
 
  m_result=num2;

//   AfxMessageBox(m_result);
//   AfxMessageBox(num2);
  UpdateData(false);
 }


}

 

void CTest1::OnBnClickedButton2()
{
 if(isresult==FALSE)
 {

  num1+="2";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {

  m_result="";
  num2="";

  num2+="2";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton3()
{
 if(isresult==FALSE)
 {

  num1+="3";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="3";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton4()
{
 if(isresult==FALSE)
 {

  num1+="4";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="4";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton5()
{
 if(isresult==FALSE)
 {

  num1+="5";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="5";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton6()
{
 if(isresult==FALSE)
 {

  num1+="6";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="6";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton8()
{
 if(isresult==FALSE)
 {

  num1+="7";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="7";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton10()
{
 if(isresult==FALSE)
 {

  num1+="8";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="8";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton7()
{
 if(isresult==FALSE)
 {
  
  num1+="9";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="9";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton14()
{
 if(isresult==FALSE)
 {

  num1+="0";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)

 {
  m_result="";
  num2="";
  num2+="0";
  m_result=num2;
  UpdateData(false);
 }

}

void CTest1::OnBnClickedButton15()
{
 if(isresult==FALSE)
 {

  num1+=".";
  m_result=num1;
  UpdateData(false);
 }
 if(isresult==TRUE)
 {
  m_result="";
  num2="";
  num2+=".";
  m_result=num2;
  UpdateData(false);
 }


}

void CTest1::OnBnClickedButton9()
{
 isresult=TRUE;
 witch=1;

}

void CTest1::OnBnClickedButton11()
{
 isresult=TRUE;
 witch=2;

}

void CTest1::OnBnClickedButton12()
{
 isresult=TRUE;
 witch=3;

}

void CTest1::OnBnClickedButton13()
{
 isresult=TRUE;
 witch=4;

}

void CTest1::OnBnClickedButton16()
{
 double number1=atof(num1);
 double number2=atof(num2);
 //AfxMessageBox(num1);  //此时,该值为空?
 //AfxMessageBox(num2);
 double result=0.0;
 switch(witch)
 {
 case 1:result=number1+number2;break;
 case 2:result=number1-number2;break;
 case 3:result=number1*number2;break;
 case 4:result=number1/number2;break;
 default:AfxMessageBox("程序运行错误");break;
 }
 
 m_result.Format("%f",result);

 UpdateData(false);


}

void CTest1::OnBnClickedButton17()
{

 m_result="0";
  UpdateData(false);
 SetDlgItemText(IDC_EDIT_RET,m_result); 
  //num1="";    
 num2="";  //将num1值赋值为空
 m_result="0";
 
 
}

 

6、在使用上述值时,需在OnInitDialog()函数中,初始代相应变量中的值。

BOOL CTest1::OnInitDialog()
{
 CDialog::OnInitDialog();

 num1="";        //第一个数据
 num2="";        //第二个数据
 isresult=FALSE;     //保存是否点击了运算按钮
 witch=0;              //保存运算按钮

 m_result="";  //清空操作


 SetIcon(m_Icon, TRUE);   // 设置大图标
 SetIcon(m_Icon, FALSE);  // 设置小图标


 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常: OCX 属性页应返回 FALSE
}

 

 

7、代码实现完成之后,打开.def文件,声明输出函数,如下所示:

; numerator.def : 声明 DLL 的模块参数。

LIBRARY      "numerator"

EXPORTS
   Showdialg = Showdialg
   Showdialg1 = Showdialg1

 

8、编译生成.dll,.lib文件。

 

9、打开需要调用dll的工程,在工程的头文件中,声明动态链接库头文件中,如

#include "numerator.h"

并将头文件中添加到该工程中,且将文件复制到工程目录下。

 

10、在使用该DLL的源文件中,声明如下:

#pragma comment (lib,"numerator.lib")

 

11、在使用处,调用dll函数,如下所示:

void CDebugDlg::On_calc()
{
 Showdialg1();
}

 

 

十四、实现定时提醒功能方法:

1、效果如下所示:

2、该功能也是通过dll来实现的,但该处只介绍实现的主要代码,详细方法,不介说明。

void CShowTimer::OnBnClickedButton2()
{
 OnCancel();
}

void CShowTimer::OnBnClickedButton1()
{
 
 SetTimer(1,1000,NULL); 
}

void CShowTimer::OnBnClickedButton3()
{
 UpdateData(true);
}

void CShowTimer::OnTimer(UINT_PTR nIDEvent)
{
 if (nIDEvent==1)
 {
 
  UpdateData(true);
  CString hour;
  CString minute;
  CString second;

  CString get_time_Hour;
  CString get_time_Minute;
  CString get_time_Second;

  hour = m_time_edit2.Format("%H");
  
  minute = m_time_edit2.Format("%M");
  second = m_time_edit2.Format("%S");

  CTime get_time=CTime::GetCurrentTime();

  get_time_Hour=get_time.Format("%H");
  get_time_Minute=get_time.Format("%M");
  get_time_Second=get_time.Format("%S");

  if (get_time_Hour==hour&&get_time_Minute==minute&&get_time_Second==second)
  {
   GetDlgItemText(IDC_EDIT_1,m_value_edit);
   MessageBox(m_value_edit,"提示",64);
   
   sndPlaySound("E:\\Project Design\\Dll\\DLL\\Debug\\prompt.wav",SND_ASYNC);


   GetDlgItemText(IDC_EDIT_3,m_value_edit3);

   if (m_value_edit3.IsEmpty() || m_value_edit3=="0")
   {
    return;
   }
   else
   while (true)
   {
     int m_edit3=atoi(m_value_edit3);
     Sleep(m_edit3*1000*60);
     MessageBox(m_value_edit,"提示",64);
     return;
   }

  }
  else
  {
   return;
  }
 }
 
 CDialog::OnTimer(nIDEvent);
}

void CShowTimer::OnBnClickedButton4()
{
 UpdateData(true);
}

BOOL CShowTimer::OnInitDialog()
{
 CDialog::OnInitDialog();

 CString str="你已经进入定时区域,请准备!";
 UpdateData(false);
 SetDlgItemText(IDC_EDIT_1,str);

 m_control_edit2.SetTime(&CTime::GetCurrentTime());  //初始化时间控件显示为当前系统时间
 
 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常: OCX 属性页应返回 FALSE
}

 

 

十五、其它应用小技巧:

 

1、获取窗口标题方法:

void CTest1::OnBnClickedButton3()
{
 CString StrText=_T("");   //申请一个字符串变量,用于存储窗口标题,其中前面增加_T用于存储unicode字符(中文字符)
 GetWindowText(StrText);  //获取窗口标题,并保存到变量StrText中
 SetDlgItemText(IDC_EDIT1,StrText);  //将窗口标题,显示在编辑框内
}

 

2、设置窗口标题:

void CTest1::OnBnClickedButton4()
{
 CString StrText1; 
 GetDlgItemText(IDC_EDIT1,StrText1);  //获取编辑中文本的内容,并保存到变量中
 SetWindowText(StrText1);  //将变量的值赋给标题
}

 

3、隐藏标题栏:

void CTest1::OnBnClickedButton5()
{
 ModifyStyle(WS_CAPTION,0,SWP_FRAMECHANGED);  //隐藏窗口标题
}

 

4、显示标题栏:

void CTest1::OnBnClickedButton6()
{
 ModifyStyle(0,WS_CAPTION,SWP_FRAMECHANGED);  //显示窗口标题
}

 

5、改变窗口大小:

void CTest1::OnBnClickedButton2()
{
 SetWindowPos(NULL,100,100,500,500,SWP_ASYNCWINDOWPOS); //改变窗口位置与大小
}

 

6、最小化所有窗口:

void CTest1::OnBnClickedButton7()
{
 CWnd* pWnd = CWnd::FindWindow(_T("Shell_TrayWnd"), NULL); //获得任务栏窗口

 pWnd->SendMessage(WM_HOTKEY, 0x1F5);  //发送ID为0x1F5(Win + M)的WM_HOTKEY消息
}

 

7、隐藏任务栏:

void CTest1::OnBnClickedButton8()
{
 CWnd* pWnd = CWnd::FindWindow(_T("Shell_TrayWnd"), NULL);  //定义一个窗口指针对象,用来存储任务栏
 if (pWnd->IsWindowVisible())
 {
  pWnd->ShowWindow(SW_HIDE);  //隐藏任务栏
 }
}

 

8、显示任务栏:

void CTest1::OnBnClickedButton9()
{
 CWnd* pWnd = CWnd::FindWindow(_T("Shell_TrayWnd"), NULL);
 if (!pWnd->IsWindowVisible())
 {
  pWnd->ShowWindow(SW_SHOW);  //显示任务栏
 }
}

 

9、最大化,最小化,恢复窗口:

void CTest1::OnBnClickedButton1()
{
 SendMessage(WM_SYSCOMMAND,SC_MAXIMIZE,0);   //最大化窗口
}

void CTest1::OnBnClickedOk()
{
 SendMessage(WM_SYSCOMMAND,SC_MINIMIZE,0);  //最小化窗口
}

 

 void CTest1::OnBnClickedCancel()
{
 SendMessage(WM_SYSCOMMAND,SC_RESTORE,0);  //恢复窗口
}

 

10、设置标题栏:

void CTest2::OnBnClickedButton1()
{
 CString m_GetStr=_T("");
 GetWindowText(m_GetStr);
 SetDlgItemText(IDC_EDIT2,m_GetStr);
}

 

11、屏蔽键盘按键功能:

BOOL CTest2::PreTranslateMessage(MSG* pMsg)   //添加PreTranslateMessage函数来处理屏蔽按ESC与ENTER键。
{
 // TODO: 在此添加专用代码和/或调用基类
 if (pMsg->message==WM_KEYDOWN)
 {
  if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
  {
  AfxMessageBox(_T("屏蔽按ESC或ENTER键退出"));
 

   return TRUE;
  }
 }

 return CDialog::PreTranslateMessage(pMsg);
}

 

技术改变世界! --狂诗绝剑
目录
相关文章
|
7月前
|
存储 JSON 搜索推荐
如何使用 saplink 安装其他网站上提供的 ABAP 程序试读版
如何使用 saplink 安装其他网站上提供的 ABAP 程序试读版
75 0
|
22天前
|
C++
【C++】C++封装成DLL并调用(初学者快速入门)
【C++】C++封装成DLL并调用(初学者快速入门)
|
6月前
|
Windows
vc入门宝典十二(调用其它程序)
vc入门宝典十二(调用其它程序)
|
C# C++ 计算机视觉
Qt开发Activex笔记(一):环境搭建、基础开发流程和演示Demo
Qt开发Activex笔记(一):环境搭建、基础开发流程和演示Demo
|
测试技术 C# 图形学
Unity C#代码封装dll文件完美教程(Chinar-开发者必经之路)
Unity C#代码封装dll文件完美教程(Chinar-开发者必经之路) 将脚本封装为DLL库,本文提供全流程,中文翻译。 助力快速完成 Unity C#脚本文件封装为 DLL 库文件 DLL (Dynamic Link Library) —— 中文:动态链接库
5201 0

热门文章

最新文章