MFC 小知识总结一

 1   vs2005 中  垂直删除列的快捷键为: ALT+A                                                                 

 

ALT+SHIFT+ENTER      全屏 转换。。。。。。  用了这么久VS2005了 ,现在才知道有全屏编辑这个功能,鄙视一下:》

 

 

ALT+SHIFT+方向键        用来选择多行 , 这个功能很好 ,最近我鼠标总是出问题,又换了一个还是出问题,用鼠标选的时候,往往刚选上那部分区域,又不小心撤销了,非常麻烦和愤怒。  幸亏找到了这个快捷键,直接用键盘就可以方便的选择一行或多行 。 尤其是在删除空白行时,很实用

 

 

ALT+F8           这个键真好,尤其是对于格式混乱的代码,简直是福音,使用后,可以将混乱的格式整理好。 有了这个键,省去了好多麻烦,其功劳甚大,完全是:整理格式之必用快捷键

 

SHIFT+END  一行中光标右面的

SHIFT+HOME 一行中光标左面的      虽作用不如前面几个大, 但对于鼠标不灵便的机子来说,有时候它还是有用武之地的。。。。       :《  再一次愤慨,公司的鼠标怎么那么差劲呢,哎,换了一个了还不行 真是影响心情呀。。。。

 

Ctrl+Alt+space  显示某个函数的参数信息


CTRL+M CTRL +M  折叠函数段落 , 当注释很多时,可以用这个组合折叠  按两次ctrl+m       方法:   选中函数(不要将函数{}也选中,否则,此函数整个折叠了),然后按这组合键,其内的注释就折叠起来了 。

对那些删了可惜,留了碍眼的注释 ,这是一个不错的方法

 

--------------------------------------------------------------------------------

F2 书签功能: Ctrl+F2 --在某行设置一个书签(再按一次次是取消)

F2 --跳到下一个书签位置

Shift+F2 --跳到上一个书签位置

Ctrl+Shift+F2 --删除所有书签

 

 

 

 

2   加载ICON位图的函数     HICON hIcon = ::LoadIcon( ::AfxGetInstanceHandle(), (LPCTSTR)IDI_ICON1 );// IDI_ICON1是图标资源的ID

 

 

 3  获得控件的CWnd指针  比如要获得CStatic控件的CWnd指针 

         CWnd   *   pStatic   =   (CWnd   *)GetDlgItem(IDC_YOUR_STATIC); 

        不能使用m_image.GetWindow(GW_OWNER) ; ,其中m_image为CStatic对象  //  其返回的应该是对话框的窗口,而不是对话框中CStatic控件的窗口

 

 

 4  StretchBlt函数在放缩图片时 会失真,解决方法:修改放缩模式。如下例:

 

[cpp]  view plain copy
  1. pDC->SetStretchBltMode(HALFTONE);  // 将周围像素的平均值作为改点的像素值,一般这样就看不出失真了  
  2.   
  3. pDC->StretchBlt(rect.TopLeft().x,rect.TopLeft().y,rect.Width(),rect.Height(),&MemDC,0,0,m_bm.bmWidth,m_bm.bmHeight,SRCCOPY);  


 

 

5  设置光标

 

1)设置光标形状

 

 

应用程序有时会设置和显示自己的光标,这需要调用SDK的API函数SetCursor和LoadCursor:

 

[cpp]  view plain copy
  1. HCURSOR SetCursor( // 返回原来光标的指针  
  2.   
  3.   HCURSOR hCursor   // handle to cursor  
  4.   
  5. );  
  6.   
  7. HCURSOR LoadCursor( // 返回新装入光标的指针  
  8.   
  9.   HINSTANCE hInstance,  // handle to application instance  
  10.   
  11.   LPCTSTR lpCursorName  // name string or cursor resource identifier  
  12.   
  13. );  


 

其中,hInstance 一般为NULL,lpCursorName可取若干符号常量值,其中常用的有:

符号常量

光标

形状

操作

IDC_ARROW

标准箭头(Standard arrow)

指向

IDC_CROSS

十字标线(Crosshair)

绘图

IDC_IBEAM

I形条(I-beam)

文本

IDC_WAIT

沙漏(Hourglass)

等待

IDC_APPSTARTING

标准箭头与小沙漏

等待

IDC_HAND

手形(Win2K以上)

超链接

IDC_HELP

箭头与问号(mark)

相关帮助

IDC_NO

斜圆(Slashed circle)

禁止

IDC_SIZEALL

四箭头

移动

IDC_SIZENESW

下斜双箭头

改变大小

IDC_SIZENS

垂直双箭头

改变高度

IDC_SIZENWSE

上斜双箭头

改变大小

IDC_SIZEWE

水平双箭头

改变宽度

