1 为对话框添加菜单,。变量须设置为全局
//先创建一个Command_Bar
HWND hwndCB = CommandBar_Create (AfxGetInstanceHandle(), m_hWnd, 1);
///再插入菜单:
CommandBar_InsertMenubar (hwndCB, AfxGetInstanceHandle(),IDR_MENU1, 0);
2 int与string的相互转化int to string :
int index=0;
CString str;
str.Format(_T("%d"),index);// str 为 0
str.Format(_T("%02d"),index); // str 为 00
string to int :
CString str = _T("00");
int index;
swscanf(str,_T("%d"),&index);
Cstring to char *
CString str = _T("CString");
char *pBuffer = NULL;
pBuffer = new char[str.GetLength()+1];
ZeroMemory(pBuffer, str.GetLength()+1);
int iLen = str.GetLength();
for(int i = 0; i < iLen; i++)
pBuffer[i] = str[i];
pBuffer[i] ='/0';
pDC->ExtTextOut(0, 0, 0, NULL, pBuffer, NULL);
delete[] pBuffer;
------------------
eVC++4.0上已经测试通过
上述方法针对中文就有问题,根源在 Unicode 上。类似下面的代码更好:
#include <atlconv.h>
USES_CONVERSION;
CString str="Cstring";
char *p;
p=(char*)W2A((LPCTSTR)str);
3.EVC实现WIN CE下截屏并且保存到文件
// this function create a bmp file to save the current screen;
// supported eVC++ 4.0 (wince 4.0) and vc++ 6.0 , test pass;
void CSnapDlg::OnScreenSave(const char *filename)
{
HDC hScrDC, hMemDC;
int width, height;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
//creates a memory device context (DC) compatible with the screen device(hScrDC)
hMemDC = CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
// on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
//write into file
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, 3*width*height, 1, mStream);
//close file
fclose(mStream);
}
//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
}
5 还回 系统时间
long Milliseconds=GetTickCount();
CString log;
SYSTEMTIME CurTime;
GetLocalTime(&CurTime);
log.Format(_T("%d-%d-%d,%d:%d:%d,"),
CurTime.wYear, CurTime.wMonth, CurTime.wDay,
CurTime.wHour, CurTime.wMinute, CurTime.wSecond );
6 读文本
CFileDialog cfd(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("文本文件(*.txt)|*.txt|所有文 件 (*.*)|*.*||"));
if(cfd.DoModal()==IDCANCEL)
return;
CFile cf;
CFileException cfe;
//TCHAR *tc;
char *tc;
if(cf.Open(cfd.GetPathName(),CFile::modeRead,&cfe)==FALSE)
{
AfxMessageBox("不能读取文件:"+cfd.GetPathName());
return;
}
tc=new char[cf.GetLength()];
cf.Read(tc,cf.GetLength());
CString str1=tc;
delete [] tc;
cf.Close();
AfxMessageBox(str1);
7.char *_gcvt( double value, int digits, char *buffer )函数使用问题
功能: 将浮点型数据转换为字符串
digits不能超过16,否则计算精度将出问题.
例如:
double preNum=9.7;
double nextNum=1;
double dblResult=preNum+nextNum;
CString dispText;
dispText=_gcvt(dblResult,20,charBuffer);
理论上dispText为10.7,但实际结果为10.6999999999999999
如果将最后一句改为:
dispText=_gcvt(dblResult,16,charBuffer);
则结果正常.
由此推测,digits参数应该在16以下