MFC中一些常用的消息响应实例

通过类向导可以自动生成消息响应函数,重载其抽象方法,实现响应

1:WM_TIMER消息响应计时器,关闭计时器用KillTimer(IDEVent)表示
如实现对话框编辑框文字逐个出现
a:在对话框或编辑框添加两个变量 int m_nCount;

                                                    CString m_strBuff;

b:重载OnInitDialog()

              SetTimer(1,900,NULL);
      m_strBuff = "曾经有一份真挚的感情摆在我的面前,我没有珍惜.........\r\n 我只想说一句话:you will die!";
      m_nCount = 0;

c:响应OnTimer()函数
void CTimerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnTimer(nIDEvent);
if (m_nCount {
CString str = m_strBuff.Left(m_nCount+1);
char ch = m_strBuff.GetAt(m_nCount);
if (ch<0||ch>256)
{
m_nCount++; // 非汉字则双倍时间
}
m_nCount++;
GetDlgItem(IDC_TEXT)->SetWindowText(str);
CEditpEdit=(CEdit)GetDlgItem(IDC_TEXT);
pEdit->SetSel(m_nCount,m_nCount); //使光标刷新

}
else
{
KillTimer(1);
}
}
补充:实现编辑框显示系统时间并可修改
a:为编辑框添加int变量

               m_year = 0;
       m_month = 0;
       m_day = 0;
       m_hour = 0;
       m_minute = 0;
       m_second = 0;

b:在OnInitDialog()函数添加

               SYSTEMTIME sys;

::GetLocalTime(&sys);
m_year = sys.wYear;
m_month = sys.wMonth;
m_day = sys.wDay;
m_hour = sys.wHour;
m_minute = sys.wMinute;
m_second = sys.wSecond;
SetTimer(0,1000,NULL); //设置计时器1秒一次

c:响应OnTimer函数
void CCalendarDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default.
SYSTEMTIME sys;
::GetLocalTime(&sys);
m_year=sys.wYear;
m_month=sys.wMonth;
m_day=sys.wDay;
m_hour=sys.wHour;
m_minute=sys.wMinute;
m_second=sys.wSecond;
UpdateData(FALSE);

CDialog::OnTimer(nIDEvent);
}

d:响应按钮实现时间修改
void CCalendarDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
SYSTEMTIME sys;
::GetLocalTime(&sys);
sys.wYear=m_year;
sys.wDay=m_day;
sys.wMonth=m_month;
sys.wHour=m_hour;
sys.wMinute=m_minute;
sys.wSecond=m_second;
::SetLocalTime(&sys);

}

e:响应OnDestory函数,实现退出关闭计时器
void CCalendarDlg::OnDestroy()
{
CDialog::OnDestroy();

// TODO: Add your message handler code here
KillTimer(0);
}

2:使控件自动调整大小。
a:添加变量 CRect m_rect;
b:在OnInitDialog()中加入GetClientRect(&m_rect);//获取大小信息 OnSize类方法
c: 类向导添加OnSize类,加入
void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

CWnd *pWnd; pWnd = GetDlgItem(IDC_EDIT1);
if(pWnd)
{
CRect rect;

 pWnd->GetWindowRect(&rect);  

ScreenToClient(&rect);
//cx/m_rect.Width();

 rect.left=rect.left*cx/m_rect.Width();
 rect.right=rect.right*cx/m_rect.Width();  

rect.top=rect.topcy/m_rect.Height();
rect.bottom=rect.bottom
cy/m_rect.Height();
pWnd->MoveWindow(rect); //获得新大小

 GetClientRect(&m_rect);                        //保存新大小

}
}

3:通过响应按钮启动字体对话框来作用于编辑框 OnButton响应 及变量传递 及CFontDialog类(字体对话框)
a:声明变量 CColorDialog类(颜色对话框)

  LOGFONT logfont;//旧字体