IDC_UPARROW

上箭头

选择列

例如,装入并设置沙漏光标:

[cpp]  view plain copy
  1. SetCursor(LoadCursor(NULL, IDC_WAIT));  


 

在CWinApp类中也有两个LoadCursor函数,其原型为:

HCURSOR LoadCursor( LPCTSTR lpszResourceName ) const;

HCURSOR LoadCursor( UINT nIDResource ) const;

其中,返回值都为新装入光标的指针,lpszResourceName 与nIDResource分别为光标资源的名串与ID。如


注: 加载的是.cur文件 不是.ico

 

[cpp]  view plain copy
  1. extern CDrawApp theApp;  
  2. SetCursor(theApp.LoadCursor(IDC_MYCURSOR));  


 

注意:若类光标非空,则在设置光标后,每当用户移动鼠标时,Windows系统就会自动恢复类光标。解决办法是在移动鼠标消息的响应函数OnMouseMove中来设置光标,如:

[cpp]  view plain copy
  1. void CDrawView::OnMouseMove(UINT nFlags, CPoint point) {  
  2.     SetCursor(LoadCursor(NULL, IDC_CROSS));  
  3. ... ...  
  4.     CView::OnMouseMove(nFlags, point);  
  5. }     


 

或在设置光标的同时也设置鼠标捕捉,则Windows会在释放鼠标捕捉后才恢复类光标。如:

[cpp]  view plain copy
  1. SetCapture();  
  2.   
  3. SetCursor(LoadCursor(NULL, IDC_CROSS));  


 

 

 

//--------------------------------------------------------------------------------------------------------------------

 

[cpp]  view plain copy
  1. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));  
[cpp]  view plain copy
  1. SetCursor(LoadCursor(NULL,IDC_HAND));  

 

[cpp]  view plain copy
  1. SetCursor((HCURSOR)::LoadImage(NULL,"res//XiangPi.ico",IMAGE_ICON,20,20,LR_LOADFROMFILE));    
[cpp]  view plain copy
  1. SetCursor((HCURSOR)::LoadImage(NULL,"res/cursor1.cur",IMAGE_CURSOR,0,0,LR_LOADFROMFILE));    

 

[cpp]  view plain copy
  1. 在SDI view中调用  
  2.   
  3. SetCursor(AfxGetApp()->LoadCursor(IDC_ERASE));  

 

 

2)裁剪光标

 

可以用API函数ClipCursor将你的光标限制在由lpRect指定的矩形区域内:

            BOOL ClipCursor(CONST RECT *lpRect); // 成功返回非0

其中,矩形区域的坐标是相对于整个屏幕的,若lpRect为NULL,则取消对鼠标移动的限制。如:

 

[cpp]  view plain copy
  1. <strong>ClipCursor(new CRect(100, 100, 300, 300));  
  2. ClipCursor(NULL);  
  3. </strong>  


 

可以用API函数GetClipCursor来获得当前鼠标的裁剪区域,坐标也为屏幕的坐标,若无裁剪区,则lpRect中的矩形为整个屏幕。

[cpp]  view plain copy
  1. BOOL GetClipCursor(LPRECT lpRect); // 成功返回非0  


3)获取与设置光标位置

 

除了可在鼠标消息的响应函数中,由输入参数point得到鼠标位置外,还可以在任何地方调用API函数GetCursorPos来得到当前的鼠标位置:

 

[cpp]  view plain copy
  1. BOOL GetCursorPos(LPPOINT lpPoint); // 成功返回非0  
  2. //其中lpPoint的坐标为屏幕的坐标,而且与映射模式无关。  

还可以调用另一个API函数SetCursorPos来设置鼠标位置:(也为屏幕的坐标)

 

[cpp]  view plain copy
  1. BOOL SetCursorPos(int X, int Y); // 成功返回非0  


 

6 将光标定位到对话框的某个控件上

 

[cpp]  view plain copy
  1. //比如定位到IDC_MOVE 按钮上       
  2. GetDlgItem(IDC_MOVE)->SetFocus();  

 

 

7 获得光标所在位置

 

[cpp]  view plain copy
  1. CPoint pos;  
  2. ::GetCursorPos(&pos);  


 有时候使用GetCursorPos取得的光标位置不正确,如果是在消息中处理,还可使用消息中自带的光标位置

 

[cpp]  view plain copy
  1. GetCursorPos(&pt);  
  2. pt=GetCurrentMessage()->pt;  
  3.   
  4. 监视时发现:  
  5.   
  6. 使用 GetCursorPos(&pt); pt为:x=344 y=232  
  7.   
  8. 使用 pt= GetCurrentMessage()->pt; 结果为:x=16 y=80 。。。。。 此结果为正确结果  
  9.    


