前段時間,將近花了一周至兩周上班和上班后的閑余時間,做了一個用於調試和測試工作項目的應用軟件,下面將實現軟件的重要步驟及主要功能講解一遍,方便日后查閱。
程序開始后,提示登錄框,輸入用戶名,密碼后,登錄進去主窗體,效果圖如下:
下面將主要實現的功能函數要點進行描述,具體實現如下:
一、設置主窗體大小
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);
}