初次用MFC,对所做的小东西做个总结,大部分都是从网上学习的,希望对读到的人有稍微的帮助
1.要创建的非模态窗口:继承于CDHtmlDialog
#define WM_USER_DVRERRWND_CLOSE WM_USER + 1001
BOOL CDDvrLoginErrMessHtmlDialog::OnInitDialog()
{
CDHtmlDialog::OnInitDialog();
//将焦点设置到ID_BTNCHECKDVRLOG控件
GetDlgItem(ID_BTNCHECKDVRLOG)->SetFocus();
//设定窗口显示位置为右下角任务栏的上面
CClientDC dc(this);
CRect rc;
GetClientRect(&rc);
int x = GetSystemMetrics(SM_CXFULLSCREEN) - rc.Width() - 8;
int y = GetSystemMetrics(SM_CYFULLSCREEN) - rc.Height() - 8;
SetWindowPos(NULL, x, y, 0, 0 ,SWP_NOSIZE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
CDDvrLoginErrMessHtmlDialog::~CDDvrLoginErrMessHtmlDialog()
{
//Destroy窗口
::DestroyWindow(GetDlgItem(IDD_DVRLOGINERRORMESSAGE_DIALOG)->GetSafeHwnd());
//通知父窗口
::PostMessage(AfxGetApp()->GetMainWnd()->GetSafeHwnd(),WM_USER_DVRERRWND_CLOSE,0,0);
}
//打开txt文件查看整个文件
ShellExecute(NULL, _T("open"),_T(".\\abc.txt"),"",NULL,SW_SHOWMAXIMIZED);
当然还有对OnOk和OnDestroy方法的重载
2.,定时器,定时发消息,查看文件是否存在并一旦更新则弹出窗口,并显示最新的一条信息,在模态父窗口中创建非模态的子窗口
//自定义消息
#define WM_USER_DVRERRMESSWND_UPDATA WM_USER + 1002
消息map中添加
ON_MESSAGE(WM_USER_DVRERRMESSWND_UPDATA,OnDVRErrMessUpdataWindownsData)
ON_MESSAGE(WM_USER_DVRERRWND_CLOSE,OnDVRErrMessWindownsClose)
enum{ TW_START_SVR_TIMER_ID = 0x19780507 };
::KillTimer(this->GetSafeHwnd(), TW_START_SVR_TIMER_ID );
//每5分钟刷新一下dvr登陆失败窗口信息
::SetTimer(this->GetSafeHwnd(), TW_START_SVR_TIMER_ID ,5*60*1000L, TimerProc );
void CALLBACK TimerProc( HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime )
{
::PostMessage(hWnd,WM_USER_DVRERRMESSWND_UPDATA,0,0);
}
LRESULT OnDVRErrMessUpdataWindownsData( WPARAM wParam, LPARAM lParam )
{
//dvrLoginErrMessDlg 子窗口指针
if (dvrLoginErrMessDlg==NULL)
{
dvrLoginErrMessDlg = new CDDvrLoginErrMessHtmlDialog(this);
dvrLoginErrMessDlg->Create(IDD_DVRLOGINERRORMESSAGE_DIALOG,this);
dvrLoginErrMessDlg->ShowWindow(SW_HIDE);
}
CString cs_tempStr1,cs_tempStr2;
CStdioFile file;
if (file.Open(TEXT(“.\\abc.txt”),CFile::modeRead))
{
do
{
if (cs_tempStr2!="")
{
cs_tempStr1 = cs_tempStr2;
}
} while(file.ReadString(cs_tempStr2));
file.Close();
if (cs_tempStr1!=dvrLoginErrMessDlg->m_csDvrErrMess)
{
dvrLoginErrMessDlg->m_csDvrErrMess = cs_tempStr1;
dvrLoginErrMessDlg->UpdateData(FALSE);
dvrLoginErrMessDlg->ShowWindow(SW_SHOWNORMAL);
}
}
return true;
}
3.
//删除文件操作
DeleteFile(_T(“”.\\abc.txt“));
//文件的创建和信息的插入
ofstream fout;
fout.open(_T(”.\\abc.txt”),ios::out| ios::app,0);
if (fout.is_open())
{
fout<<"时间 = "<<JTimeUtils::FormatTimeForUI(PTime())
<< "; 设备IP = " << pUS->remoteIp
<< "; 设备端口 = " << pUS->remotePort
<< "; 设备DVR ID = " << pUS->dto.dvrEquipId
<< "; 设备DVR通道号 = " << pUS->dto.channelNo
<<"; 出错原因 = 设备断电或网络不通"
<<"\n";
fout.close();
}