至于 为什么会出错,是因为没有将屏幕坐标转换为客户坐标

 

[cpp]  view plain copy
  1. CPoint point;  
  2. //获得屏幕坐标  
  3. GetCursorPos(&point);  
  4. //屏幕坐标转换为客户坐标  
  5. ScreenToClient(&point);  


 

 

 

 8  OnMouseMove(UINT nFlags, CPoint point)  判断鼠标左键是否被按下

 

nFlags取值如下:

 

符号常量

数值

含义

MK_CONTROL

8

Ctrl键被按下

MK_LBUTTON

1

左鼠标键被按下

MK_MBUTTON

16

中鼠标键被按下

MK_RBUTTON

2

右鼠标键被按下

MK_SHIFT

4

Shift键被按下

 
[cpp]  view plain copy
  1. void OnMouseMove(UINT nFlags, CPoint point) 通过这个函数我们可以得到两个参数:  
  2. uFlags 和 point ,这两个参数.我们在MSDN种查到对这两个参数的描述:  
  3. nFlags  
  4. Indicates whether various virtual keys are down. This parameter can be any combination of the following values:   
  5. 指示哪些键被按下。这个参数可以是以下值的任意组合:  
  6. ·    MK_CONTROL   Set if the CTRL key is down. //CTRL键  
  7. ·    MK_LBUTTON   Set if the left mouse button is down.//鼠标左键  
  8. ·    MK_MBUTTON   Set if the middle mouse button is down.//鼠标中键  
  9. ·    MK_RBUTTON   Set if the right mouse button is down.//鼠标右键  
  10. ·    MK_SHIFT   Set if the SHIFT key is down. //SHIFT键  
  11.   
  12. if ((nFlags & MK_LBUTTON)==MK_LBUTTON) //判断左键是否按下  

 

9 鼠标拖动消息

windows鼠标消息中没有拖动消息,可在鼠标移动消息中,判断左键是否按下来代替

 

[cpp]  view plain copy
  1. void CDrawView::OnMouseMove(UINT nFlags, CPoint point) {  
  2.     if (nFlags&MK_LBUTTON) {  
  3.     ... ...  
  4.     }  
  5.     CView::OnMouseMove(nFlags, point);  
  6. }  

 

10 捕捉鼠标消息

 

       如果用户在客户区按下鼠标,拖动鼠标到客户区外后才释放鼠标,则应用程序得不到鼠标被释放的消息,可能会产生错误的响应操作。为避免这种事情发生,可以调用CWnd类的成员函数SetCapture来捕捉鼠标,使得以后的鼠标消息都发给自己,而不论鼠标的光标是否在自己窗口的客户区。

      当程序不再需要鼠标消息后,必须调用函数ReleaseCapture释放对鼠标的捕捉,从而使其他程序可以获得它自己的鼠标消息。在窗口失去对鼠标的捕捉时,会收到Windows发送的WM_CAPTURECHANGED消息。还可以调用CWnd类的成员函数GetCapture来获得当前捕捉鼠标的窗口的指针。这些函数的原型如下:

 

[cpp]  view plain copy
  1. CWnd* SetCapture( );   
  2. BOOL ReleaseCapture();  
  3. static CWnd* PASCAL GetCapture( );  

 

程序一般是在用户按下鼠标键时设置鼠标捕捉,而在用户释放鼠标键后释放鼠标捕捉。如:


 

[cpp]  view plain copy
  1. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) {  
  2.     m_bLButtonDown = TRUE;  
  3.     SetCapture();  
  4.     ... ...  
  5.     CView::OnLButtonDown(nFlags, point);  
  6. }  
  7. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) {  
  8.     ... ...  
  9.     m_bLButtonDown = FALSE;  
  10.     ReleaseCapture();  
  11.     CView::OnLButtonUp(nFlags, point);  
  12. }  


 

 

 

 


 

11  在PreTranslateMessage(MSG* pMsg) 预处理消息  将指定的消息发送出去

 

[cpp]  view plain copy
  1. BOOL CImageView::PreTranslateMessage(MSG* pMsg)  
  2. {  
  3.     // TODO: 在此添加专用代码和/或调用基类  
  4.   
  5.     if(pMsg->message==WM_KEYDOWN){  //  窗口默认不响应方向键按下WM_KEYDOWN消息  所以这样需要预先发送以下  
  6.         switch(pMsg->wParam)  
  7.         {  
  8.         case VK_LEFT:  
  9.             SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);  
  10.             return TRUE;  // 不使用默认的方向键调用方式      如果不加return ture  按左方向键时 光标会移动左面的控件上  
  11.         case VK_RIGHT:  
  12.             SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);  
  13.             return TRUE;  
  14.   
  15.         case VK_DOWN:  
  16.             SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);  
  17.             return TRUE;  
  18.   
  19.         case VK_UP:  
  20.             SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);  
  21.             return TRUE;  
  22.         }  
  23.     }  
  24.   
  25.     else if (pMsg->message==WM_KEYUP)  
  26.     {  
  27.         if(pMsg->wParam==VK_LEFT)  
  28.             SendMessage(WM_KEYUP,pMsg->wParam,pMsg->lParam);  
  29.     }  
  30.     return CFormView::PreTranslateMessage(pMsg);  
  31. }  


