文件操作
1、UNICODE文件开头标识:0Xfeff;
#define SETUNICODE(pFile) (fputwc(0xfeff, pFile))
2、TEXT("")功能符能转换代码值,而不仅仅表示宽字符,例如:
TCHAR tzBuf[126];
CHAR czBuf[126];
memset(tzBuf, 0, 126*sizeof(TCHAR));
memset(czBuf, 0, 126);
wsprintf(tzBuf, TEXT("阿三asga"));
sprintf(czBuf, "阿三asga");
或者:
TCHAR tzBuf[126] = TEXT("阿三asga");
CHAR czBuf[126] = "阿三asga";
结论:
地址tzBuf:963f,4e09,0061,0073,0067,0061,0000
地址czBuf:b0,a2,c8,fd,61,73,67,61,00
3、部分设备自带文本编辑器无法识别中文字符,无论是ANSI或者UNICODE格式;
4、读取和写入UNICODE文本时,需要使用"b"修饰符,如"rb"、"wb",不受fgets和fgetws的影响;
5、输出格式控制符说明:
Single-byte character with printf functions h c or C
Single-byte character with wprintf functions h c or C
Wide character with printf functions l c or C
Wide character with wprintf functions l c or C
Single-byte – character string with printf functions h s or S
Single-byte – character string with wprintf functions h s or S
Wide-character string with printf functions l s or S
Wide-character string with wprintf functions l s or S
易发生错误一:
sscanf(buf, TEXT(“%d”), var); //遗忘”&”
sscanf(buf, TEXT(“%d”), &var);
易发生错误二:
Template<class T> class mytem操作时,如果在头文件中仅仅声明class myclass,那么如下操作导致:
Mytem<myclass> m_Mytem;
可能出错。已遇上的错误:myclass中使用VirtualAlloc分配内存时,sizeof(myclass)的值不确定,导致分配内存失败。
关于Combobox的使用:
Combobox有Dropdown和Droplist两种Type,对于Droplist来说,OnSelchange()中使用UpdateData()可以获得用户选择的项,而对于Dropdown来说,OnSelchange()中必须先使用m_CtrlCombo.SetSel(m_CtrlCombo.GetSel()),然后使用UpdateData()才可以获取用户选择的项。
要善于使用SHE处理问题,如记录文件读取:
FILE* pFile = fopen(strFileName, “r”);
do
{
__try
{
fgets(czBuf, LINENUM, pFile);
//do your things here…
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
//continue or break?
}
}while(!feof(pFile));
fclose(pFile);
使用位图缓冲区绘图:
//定义位图:
CBitmap m_Bitmap;
//装载位图或者初始化位图:
//装载位图:
m_Bitmap.LoadBitmap(IDB_BITMAP1);
/****初始化位图**********
CClientDC ClientDC(this);
m_Bitmap.CreateCompatibleBitmap(&ClientDC, 32, 32);
//**********************/
//初始化内存设备句柄:
CClientDC ClientDC(this); //显示设备句柄
CDC MemDC;
MemDC.CreateCompatibleDC(&ClientDC);
//位图装入内存设备描述表:
MemDC.SelectObject(&m_Bitmap);
//使用内存设备操作位图:
MemDC.PatBlt(0, 0, 32, 32, WHITENESS);//刷墙
MemDC.Ellipse(2, 2, 30, 30);//画圆
//显示位图:OnDraw(CDC* pDC,…)
BITMAP BM;
m_Bitmap.GetObject(sizeof(BM), &BM);
pDC->BitBlt(
0,
0,
BM.bmWidth,
BM.bmHeight,
&MemDC,
0,
0,
SRCCOPY);
//其他位图操作函数:
//PatBlt(int x, int y, int nWidth, int nHeight, DWORD dwRop);
//StretchBlt(int x, int y, int nWidth, int nHeight, CDC* pSreDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop);
读取CF卡ID号:
#include <winioctl.h>
typedef struct _STORAGE_IDENTIFICATION {
DWORD dwSize;
DWORD dwFlags;
DWORD dwManufactureIDOffset;
DWORD dwSerialNumOffset;
} STORAGE_IDENTIFICATION, *PSTORAGE_IDENTIFICATION;
…
TCHAR tzCFCardID[22];
HANDLE hCard = CreateFile(TEXT("DSK1:"), //设备标识DISK1由查询注册表获得
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hCard)
{
int i = 0;
PSTORAGE_IDENTIFICATION pStoreInfo;
pStoreInfo=(STORAGE_IDENTIFICATION *)LocalAlloc(LMEM_ZEROINIT,3000);
DWORD dwGetBytes;
if (DeviceIoControl(hCard,
CTL_CODE(0x0007, 0x0709, METHOD_BUFFERED, FILE_ANY_ACCESS), // 关键行:指定IOCTL
NULL,
0,
(LPVOID)pStoreInfo,
3000,
&dwGetBytes,
NULL)
!=FALSE)
{
if(dwGetBytes > 0)
{
i = 0;
int j = 0;
if(pStoreInfo->dwSerialNumOffset < dwGetBytes)
{
BYTE* SerialNo=(((BYTE *)pStoreInfo)+pStoreInfo->dwSerialNumOffset);
while (SerialNo[i]!=0 && i<(int)(dwGetBytes-pStoreInfo->dwSerialNumOffset) && i<22)
{
if(SerialNo[i] < '0' || SerialNo[i] > 'z')
{
j++;
}
else
{
tzCFCardID[i - j]=(TCHAR)SerialNo[i];
}
i++;
}
}
tzCFCardID[i - j]=0;
}
}
else
{
wsprintf(tzError, TEXT("查询储存卡ID失败,错误号%lu!"), GetLastError());
AfxMessageBox(tzError);
LocalFree(pStoreInfo);
CloseHandle(hCard);
return FALSE;
}
LocalFree(pStoreInfo);
CloseHandle(hCard);
…
//说明:有些卡是没有卡号的,当初折磨了我很久:)
关键代码段运行时间测试
__int64 FileTimeToQuadWord(PFILETIME pft)
{
return (Int64ShllMod32(pft->dwHighDateTime, 32) | pft->dwLowDateTime);
}
union LTIME
{
int nTime[2];
__int64 lTime;
};
FILETIME ftKernelTimeStart, ftKernelTimeEnd;
FILETIME ftUserTimeStart, ftUserTimeEnd;
FILETIME ftDummy;
__int64 qwKernelTimeElapsed, qwUserTimeElapsed,
qwTotalTimeElapsed;
int nTimes;
LTIME KernelTime, UserTime, TotalTime;
void TimeStart()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy,
&ftKernelTimeStart, &ftUserTimeStart);
}
void TimeEnd()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy,
&ftKernelTimeEnd, &ftUserTimeEnd);
qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) -
FileTimeToQuadWord(&ftKernelTimeStart);
qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) -
FileTimeToQuadWord(&ftUserTimeStart);
/*
LARGE_INTEGER liStart, liEnd;
liStart.HighPart = ftUserTimeStart.dwHighDateTime;
liStart.LowPart = ftUserTimeStart.dwLowDateTime;
liEnd.HighPart = ftUserTimeEnd.dwHighDateTime;
liEnd.LowPart = ftUserTimeEnd.dwLowDateTime;
qwUserTimeElapsed = liEnd.QuadPart - liStart.QuadPart;
//*/
qwTotalTimeElapsed = qwKernelTimeElapsed + qwUserTimeElapsed;
nTimes++;
KernelTime.lTime = KernelTime.lTime + (qwKernelTimeElapsed - KernelTime.lTime)/nTimes;
UserTime.lTime = UserTime.lTime + (qwUserTimeElapsed - UserTime.lTime)/nTimes;
TotalTime.lTime = TotalTime.lTime + (qwTotalTimeElapsed - TotalTime.lTime)/nTimes;
}
void TimeReport()
{
FILE* pFile = fopen("TestTime.txt", "a+");
fprintf(pFile, "Test Description :\n");
fprintf(pFile, "Total Times:%d\n", nTimes);
fprintf(pFile, "Kernel Time Elapsed:%d, %d\n", KernelTime.nTime[1], KernelTime.nTime[0]);
fprintf(pFile, "User Time Elapsed:%d, %d\n", UserTime.nTime[1], UserTime.nTime[0]);
fprintf(pFile, "Total Time Elapsed:%d, %d\n\n", TotalTime.nTime[1], TotalTime.nTime[0]);
fclose(pFile);
}
void InitTestTime()
{
nTimes = 0;
KernelTime.lTime = 0;
UserTime.lTime = 0;
TotalTime.lTime = 0;
}
代码替换链接
#pragma comment( lib, "opengl32.lib" ) // 链接时使用OpenGL32.lib
列表操作
图标设置
m_AttTypeImageList.Create(IDB_ATTRIBUTETYPE, 16, 0, FALSE);
m_CtrlListPos.SetImageList(&m_AttTypeImageList, LVSIL_SMALL);
专栏操作
LV_COLUMN lvc;
lvc.mask = (LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM);
lvc.fmt = LVCFMT_LEFT;
TCHAR* Caption[6] = {TEXT("点名"),
TEXT("工程X"), TEXT("工程Y"),
TEXT("工程H"), TEXT("天线高"),
TEXT("描述")};
for(int i = 0; i<6; i++)
{
lvc.pszText = Caption[i];
if(i == 0) lvc.cx = 60;
else lvc.cx = 100;
lvc.iSubItem = i;
m_CtrlListPos.InsertColumn(i, &lvc);
}
记录操作
LV_ITEM lvi;
lvi.mask = LVIF_TEXT|LVIF_IMAGE;
lvi.pszText = pDPos->m_tzName;
lvi.iItem = i;
lvi.iSubItem = 0;
if(…)
lvi.iImage = 0;
else
lvi.iImage = 1;
m_CtrlListPos.SetItem(&lvi);
TCHAR cc[20];
_stprintf(cc, TEXT("%.3lf"), pDPos->m_x);
m_CtrlListPos.SetItemText(lvi.iItem, 1, cc);
_stprintf(cc, TEXT("%.3lf"), pDPos->m_y);
m_CtrlListPos.SetItemText(i, 2, cc);
_stprintf(cc, TEXT("%.3lf"), pDPos->m_z);
m_CtrlListPos.SetItemText(i, 3, cc);
_stprintf(cc, TEXT("%.3lf"), pDPos->m_dAntH);
m_CtrlListPos.SetItemText(i, 4, cc);
m_CtrlListPos.SetItemText(i, 5, pDPos->m_tzAttribute);