数据转换:
CString –> double : double dNum = atof(str);
double –> CString : CString cStr = (TCHAR*)(_bstr_t)dNum;
#include<comutil.h> comsuppwd.lib
CString –> std::string : std::string = (char*)(_bstr_t(cStr));
std::string –> CString: CString cStr = (TCHAR*)(_bstr_t(str.c_str()));
CString –> char*: LPTSTR p = cStr.GetBuffer();
string –> int :
- #include <sstream>
- #include <string>
- std::string text = "152";
- int number;
- std::stringstream ss;
- ss < < text;//可以是其他数据类型
- ss >> number; //string -> int
- ss < < number;// int->string
- string str = ss.str();
字符串转换:http://blog.csdn.net/jinhill/article/details/4025968
CString操作指南:http://blog.csdn.net/wilsonwong/article/details/1329420
---------------------------------------------------------------------------------------------------------------------------
数据格式化:
CString cFormatStr;
cFormatStr.Format(“%0.2f”,fOrg); //格式化为2位小数
时间日期:CTime cTime;
cTime = CTime::GetCurrentTime();
CString cTimeStr = cTime.Format(“%Y-%m-%d”);
- TCHAR szMsg[MAX_PATH];
- //将格式化的字符串写入szMsg
- StringCchPrintf(szMsg,_countof(szMsg),TEXT("%d is a number!"),number);
- AfxMessageBox(szMsg);
---------------------------------------------------------------------------------------------------------------------------
控件使用技巧:
1、静态文本响应事件:修改文本ID,Notify属性选为true.
2、静态文本显示位图:
HBITMAP hBmp = (HBITMAP)::LoadImage(0,imgPath,IMAGE_BITMAP,0,0,LP_LOADFROMFILE);
m_img.SetBitmap(hBmp);
3、Button禁用或者启用:CWnd类的EnableWindow(BOOL)方法。TRUE为启用。
5、VC从文件中加载图片:http://blog.csdn.net/jinhill/article/details/4251551
---------------------------------------------------------------------------------------------------------------------------
对话框技巧:
1、设置对话框背景色:
方法1:设置单个对话框的背景色:
- BOOL CShowImgDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_brush.CreateSolidBrush(RGB(255,255,255));//设置白色背景
- return TRUE;
- }
- HBRUSH CShowImgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- return m_brush;
- }
1、ClientToScreen 函数:这个函数的功能:该函数将指定点的用户坐标转换成屏幕坐标。
函数原型:BOOL ClientToScreen(HWND hWnd,LPPOINT lpPoint);
CWnd::ClientToScreen(LPPOINT lpPoint) (ClientToScreen(LPRECT lpRect))
实现的是同样的效果,只不过是窗口句柄缺省为调用此函数的窗口。
2、ScreenToClient 函数:函数功能:该函数把屏幕上指定点的屏幕坐标转换成用户坐标。
函数原型:BOOL ScreenToClient(HWND hWnd, LPPOINT lpPoint);
3、CWnd::GetClientRect(CRect rect) 函数:此函数得到的区域是相对于调用此函数的窗口指针所指向的窗口的客户区域,坐标点是相对于此窗口而言的。
4、CWnd::GetWindowRect(rect) 函数:此函数得到的区域坐标是相对于屏幕左上角而言
---------------------------------------------------------------------------------------------------------------------------
文件操作遍历文件夹下所有的文件:
- CFileFind ff;
- BOOL bRes = ff.FindFile(m_currentPathStr+TEXT("\\images\\*.bmp"));
- while (bRes)
- {
- bRes = ff.FindNextFile();
- if(!ff.IsDirectory() && !ff.IsDots())//如果是文件
- {
- //.....
- }
- }
- void SearchFile(CString strDir){
- CFileFind ff;
- CString szDir = strDir;//文件目录
- if(szDir.Right(1)!=TEXT("\\")){
- szDir += TEXT("\\");
- }
- szDir += TEXT("*.*");
- BOOL bRes = ff.FindFile(szDir);
- while(bRes){
- bRes = ff.FindNextFile();
- if(!ff.IsDirectory() && !ff.IsDots()){//是文件
- }else if(ff.IsDirectory() && !ff.IsDots()){//目录
- SearchFile(ff.GetFilePath());//递归调用
- }
- }
- ff.Close();
- }
创建文件,读写文件1
- CStdioFile stdFile(TEXT("file.conf"),CStdioFile::modeCreate|CStdioFile::modeWrite) ;
- if(!stdFile){
- AfxMessageBox(TEXT("创建配置文件失败!"));
- return;
- }
- for(vector<CString>::iterator iter =configFileVec.begin();iter!=configFileVec.end();iter++ ){
- stdFile.WriteString(*iter);
- }
- stdFile.Close();
---------------------------------------------------------------------------------------------------------------------------
1、手动添加响应WM_HOTKEY消息
.h文件中:afx_msg LONG OnHotKey(WPARAM wParam,LPARAM lParam);
.cpp文件中:
- BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
- ON_WM_CREATE()
- //.....
- ON_MESSAGE(WM_HOTKEY,OnHotKey)
- END_MESSAGE_MAP()
响应函数:
- LONG CMainWnd::OnHotKey(WPARAM wParam,LPARAM lParam)
- {
- UINT fuModifiers = (UINT) LOWORD(lParam); // key-modifier flags
- UINT uVirtKey = (UINT) HIWORD(lParam); // virtual-key code
- //判断响应了什么热键
- if( MOD_CONTROL|MOD_ALT == fuModifiers && 'T' == uVirtKey )
- {
- AfxMessageBox(_T("你按下了组合键 ctrl + alt+T"));
- }
- return 0;
- }
2.在init()或者OnCreate()中注册:
- //Register热键
- def _DEBUG //debug版本
- int nRet = RegisterHotKey(GetSafeHwnd(),ID_HOTKEY_CTRL_ALT_T,MOD_CONTROL|MOD_ALT,'T'); //热键 ctrl +alt+T
- if(!nRet)
- AfxMessageBox(_T("RegisterHotKey Ctrl+Alt+T false"));
- se //release版本
- RegisterHotKey(GetSafeHwnd(),ID_HOTKEY_CTRL_ALT_T,MOD_CONTROL|MOD_ALT,'T'); //热键 ctrl +alt+T
- dif
3、用完后在合适的时机注销快捷键:
- #ifdef _DEBUG
- int nRet = UnregisterHotKey(GetSafeHwnd(), ID_HOTKEY_CTRL_ALT_T);
- if(!nRet)
- AfxMessageBox(_T("UnregisterHotKey Ctrl+Alt+T false"));
- #else
- UnregisterHotKey(GetSafeHwnd(), ID_HOTKEY_CTRL_ALT_T);
- #endif
---------------------------------------------------------------------------------------------------------------------------
文本编程:
1、创建文本插入符:void CreateSolidCaret(int nWidth, int nHeight);
设置插入符的位置: static void PASCAL CWnd::SetCaretPos(POINT point);
2、获得当前设备描述表的字体的度量信息:
BOOL CDC::GetTextMetrics( LPTEXTMETRIC lpMetrics ) const;
获得某个特定字符串在窗口中显式时所占据的宽度和高度:CSize CDC::GetTextExtent( const CString& str ) const;
3、路径层:在路径层内绘制,不会覆盖该区域原有的内容BOOL CDC::BeginPath();
BOOL CDC::EndPath();
4、设置字体
- CFont font;
- font.CreatePointFont(300,"华文行楷",NULL);
- CFont *pOldFont = dc.SelectObject(&font);
---------------------------------------------------------------------------------------------------------------------------
窗体控制:
1、获得窗体的大小和位置:void CWnd::GetWindowRect(LPRECT lpRect);
2、设置窗体位置和大小:
BOOL SetWindowPos( const CWnd* pWndInsertAfter, int x, //左上角x坐标 int y, //左上角y坐标 int cx, //窗体宽度 int cy, //窗体高度 UINT nFlags //控制窗体:如SWP_DRAWFRAME:在窗体周围绘制一个边框 );3、修改窗体的过程函数,使用:LONG ::SetWindowLong(HWND hWnd,int nIndex,LONG dwNewLong);
WNDPROC prevProc = (WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT2)->m_hWnd,GWL_WNDPROC,(LONG)NewWndProc);4、获得窗口句柄的方法:
- HWND ::GetNextWindow(HWND hWnd,UINT wCmd);
- HWND ::GetWindow(HWND hWnd,UINT uCmd);
- HWND ::GetNextDlgTabItem(HWND hDlg,HWND hCtrl,BOOL bPrevious);
5、按回车使得焦点在对话框上控件之间依次传递:
并在[格式]—>[tab键顺序]中调整Tab顺序。
- void COrderInfoDlg::OnOK()
- {
- GetNextDlgGroupItem(GetFocus())->SetFocus();
- //CDialog::OnOK();
- }
------------------------------------------------------------------------------------------------------------------
菜单
1、菜单命令路由:视图类-->文档类-->框架类-->应用程序类
2、菜单的有关函数:
获得菜单: CMenu* CWnd::GetMenu()const;
获得菜单的子菜单: CMenu* CMenu::GetSubMenu(int nPos);
标记菜单: UINT CMenu::CheckMenuItem(UINT nIDCheckItem,UNIT nCheck);
图形标记菜单: BOOL CMenu::SetMenuItemBitmaps(UNIT nPosition,UNTI nFlags,…);
禁用菜单: UNIT CMenu::EnableMenuItem(UNIT nIDEnableItem,UNIT nEnable); (…,MF_BYPOSITION|MF_DISABLE|MF_GRAYD)且m_bAutoMenuEnable = FALSE
移除,设置菜单: BOOL CWnd::SetMenu(CMenu* pMenu); SetMenu(NULL)(移除菜单);
---------------------------------------------------------------------------------------------------------------
绘图:
1、OnPaint与OnDraw:
OnPaint是CWnd的成员函数. OnDraw是CView的成员函数,OnPaint是WM_PAINT消息引发的重绘消息处理函数,在OnPaint中会调用OnDraw来进行绘图。
----------------------------------------------------------------------------------------------------------
截图:
- /************************************************************************/
- /*将图像截取为HBITMAP
- */
- /************************************************************************/
- static HBITMAP CopyScreenToBitmap(LPRECT lpRect)
- {
- //创建屏幕设备描述表
- HDC hScrDC;
- //创建内存设备描述表
- HDC hMemDC;
- //位图句柄
- HBITMAP hBitmap, hOldBitmap;
- int nX, nY, nX2, nY2;
- // 选定区域坐标
- int nWidth, nHeight;
- // 确保选定区域不为空矩形
- if (IsRectEmpty(lpRect))
- return NULL;
- //为屏幕创建设备描述表
- hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
- //为屏幕设备描述表创建兼容的内存设备描述表
- hMemDC = CreateCompatibleDC(hScrDC);
- // 获得选定区域坐标
- nX = lpRect->left;
- nY = lpRect->top;
- nX2 = lpRect->right;
- nY2 = lpRect->bottom;
- //确保选定区域是可见的
- if (nX < 0)
- nX = 0;
- if (nY < 0)
- nY = 0;
- /*
- if (nX2 > m_xScreen)
- nX2 = m_xScreen;
- if (nY2 > m_yScreen)
- nY2 = m_yScreen;*/
- //获得位图的宽度和长度
- nWidth = nX2 - nX;
- nHeight = nY2 - nY;
- // 创建一个与屏幕设备描述表兼容的位图
- hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
- // 把新位图选到内存设备描述表中
- hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
- // 把屏幕设备描述表拷贝到内存设备描述表中
- BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY);
- hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
- //得到屏幕位图的句柄
- //清除
- DeleteDC(hScrDC);
- DeleteDC(hMemDC);
- // 返回位图句柄
- if (OpenClipboard(theApp.m_pMainWnd->m_hWnd)) {
- EmptyClipboard();//清空剪贴板
- //把屏幕内容粘贴到剪贴板上,
- SetClipboardData(CF_BITMAP, hBitmap);
- //关闭剪贴板
- CloseClipboard();
- }
- return hBitmap;
- }
- static BOOL SaveBmp(HBITMAP hBitmap,LPTSTR lpFileName)
- {
- HDC hDC;
- //设备描述表
- int iBits;
- //当前显示分辨率下每个像素所占bit位数
- WORD wBitCount;
- //位图中每个像素所占bit位数
- //定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
- DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
- BITMAP Bitmap;
- //位图属性结构
- BITMAPFILEHEADER bmfHdr;
- //位图文件头结构
- BITMAPINFOHEADER bi;
- //位图信息头结构
- LPBITMAPINFOHEADER lpbi;
- //指向位图信息头结构
- HANDLE fh, hDib, hPal;
- HPALETTE hOldPal=NULL;
- //定义文件,分配内存句柄,调色板句柄
- //计算位图文件每个像素所占bit位数
- hDC = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
- iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
- DeleteDC(hDC);
- if (iBits <= 1)
- wBitCount = 1;
- else if (iBits <= 4)
- wBitCount = 4;
- else if (iBits <= 8)
- wBitCount = 8;
- else if (iBits <= 24)
- wBitCount = 24;
- else
- wBitCount = 32;
- //计算调色板大小
- if (wBitCount <= 8)
- dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);
- //设置位图信息头结构
- GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = Bitmap.bmWidth;
- bi.biHeight = Bitmap.bmHeight;
- bi.biPlanes = 1;
- bi.biBitCount = wBitCount;
- bi.biCompression = BI_RGB;
- bi.biSizeImage = 0;
- bi.biXPelsPerMeter = 0;
- bi.biYPelsPerMeter = 0;
- bi.biClrUsed = 0;
- bi.biClrImportant = 0;
- dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
- //为位图内容分配内存
- hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
- *lpbi = bi;
- // 处理调色板
- hPal = GetStockObject(DEFAULT_PALETTE);
- if (hPal)
- {
- hDC = ::GetDC(NULL);
- hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
- RealizePalette(hDC);
- }
- // 获取该调色板下新的像素值
- if(0==GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize,
- (BITMAPINFO *)&bi,DIB_RGB_COLORS))
- {
- //Quit();
- PostQuitMessage(0);
- return FALSE;
- }
- //恢复调色板
- if (hOldPal)
- {
- SelectPalette(hDC, hOldPal, TRUE);
- RealizePalette(hDC);
- ::ReleaseDC(NULL, hDC);
- }
- //创建位图文件
- fh=CreateFile(lpFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
- if (fh==INVALID_HANDLE_VALUE)
- return FALSE;
- // 设置位图文件头
- bmfHdr.bfType = 0x4D42; // "BM"
- dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
- bmfHdr.bfSize = dwDIBSize;
- bmfHdr.bfReserved1 = 0;
- bmfHdr.bfReserved2 = 0;
- bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
- // 写入位图文件头
- WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
- // 写入位图文件其余内容
- WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
- //清除
- GlobalUnlock(hDib);
- GlobalFree(hDib);
- CloseHandle(fh);
- return TRUE;
- }
使用:
- RECT rect;
- rect.left = theApp.m_wndPos.x;//看自己的需要了
- rect.top = theApp.m_wndPos.y;
- rect.right = rect.left+ theApp.m_wndSize.cx;
- rect.bottom = rect.top + theApp.m_wndSize.cy;
- HBITMAP hBitmap = CopyScreenToBitmap(&rect);
- SaveBmp(hBitmap,strPath.GetBuffer());//strPath:保存的路径
----------------------------------------------------------------------------------------------------------------
消息
Windows消息分类:
- 标准消息:出WM_COMMAND以外的所有以WM_开头的消息
- 命令消息:WM_COMMAND:在MFC中以ID区分,在SDK中以wParam参数识别。派生自CCmdTarget的类都可接受这样的消息。
- 通告消息:控件产生的消息,为了向父窗体通知事件发生。WM_COMMAND
派生自CCmdTarget的类只能接收命令消息和通告消息。派生自CWnd的类可接受所有消息。
MFC下自定义消息:ON_MESSAGE
1、定义消息ID:#define WM_ADDINPUTINFO 10000
2、定义响应函数:
3、响应函数:
- virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
4、加到MESSAGE_MAP:
- LRESULT COutputWnd::OnAddInputInfo(WPARAM wParam,LPARAM lParam){
- AfxMessageBox(TEXT("ssss"));
- return 0;
- }
- BEGIN_MESSAGE_MAP(COutputWnd, CDockablePane)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_MESSAGE(WM_ADDINPUTINFO, &COutputWnd::OnAddInputInfo)
- END_MESSAGE_MAP()
深度解析VC中的消息:http://blog.csdn.net/jinhill/article/details/5084990
--------------------------------------------------------------------------------------------------------------------如何在控制台下使用VC的部分类或方法:
1、#include<afx.h>
2、工程属性 ->配置属性->常规->MFC的使用:在静态库中使用MFC
转自:http://blog.csdn.net/tjxin_xd/article/details/6581140