将消息发送到其它窗口中, 需先获得其它窗口的指针或句柄

 

[cpp]  view plain copy
  1. BOOL CModelDlg::PreTranslateMessage(MSG* pMsg)  
  2. {  
  3.     // TODO: 在此添加专用代码和/或调用基类  
  4.    
  5.     if (pMsg->message==WM_KEYDOWN)  
  6.     {  
  7.         switch (pMsg->wParam)  
  8.         {  
  9.         case VK_F7:  
  10.         case VK_DOWN:  
  11.         case VK_LEFT:  
  12.         case VK_RIGHT:  
  13.             {  
  14.                 CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));  
  15.                 pView->SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);  
  16.             }  
  17.             break;  
  18.         default:  
  19.             break;  
  20.         }  
  21.     }  
  22.   
  23.     if(hAcc)     
  24.     {   
  25.         if(::TranslateAccelerator(m_hWnd,hAcc,pMsg))     
  26.             return(TRUE);   
  27.     }  
  28.     return CDialog::PreTranslateMessage(pMsg);  
  29. }  


 

 12 在 m_wndSplitter 拆分窗口中,获得指定窗口的指针

 

[cpp]  view plain copy
  1. CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));  


13 Unicode环境下,调试对话框

 

[cpp]  view plain copy
  1. AfxMessageBox(L"调试信息");  


14  使用一个按钮,通过改变文字及变量值  产生两种相反的信息

 

[cpp]  view plain copy
  1. void CImageView::OnBnClickedErase()  
  2. {  
  3.     if(m_berase==FALSE)  // 用来控制是否察除的BOOL变量  
  4.         m_berase=TRUE;  
  5.     else  
  6.         m_berase=FALSE;  
  7.   
  8.     if(m_berase)      
  9.         GetDlgItem(IDC_ERASE)->SetWindowText(_T("正在察除中"));     
  10.     else  
  11.         GetDlgItem(IDC_ERASE)->SetWindowText(_T("察除"));  
  12. }  


15 对话框若无标题栏时,则不能移动,  通过响应WM_NCHITTEST()事件 返回标题栏, 就可以移动了

 

[cpp]  view plain copy
  1. LRESULT CModelDlg::OnNcHitTest(CPoint point)  
  2. {  
  3.     // TODO: 在此添加消息处理程序代码和/或调用默认值  
  4.     CRect rc;  
  5.     GetClientRect(&rc);  
  6.     ClientToScreen(&rc);  
  7.     if(rc.PtInRect(point))  
  8.         return HTCAPTION;  
  9.     else  
  10.         return  CDialog::OnNcHitTest(point);  
  11. }  


16  建立非模态对话框

 

方法一:

[cpp]  view plain copy
  1. //类变量  
  2. CShowImageDlg m_dlg;  

 

[cpp]  view plain copy
  1. // 构造函数或初始化函数中或其它地方  
  2. m_dlg.Create(IDD_DIALOG_SHOW,this);  
  3. m_dlg.ShowWindow(SW_SHOW);  
  4. m_dlg.MoveWindow(0,0,20,20);  

注意:对话框属性 Style设置为Child时 ,生成的对话框是在父窗口内部的,不能遮盖其它窗口, 而Style属性设置为Popup时,却可以遮盖其它窗口

            此种方法不能很很好的传递父窗口指针


方法二:

[cpp]  view plain copy
  1. class CParent;  
  2. class SubCLASS:public CDialog  
  3. {  
  4.   
  5. public:  
  6.     CParent *pParent;  
  7.   
  8.     void WndCreate(CParent *p){  //自定义函数, 创建非模态对话框 并传递父窗口  
  9.   
  10.         Create(IDD_DIALOG_JIGU);  
  11.         ShowWindow(SW_SHOW);  
  12.         pParent=p;  
  13.     };  
  14.   
  15.   
  16. };  
  17.   
  18.   
  19. //在父对话框中创建  
  20. SubCLASS  subDlg;  
  21. subDlg.WndCreate(this);  


      

 


17  GetSysColor(int nIndex);  指定windows显示对象的颜色

 