CFont font;//新字体
b:添加编辑框变量contolr类m_Eidt
c:响应按钮中添加
CFontDialog fontDialog;
//CFontDialog *fontDialog=new CFontDialog(NULL,CF_EFFECTS|CF_SCREENFONTS,NULL,NULL);
if (fontDialog.DoModal()==IDOK)
{
//LOGFONT logfont;
//CFont font;

                  font.Detach();          分离font,使新字体新生。

fontDialog.GetCurrentFont(&logfont);
font.CreateFontIndirect(&logfont);
m_Eidt.SetFont(&font);
}
加入 CFont* e=m_Edit.GetFont();

   e->GetLogFont(&a);
           CFontDialog c(&a);    使其首选编辑框字体。

补充:pDC->SetTextColor(fo.GetColor())可以控制控件颜色,在响应OnCtlColor中添加if(nCtlColor=CTLCOLOR_EDIT)可控制编辑框颜色。有的可以控制背景颜色。
*声明全局变量COLORREF m_clr;
按钮响应颜色对话框并得到值
CColorDialog colorDlg;
if (colorDlg.DoModal())
{
m_clr=colorDlg.GetColor();
Invalidate();
}
响应OnCtrColor函数释放得到的值如:pDC->SetTextColor(m_clr);//可为颜色变量//或在字体对话框添加m_clr=fontDialog.GetColor();

4:使窗体保持最前 //On按钮响应
添加::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,-1,-1,-1,-1,SWP_NOMOVE/SWP_NOSIZE);编译运行即可。

5:定义光标热区 //WM_SETCURSOR消息响应OnSetCursor函数
处理·ID添加消息响应代码
switch(pWnd->GetDlgCtrlID())
{
case IDC_BUTTON1:
{
SetCursor(AfxGetApp()->LoadCursor(IDC_HAND));
GetDlgItem(IDC_STATIC)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC)->SetWindowText(“定义光标热区响应”);
return TRUE;
}
break;
default:
{
GetDlgItem(IDC_STATIC)->ShowWindow(SW_HIDE);
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
return TRUE;
}
}
return CDialog::OnSetCursor(pWnd, nHitTest, message);

5:响应按钮使单文档程序最小化无效
void CMainFrame::OnBotton1()
{
// TODO: Add your command handler code here

LONG style = ::GetWindowLong(m_hWnd,GWL_STYLE);
style&=~(WS_MINIMIZEBOX); //若设为有效改为style|=WS_MINIMIZEBOX
::SetWindowLong(m_hWnd,GWL_STYLE,style);
CRect rc;
GetWindowRect(&rc);
::SetWindowPos(m_hWnd,HWND_NOTOPMOST,rc.left,rc.top,rc.Width(),rc.Height(),SWP_DRAWFRAME);
}

6:响应按钮使单文档程序系统按钮关闭无效
void CMainFrame::OnBotton2()
{
// TODO: Add your command handler code here
CMenu *pMenu=GetSystemMenu(FALSE);
int x=pMenu->GetMenuItemCount();
UINT pID=pMenu->GetMenuItemID(x-1);

pMenu->EnableMenuItem(pID,MF_DISABLED);                                                             //若设为有效改为MF_ENABLED

}

7:响应按钮使程序开机自动执行
a:编辑框添加变量 CStringm_File;

b:响应按钮浏览exe文件
void CAutoRunDlg::OnBrowse()
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg(TRUE,T(“EXE”),T(“.exe”),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(_T(“Executable File(.exe)|*.exe||“)));
if (fileDlg.DoModal()==IDOK)
{
m_File=fileDlg.GetPathName();
UpdateData(FALSE);
}
}

c:响应按钮使程序写入数据实现自动
void CAutoRunDlg::OnApply()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
LPCTSTR filename;
filename=m_File;
WritePrivateProfileString(T(“windows”),T(“load”),filename,_T(“c:windows\win.ini”));

}

7:限定释放鼠标到一定区域
a:响应按钮使鼠标限定在一定区域
void CLimitCursorPosDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CWnd* pWnd=GetDlgItem(IDC_STATIC);
CRect rc;
pWnd->GetWindowRect(&rc);
ClipCursor(&rc);

}

b:响应按钮使鼠标释放
void CLimitCursorPosDlg::OnButton2()
{
// TODO: Add your control notification handler code here
ClipCursor(NULL);

}

如果想要了解更多,欢迎加入QQ群162876848,共同探讨吧!

你可能感兴趣的:(mfc,实例)