MFC常用技巧

SetTimer(1,1000,NULL); 

 

1表示定时器的ID,1000表示没1000ms也就是1s调用一次处理函数,最后一个参数是处理的函数,如果填NULL表示,使用系统默认的,默认处理函数是OnTimer()那个函数,在这里面写你要执行的操作就可以了,如果你有多个定时器要在这里写if(id == 1)类似的东西去区分不同的定时器,更换贴图最好是在OnTimer里面更换当前显示的图片编号,然后刷新界面,更换图片的工作让OnPaint去做,让他们各司其职。
关闭定时器:KillTimer(1) 
启动计时器 
UINT ID_TIMER1 = 1 , ID_TIMER2 = 2   //设置id
然后再你需要的时机依次启动各个计时器:
SetTimer(ID_TIMER1 , 300);
……
SetTimer(ID_TIMER2 , 400);
……
……
OnTimer函数中这样写:
void CMyClass::OnTimer(UINT nIDEvent) 
{
    switch(nIDEvent)
    {
     case ID_TIMER1:
         // id为1的计时器要做的事。
         ……
         break;
     case ID_TIMER2:
         //id为2的计时器要做的事。
         ……
         break;
     ……
     ……
    }
}  
MFC静态文本与窗口背景色一致
重写WM_CTLCOLOR :

代码:
HBRUSH CWeatherTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
switch (pWnd->GetDlgCtrlID())
{
   case IDC_STATIC_WEEK:   //为静态文本框的资源ID号
   {
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(0,0,0));
    return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
    Invalidate();
   }
   break;
}
return hbr;
} 

CRect rect; CWnd* pControl = GetDlgItem(IDC_TIMER); pControl->GetWindowRect(&rect); //获得子控件的屏幕坐标; this->ScreenToClient(&rect); //子控件屏幕坐标映射到控件客户区; InvalidateRect(&rect,TRUE); 

 

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_RETURN) return TRUE; if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_ESCAPE) return TRUE; return CDialog::PreTranslateMessage(pMsg); } //----------------------------------------------- // 另一种方法 //----------------------------------------------- BOOL CYourDlg::PreTranslateMessage(MSG* pMsg) { { //屏蔽回车和ESC键 //屏蔽ESC键退出 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == 0x1b) return TRUE; //回车 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == 0x0d) return TRUE; } } 

 

 

PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当你需要在MFC之前处理某些消息时,常常要在这里添加代码. 
BOOLCyouDialog:PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_KEYDOWN && pMsg->wParam==67)//键盘对应数字或ASCII码 { ///OnKeyDown()中的程序 } return CDialog::PreTranslateMessage(pMsg); } #define VK_LBUTTON 0x01 #define VK_RBUTTON 0x02 #define VK_CANCEL 0x03 #define VK_MBUTTON 0x04 #define VK_BACK 0x08 #define VK_TAB 0x09 #define VK_CLEAR 0x0C #define VK_RETURN 0x0D #define VK_SHIFT 0x10 #define VK_CONTROL 0x11 #define VK_MENU 0x12 #define VK_PAUSE 0x13 #define VK_CAPITAL 0x14 #define VK_KANA 0x15 #define VK_HANGEUL 0x15 #define VK_HANGUL 0x15 #define VK_JUNJA 0x17 #define VK_FINAL 0x18 #define VK_HANJA 0x19 #define VK_KANJI 0x19 #define VK_ESCAPE 0x1B #define VK_CONVERT 0x1C #define VK_NONCONVERT 0x1D #define VK_ACCEPT 0x1E #define VK_MODECHANGE 0x1F #define VK_SPACE 0x20 #define VK_PRIOR 0x21 #define VK_NEXT 0x22 #define VK_END 0x23 #define VK_HOME 0x24 #define VK_LEFT 0x25 #define VK_UP 0x26 #define VK_RIGHT 0x27 #define VK_DOWN 0x28 #define VK_SELECT 0x29 #define VK_PRINT 0x2A #define VK_EXECUTE 0x2B #define VK_SNAPSHOT 0x2C #define VK_INSERT 0x2D #define VK_DELETE 0x2E #define VK_HELP 0x2F /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */ /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */ #define VK_LWIN 0x5B #define VK_RWIN 0x5C #define VK_APPS 0x5D #define VK_NUMPAD0 0x60 #define VK_NUMPAD1 0x61 #define VK_NUMPAD2 0x62 #define VK_NUMPAD3 0x63 #define VK_NUMPAD4 0x64 #define VK_NUMPAD5 0x65 #define VK_NUMPAD6 0x66 #define VK_NUMPAD7 0x67 #define VK_NUMPAD8 0x68 #define VK_NUMPAD9 0x69 #define VK_MULTIPLY 0x6A #define VK_ADD 0x6B #define VK_SEPARATOR 0x6C #define VK_SUBTRACT 0x6D #define VK_DECIMAL 0x6E #define VK_DIVIDE 0x6F #define VK_F1 0x70 #define VK_F2 0x71 #define VK_F3 0x72 #define VK_F4 0x73 #define VK_F5 0x74 #define VK_F6 0x75 #define VK_F7 0x76 #define VK_F8 0x77 #define VK_F9 0x78 #define VK_F10 0x79 #define VK_F11 0x7A #define VK_F12 0x7B #define VK_F13 0x7C #define VK_F14 0x7D #define VK_F15 0x7E #define VK_F16 0x7F #define VK_F17 0x80 #define VK_F18 0x81 #define VK_F19 0x82 #define VK_F20 0x83 #define VK_F21 0x84 #define VK_F22 0x85 #define VK_F23 0x86 #define VK_F24 0x87 #define VK_NUMLOCK 0x90 #define VK_SCROLL 0x91 A 至 Z 键与 A – Z 字母的 ASCII 码相同: 值 描述/r 65 A 键/r 66 B 键/r 67 C 键/r 68 D 键/r 69 E 键/r 70 F 键/r 71 G 键/r 72 H 键/r 73 I 键/r 74 J 键/r 75 K 键/r 76 L 键/r 77 M 键/r 78 N 键/r 79 O 键/r 80 P 键/r 81 Q 键/r 82 R 键/r 83 S 键/r 84 T 键/r 85 U 键/r 86 V 键/r 87 W 键/r 88 X 键/r 89 Y 键/r 90 Z 键/r 0 至 9 键与数字 0 – 9 的 ASCII 码相同: 值 描述/r 48 0 键/r 49 1 键/r 50 2 键/r 51 3 键/r 52 4 键/r 53 5 键/r 54 6 键/r 55 7 键/r 56 8 键/r 57 9 键/r 下列常数代表数字键盘上的键: 值 描述/r 0x60 0 键/r 0x61 1 键/r 0x62 2 键/r 0x63 3 键/r 0x64 4 键/r 0x65 5 键/r 0x66 6 键/r 0x67 7 键/r 0x68 8 键/r 0x69 9 键/r 

 

你可能感兴趣的:(timer,null,delete,mfc,insert,scroll)