[cpp]  view plain copy
  1. COLORREF color = ::GetSysColor(COLOR_3DFACE);  //3D阴影化对象的正面颜色  
  2. SolidBrush brush(Color(255,GetRValue(color),GetGValue(color),GetBValue(color)));  

 


 18  设置对话框位置

 

[cpp]  view plain copy
  1. SetWindowPos(&wndTopMost,0,0,width,height,SWP_SHOWWINDOW);  


使用SWP_SHOWWINDOW  当对话框移动到任务栏时,可以遮盖任务栏

 

[cpp]  view plain copy
  1. SetWindowPos(&wndTopMost,0,0,width,height,SWP_NOMOVE);  


使用SWP_NOMOVE 当对话框移动到任务栏时,位于任务栏之下

 

 

19 设置控件在对话框中的位置

 

[cpp]  view plain copy
  1. // m_colText为CRichEdit控件  
  2.       m_colText.MoveWindow(10,0,cx,cy); // (10,0)控件左上角,cx,cy控件宽与高   

 

20  获得对话框的边框大小

 

[cpp]  view plain copy
  1. CRect rc;  
  2.   
  3. GetClientRect(&rc); //获得对话框客户区大小    
  4.   
  5. GetWindowRect(&rc); // 加边框的大小  

 

     在资源中,将对话框Client Edge 设置为TRUE 时 ,容易调节大小,否则 调节大小的箭头可能不很灵便。。。 

     这个不一定具有普遍意义,只是我测试的程序是这样的,不知道为什么
 

21  获得屏幕宽高

 

[cpp]  view plain copy
  1. int  width=GetSystemMetrics(SM_CXSCREEN);  
  2. int  height=GetSystemMetrics(SM_CYSCREEN);  
[cpp]  view plain copy
  1. int borderWidth=GetSystemMetrics(SM_CXBORDER); // 窗口边框的尺寸  



 

 22  判断窗口自带的滚动条是否可见

    

        对于窗口自带的滚动条,使用 GetScrollBarCtrl(SB_HORZ) 返回的始终是NULL ,得不到滚动条句柄,因为滚动条作为窗口的一个元素存在,而不是一个对象,那怎么判断窗口是否显示滚动条了呢? 

        可以通过获得SCROLLBARINOF来进行判断

 

[cpp]  view plain copy
  1. SCROLLBARINFO sbInfo;  
  2. ZeroMemory((void *)&sbInfo,sizeof(SCROLLBARINFO));  
  3. sbInfo.cbSize=sizeof(SCROLLBARINFO);  
  4. BOOL bRet=::GetScrollBarInfo(m_colText.GetSafeHwnd(),OBJID_VSCROLL,&sbInfo);  
  5. DWORD dRet=sbInfo.rgstate[0];  
  6.   
  7. if (dRet&STATE_SYSTEM_INVISIBLE)  //不可见  
  8. {  
  9.   
  10. }else//可见  
  11.   
  12.   
  13. }  


 

 23 SDI程序运行时 窗口便最大化

 

 在 SDIApp::InitInstance()中添加 m_nCmdShow = SW_SHOWMAXIMIZED;

[html]  view plain copy
  1. CCommandLineInfo cmdInfo;  
  2. ParseCommandLine(cmdInfo);  
  3.   
  4. m_nCmdShow = SW_SHOWMAXIMIZED;  // 添加的语句 刚开始运行便最大化  


 

 

 24  对话框构造函数、OnSize()函数、初始化函数调用顺序

      

       构造函数--->OnSize()函数------>初始化函数    

       构造函数只是简单的初始化参数,而给对象具体的分配内存 等则是在初始化函数中,   但是,OnSize()中,通常要设置某对象的位置,第一次调用时,由于该对象还没有分配内存,因此要加个判断条件。通常是看能否GetSafeHWnd() 来判断,若判断不了,可根据其他初始值来判断

      

       一个非模态对话框调用DestroyWindow()后,销毁的是分配内存的资源,而构造函数中的参数并没有销毁,也就是说,当一个对话框调用DestroyWindow()后,我们仍然可以调用构造函数中的参数,并重新设置其值。

 

    当一个非模态对话框dlg.DestroyWindow()后,再调用dlg.Create(IDD,parentWnd); 重新生成对话框时, 它不再调用构造函数, 而是从OnSize()----->初始化函数  这个顺序调用。

    若原先对象是否存在的判断条件在构造函数中设定,则此时,判断对象是否存在的条件可能已经被修改(因为构造函数没有被重新调用),所以要重新设置判断条件。

 

 

 

25 SetTimer()设置定时器

 

MFC 中定时器的设置

 

      SetTimer(1,50,NULL);

 

然后响应改窗口的WM_TIMER消息 处理此定时器事件

[cpp]  view plain copy
  1. void CModelDlg::OnTimer(UINT_PTR nIDEvent)  
  2. {  
  3.     // TODO: 在此添加消息处理程序代码和/或调用默认值  
  4.   
  5.   
  6.   
  7.     if (nIDEvent==1)  
  8.     {  
  9.        ....// 处理改事件  
  10.   
  11.        KillTimer(1);//撤销改事件,否则每隔指定时间,会连续的发送改定时器事件  
  12.     }  
  13.   
  14.     CDialog::OnTimer(nIDEvent);  
  15. }  


 

26 确定某键是否按下   

 

 SHORT GetAsyncKeyState(int vKey);

参数:nVirtKey指出要检查键的虚键代码。结果的高位指出该键当前是否被按下(是为1,否为0)。

常用键的VK值:

  VK_SHIFT Shift键

  VK_LSHIFT 左Shift键

  VK_RSHIFT 右Shift键

  VK_CONTROL Ctrl键

  VK_LCONTROL 左Ctrl键

  VK_RCONTROL 右Ctril键

  VK_MENU Alt键

  VK_LMENU 左Alt键

  VK_RMENU 右Alt键

  VK_LBUTTON 鼠标左键

  VK_RBUTTON 鼠标右键

 

int  i=GetKeyState(VK_CONTROL)

int  j=GetAsyncKeyState(VK_CONTROL)

  

    返回值<0,说明该键被按下(高位为1 说明为负值),否则(高位为0 不为负数)表示 没有被按下

     

    另一种判断方式: 与0x8000相与

 

   0X8000  即: 1000 0000 0000 0000     由此判断最高位是否为1

  

[cpp]  view plain copy
  1. short sKeyState=0;//判断Ctrl是否按下  
  2. sKeyState=GetAsyncKeyState(VK_LCONTROL);  
  3. if((sKeyState & 0x8000)==0x8000 )   


 

    二者区别:

GetKeyState是检查window message发生时,某一key的状态。

GetAsyncKeyState是检查在调用该函数时,某一key的状态。

 

 

 

 

 27  动态修改模态对话框标题

模态对话框在DoModal之前向其发生消息是无效的,因为这是窗口根本就没有创建。
你应该通过设置变量的方式来操作,例如:

[cpp]  view plain copy
  1. class CDlgInput : public CDialog  
  2. {  
  3. public:  
  4.     ....  
  5.     void SetCaption(LPCTSTR lpszCaption) { m_strCaption = lpszCaption; }  
  6. private:  
  7.     CString m_strCaption;  
  8. };  
  9.   
  10. BOOL CDlgInput::OnInitDialog()  
  11. {  
  12.     CDialog::OnInitDialog();  
  13.   
  14.     SetWindowText(m_strCaption);  
  15.     return TRUE;  // return TRUE unless you set the focus to a control  
  16.               // EXCEPTION: OCX Property Pages should return FALSE  
  17. }  
  18.   
  19. CView::OnInputgrade()  
  20. {  
  21.     CDlgInput dlg;  
  22.     dlg.SetCaption("请输入第 1 位学生的成绩");  
  23.     dlg.DoModal();  
  24. }  


 

 

 28  使用文件对话框 选择文件

 

[cpp]  view plain copy
  1. CFileDialog  dlg(    
  2.     TRUE,    
  3.     _T("*"),     
  4.     _T("*.txt"),    
  5.     OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,     
  6.     _T("All Files(*.txt|*.txt||)")   
  7.     );    
  8. TCHAR    szbuffer[1024];    
  9. szbuffer[0]=0;    
  10. dlg.m_ofn.lpstrFile = szbuffer;    
  11. dlg.m_ofn.nMaxFile = 1024;    
  12.   
  13. if(IDOK==dlg.DoModal())    
  14. {    
  15.     POSITION   pos = dlg.GetStartPosition();    
  16.     CString    filepath;    
  17.     CStdioFile wfile;  
  18.   
  19.     while(pos!=NULL)    
  20.     {    
  21.         filepath = dlg.GetNextPathName(pos);  //pos 自动更新为下一个位置  
  22.         if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary))     
  23.         {  
  24.             AfxMessageBox(L"无法打开文件");   
  25.             return ;  
  26.         }  
  27.   
  28.         // 进行读写文件的操作   
  29.         // ----  
  30.         //-----  
  31.         wfile.Close();  
  32.     }  
  33. }  

 

 

 29 使用目录对话框 选择目录

[cpp]  view plain copy
  1. void CComputerNumDlg::OnBnClickedButton1()  
  2. {  
  3.   
  4.     BROWSEINFO bi;  
  5.     ZeroMemory(&bi,sizeof(BROWSEINFO));  
  6.     LPMALLOC pMalloc;  
  7.     LPITEMIDLIST pidl = SHBrowseForFolder(&bi);  
  8.   
  9.     if (pidl==NULL)  
  10.       return;  
  11.   
  12.     if(pidl != NULL)  
  13.     {  
  14.         TCHAR * path = new TCHAR[MAX_PATH];   
  15.           
  16.         SHGetPathFromIDList(pidl,path);  
  17.         //      MessageBox(NULL,path,TEXT("Choose"),MB_OK);  
  18.         if(SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放,之前忽略了  
  19.         {  
  20.             pMalloc->Free(pidl);  
  21.             pMalloc->Release();  
  22.         }  
  23.         m_filePath=path;  
  24.         UpdateData(FALSE);    
  25.   
  26.         delete [] path;  
  27.   
  28.     }  
  29.   
  30. }  


 

 30  CFileFind 判断文件是否存在

 

[cpp]  view plain copy
  1. CFileFind finder;  
  2. BOOL bWorking = finder.FindFile("*.*");  
  3. while (bWorking)  
  4. {  
  5.    bWorking = finder.FindNextFile();  
  6.    cout << (LPCTSTR) finder.GetFileName() << endl;  
  7. }  


 

 31 判断文件或目录是否存在

 

[cpp]  view plain copy
  1. CString str=_T("d:\\1.txt");  
  2.   
  3. if (PathFileExists(str))// 如果文件或目录存在  
  4. {  
  5.     //.....  
  6. }  

 

如果文件存在 则删除文件:

 

[cpp]  view plain copy
  1. if(PathFileExists(szPicFilePath))  
  2.     CFile::Remove(szPicFilePath);   


 

 32 判断目录是否存在,若不存在则创建

 

使用 PathIsDirectory判断是否为目录时,需要添加头文件     #include "Shlwapi.h" 

 

[cpp]  view plain copy
  1. CString   strFolderPath="c:\\test"     
  2.   //   判断路径是否存在     
  3.   if   (!PathIsDirectory(m_strFolderPath)   )     
  4.   {     
  5.       CString   strMsg;     
  6.       strMsg.Format   ("指定路径\"%s\"不存在,是否创建?",   m_strFolderPath);     
  7.       if   (AfxMessageBox(strMsg,   MB_YESNO)   ==   IDYES)     
  8.       {     
  9.           if   (!CreateDirectory(m_strFolderPath,   NULL   )   )     
  10.           {     
  11.                 strMsg.Format   ("创建路径\"%s\"失败!是否继续?",   m_strFolderPath);     
  12.                 if   (AfxMessageBox(strMsg,   MB_YESNO)   ==   IDYES)     
  13.                       return;     
  14.             }     
  15.       }     
  16.   }    

 

 

简化:

[cpp]  view plain copy
  1. if (!PathIsDirectory(directoryPath))  
  2. {  
  3.     if (!CreateDirectory(directoryPath,NULL))  
  4.     {  
  5.         CString errorInfo;  
  6.         errorInfo.Format(L"无法创建目录 %s ",directoryPath);  
  7.         AfxMessageBox(errorInfo);  
  8.         return ;  
  9.     }  
  10. }  


 


 33 判断二者RGB颜色值是否相等

 

[cpp]  view plain copy
  1. <strong>ASSERT(GetRValue(RGB(255,0,0))==255)</strong>  

其它类似

分别为: 

 GetGValue(RGB())

GetBValue(RGB()) 

 

 

34 与或非&、 ~、 |  &&  ||    优先级小于判断运算符==   >   <等

 

也就是说  若要先&后再加以判断的话,必须用()将要&的左右两部分包括起来,然后再进行判断 ,否则出错

 

[cpp]  view plain copy
  1. int b=1;  
  2. int c=2;  
  3.   
  4. if (b&c==c)  
  5. {  
  6.     AfxMessageBox(L"==");  
  7. }  


按优先级顺序,此判断条件相当于

[cpp]  view plain copy
  1. if (b&(c==c))  

 

这与我们想要的计算顺序不符,所以需要添加() 改变计算顺序,正确的程序如下:

[cpp]  view plain copy
  1. int b=1;  
  2. int c=2;  
  3.   
  4. if ((b&c)==c)  
  5. {  
  6.     AfxMessageBox(L"==");  
  7. }  



 

 35  从字符串中读取格式化数据

 

[cpp]  view plain copy
  1. int nStart=0;  
  2. int nEnd=0;  
  3. CString  testString=_T("_100_135"):  
  4. _stscanf(testString,_T("_%d_%d"),&nStart,nEnd);  


 

 36 CString Format   ULONGLONG   无符号长整形所使用的格式

 

[cpp]  view plain copy
  1. ULONGLONG  longNum;  
  2. CString temp;  
  3. temp.Format(_T("%I64u"),longNum);  


 相互转换:

[cpp]  view plain copy
  1. ULONGLONG l;  
  2. CString str;  
  3. str.Format( _T("%I64u") , l );  
  4. _stscanf( (LPCTSTR)str,_T("%I64u") ,&l);  


 

 

 37 鼠标双击的最大时间间隔

[cpp]  view plain copy
  1. //双击的最大时间间隔可用下列函数获取或设置:(缺省为500毫秒)  
  2. UINT GetDoubleClickTime(VOID); // 返回间隔的毫秒数  
  3. BOOL SetDoubleClickTime(UINT uInterval); // 若成功返回非0值  
  4. //其中,uInterval为时间间隔值(毫秒数)。  


 

 

38 在Doc中 获取View的指针

 

[cpp]  view plain copy
  1. CView* CMyDoc::GetView(CRuntimeClass *pClass)  
  2. {  
  3.   
  4.     CView *pView;  
  5.     POSITION pos=GetFirstViewPosition();  
  6.   
  7.     while(pos!=NULL){  
  8.         pView=GetNextView(pos);  
  9.         if (pView->IsKindOf(pClass))  
  10.             break;  
  11.     }  
  12.   
  13.     if (!pView->IsKindOf(pClass))  
  14.     {  
  15.         AfxMessageBox(L"Cannot Locate the View");  
  16.         return NULL;  
  17.     }  
  18.   
  19.     return pView;  
  20. }  

 

使用如下:

[cpp]  view plain copy
  1. CMyView * pView=(CMyView*)GetView(RUNTIME_CLASS(CMyView));  


 39  将图片等比例缩放到视图中

 

        图片自动适应视图大小

 

[cpp]  view plain copy
  1. CString nPicPath=L"c:\\1.jpg";  
  2. CImage imagePic;  
  3. CRect clientRect;  
  4.   
  5. GetClientRect(&clientRect);  
  6.   
  7. if (!imagePic.IsNull())  
  8.     imagePic.Destroy();  
  9.   
  10.   
  11. // load the image  
  12.   
  13. HRESULT hResult=NULL;  
  14. hResult=imagePic.Load(nPicPath);  
  15.   
  16. if (hResult!=S_OK)  
  17. {  
  18.     CString str;  
  19.     str.Format(L"未能加载图片:");  
  20.     str+=nPicPath;  
  21.     AfxMessageBox(str);  
  22.     return;  
  23.   
  24. }  
  25.   
  26.   
  27. // get the rate  
  28.   
  29. double rateW=(double)clientRect.Width()/imagePic.GetWidth();  
  30. double rateH=(double)clientRect.Height()/imagePic.GetHeight();  
  31.   
  32. // the minmum value   
  33. double rate=rateW<rateH?rateW:rateH;  
  34.   
  35. // 原图片等比例适应到视图中  
  36. int showWidth=rate*imagePic.GetWidth();  
  37. int showHeight=rate*imagePic.GetHeight();  
  38.   
  39.   
  40.   
  41. //设置滚动范围  
  42. CSize sizeTotal;  
  43. sizeTotal.cx=showWidth;  
  44. sizeTotal.cy=showHeight;  
  45. SetScrollSizes(MM_TEXT, sizeTotal);  


 

 40  获得应用程序路径

 

[cpp]  view plain copy
  1. TCHAR szPath[255];  
  2. GetModuleFileName(NULL,szPath,255);  
  3. *(_tcsrchr(szPath,_T('\\'))+1) = 0; // 查找字符串中某个字符最后一次出现的位置  返回值为:最后一次在字符串中出现该字符的指针  
  4.   
  5. CString exePath=szPath; // exe文件路径  如: c:\file\1.exe  则exePath为 c:\file\  


 

  

 41 CString 转换成 int

 

[cpp]  view plain copy
  1. CString str;  
  2. int i=0;  
  3.   
  4. str.Format(L"%d",i);  //int --->CString  
  5.   
  6.   
  7.   
  8. // CString ---->int  
  9. // UNICODE 环境下 进行转换  使用_ttoi  
  10.   
  11. i=_ttoi(str);  
  12.   
  13. // 或使用_tsanf()  
  14.   
  15. _tscanf(str,L"%d",i);  
  16.   
  17. /* 
  18. i=atoi(str); //  在UNICODE环境下 不能使用 atoi 进行转换,因为 atoi 运行在ASCII环境下 
  19. */  


 

 42 计算文本串的宽高

This function computes the width and height of the specified string of text.

[cpp]  view plain copy
  1. SIZE sz;  
  2. GetTextExtentPoint(hDC,_T("ab"),2,&sz);  


 

转自:http://blog.csdn.net/shuilan0066/article/details/6638408

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值