vc常用代碼2
……21,程序只允許一個實例運行
//在這個位置調用FirstInstance函數?
BOOL CWindowsApp::InitInstance()?
{?
if (!FirstInstance())?
return FALSE; //已經有實例存在了,退出?
AfxEnableControlContainer();?
}?
//FirstInstance函數?
BOOL FirstInstance()?
{?
CWnd *pWndPrev;?
//根據主窗口類名和主窗口名判斷是否已經有實例存在了?
if (pWndPrev = CWnd::FindWindow("#32770","Windows秘書"))//第二個參數為程序名?
{
//如果存在就將其激活,并顯示出來?
pWndPrev->ShowWindow(SW_RESTORE);?
pWndPrev->SetForegroundWindow();?
return FALSE;?
}else{
return TRUE;?
}?
}
………22,取得系統(tǒng)時間及計算時間差
DWORD system_runtime;?
system_runtime=GetTickCount()/60000;//取得系統(tǒng)運行時間?
SYSTEMTIME sysTime; // Win32 time information?
GetLocalTime(&sysTime);?
COleDateTime now(sysTime);?
//COleDateTime now;?
//now=now.GetCurrentTime();?
//GetCurrentTime()取得時間與time( time_t *timer );和struct tm *localtime( const time_t *timer );所得時間相同只能到2037年?
//而GetLocalTime(&sysTime);可到9999年?
m_zbyear=now.GetYear();//年?
m_zbmonth=now.GetMonth();//月?
m_zbday=now.GetDay();//日?
now.GetDayOfWeek();//weekday?
COleDateTimeSpan sub;?
sub.SetDateTimeSpan(m_howdaysafteris,0,0,0);//設定時間差為天數?
now+=sub;?
then.SetDateTime(,,,,,,,);//其它方式添加時保存的當時時間?
if(!now.SetDate(m_jsyear,m_jsmonth,m_jsday)//檢查時間是否正確 &&!then.SetDate(m_jsyear2,m_jsmonth2,m_jsday2))?
{?
sub=then-now;?
CString cs;?
cs.Format("%d年%d月%d日 相距 %d年%d月%d日/n/n %.0f天",m_jsyear,m_jsmonth,m_jsday,?
m_jsyear2,m_jsmonth2,m_jsday2,sub.GetTotalDays());?
MessageBox(cs);?
}?
else?
MessageBox("您輸入的時間有誤,請重新輸入!","Windows秘書",MB_OK|MB_ICONSTOP);?
………23,文件操作
//打開文件對話框?
CFileDialog dlg(TRUE, "mp3", NULL, OFN_FILEMUSTEXIST|OFN_NOCHANGEDIR,?
"音樂/電影文件(*.mp3,*.wav,*.avi,*.asf)|*.mp3;*.wav;*.avi;*.asf|"/?
"mp3 文件(*.mp3)|*.mp3|" /?
"音頻文件 (*.wav)|*.wav|" /?
"視頻文件 (*.avi)|*.avi|" /?
"Window Media 文件(*.asf)|*.asf|" /?
"所有文件 (*.*)|*.*||");?
if(dlg.DoModal()==IDOK)?
{?
CString m_filename=dlg.GetPathName();?
}?
//讀取文件?
CFile file;?
If (file.Open(m_filename,CFile::modeRead))?
{?
file.Read(zbpassword,sizeof(zbpassword));?
}?
file.Close();?
//寫入文件?
if (file.Open(m_zbfilename,CFile::modeCreate|CFile::modeWrite))?
{?
zbfile.Write(zbpassword,sizeof(zbpassword));?
}?
file.Close();?
………24,進制轉換
CString BinaryToDecimal(CString strBinary)//轉換二進制為十進制?
{?
int nLenth = strBinary.GetLength();?
char* Binary = new char[nLenth];?
Binary = strBinary.GetBuffer(0);?
int nDecimal = 0;?
for(int i=0;i<nLenth;i++)?
{?
char h = Binary[nLenth-1-i];?
char str[1];?
str[0] = h;?
int j = atoi(str);?
for(int k=0;k<i;k++) j=j*2;??
nDecimal += j;?
}?
CString strDecimal;?
strDecimal.Format("%d",nDecimal);?
return strDecimal;?
}?
CString BinaryToHex(CString strBinary)//轉換二進制為十六進制?
{?
int nLength = strBinary.GetLength();?
CString str = strBinary;?
//位數不是四的倍數時補齊?
switch(nLength%4)?
{?
case 0:?
break;?
case 1:?
strBinary.Format("%d%d%d%s",0,0,0,str);?
break;?
case 2:?
strBinary.Format("%d%d%s",0,0,str);?
break;?
case 3:?
strBinary.Format("%d%s",0,str);?
break;?
default:?
return "";?
break;?
}?
CString strHex,str1;?
str1 = "";?
nLength = strBinary.GetLength();?
for(int i=1;i<=(nLength/4);i++)?
{?
//每四位二進制數轉換為一十六進制數?
str = strBinary.Left(4);?
CString strDecimal = BinaryToDecimal(str);?
int nDecimal = atoi(strDecimal.GetBuffer(0));?
if (nDecimal<10)?
str1.Format("%d",nDecimal);?
else?
{?
char c = 'A' + (nDecimal-10);?
str1.Format("%c",c);?
}?
strHex += str1;?
strBinary = strBinary.Right(strBinary.GetLength()-str.GetLength());?
}?
return strHex;?
}?
unsigned char BtoH(char ch)//將16進制的一個字符轉換為十進制的數?
{?
//0-9?
if (ch >= '0' && ch <= '9')?
return (ch - '0');?
//9-15?
if (ch >= 'A' && ch <= 'F')?
return (ch - 'A' + 0xA);?
//9-15?
if (ch >= 'a' && ch <= 'f')?
return (ch - 'a' + 0xA);?
return(255);?
}?
CString DecimalToBinary(CString strDecimal)//轉換十進制為二進制?
{?
int nDecimal = atoi(strDecimal.GetBuffer(0));?
int nYushu; //余數?
int nShang; //商?
CString strBinary = "";?
char buff[2];?
CString str = "";?
BOOL bContinue = TRUE;?
while(bContinue)?
{?
nYushu = nDecimal%2;?
nShang = nDecimal/2;?
sprintf(buff,"%d",nYushu);?
str = strBinary;?
strBinary.Format("%s%s",buff,str);?
nDecimal = nShang;?
if(nShang==0)?
bContinue = FALSE;?
}?
return strBinary;?
}?
CString BinaryToDecimal(CString strBinary)//轉換二進制為十進制?
{?
int nLenth = strBinary.GetLength();?
char* Binary = new char[nLenth];?
Binary = strBinary.GetBuffer(0);?
int nDecimal = 0;?
for(int i=0;i<nLenth;i++)?
{?
char h = Binary[nLenth-1-i];?
char str[1];?
str[0] = h;?
int j = atoi(str);?
for(int k=0;k<i;k++) j=j*2;??
nDecimal += j;?
}?
CString strDecimal;?
strDecimal.Format("%d",nDecimal);?
return strDecimal;?
}?
………25,數學函數
sin();?
cos();?
tan();?
sqrt();?
pow(x,y);?
log();?
log10();?
………26,遞歸法遍歷磁盤目錄
void CQt::BrowseDir(CString strDir)?
{?
CFileFind ff;?
CString str,cs,inifile;?
inifile=regfilepath;?
inifile+="http://Windows秘書.ini";?
CString szDir = strDir;?
int index;?
char jjj,ppp,ggg;?
if(szDir.Right(1) != "http://")?
szDir += "http://";?
szDir += "*.*";?
BOOL res = ff.FindFile(szDir);?
while(res)?
{?
res = ff.FindNextFile();?
if(ff.IsDirectory() && !ff.IsDots())?
{?
//如果是一個子目錄,用遞歸繼續(xù)往深一層找?
BrowseDir(ff.GetFilePath());?
}?
else if(!ff.IsDirectory() && !ff.IsDots())?
{?
//顯示當前訪問的文件?
str=ff.GetFilePath();?
/* index=str.GetLength();?
//MessageBox(str);?
jjj=str.GetAt(index-3);?
ppp=str.GetAt(index-2);?
ggg=str.GetAt(index-1);?
if(((jjj=='J'||jjj=='j')&&(ppp=='P'||ppp=='p')&&(ggg=='G'||ggg=='g'))||((jjj=='B'||jjj=='b')&&(ppp=='M'||ppp=='m')&&(ggg=='P'||ggg=='p')))
{?
intNumber++;?
cs.Format("%d",intNumber);?
WritePrivateProfileString("圖片目錄文件",cs,str,inifile);*/?
//Sleep(3);?
}?
}?
}?
ff.Close();//關閉?
//cs.Format("%d",intNumber);?
//WritePrivateProfileString("圖片目錄文件","total",cs,inifile);?
//WritePrivateProfileString("圖片目錄文件","turntowhich","1",inifile);?
}?
//公用目錄對話框****************************************************?
//添加如下程序段?
LPITEMIDLIST pidlBeginAt, pidlDestination ;?
char szPath[ MAX_PATH] ;?
// 取得開始菜單或桌面的PIDL?
SHGetSpecialFolderLocation(AfxGetApp()->m_pMainWnd->m_hWnd,CSIDL_DESKTOP,&pidlBeginAt) ;?
// 取得新建文件夾的父文件夾?
if( !BrowseForFolder(pidlBeginAt ,?
&pidlDestination,?
"請選擇圖片目錄的位置:"))?
{?
return ;?
}?
// 把PIDL轉換為路徑名?
SHGetPathFromIDList( pidlDestination, szPath);?
//添加如下函數?
BOOL BrowseForFolder(?
LPITEMIDLIST pidlRoot,//瀏覽開始處的PIDL?
LPITEMIDLIST *ppidlDestination,?
//瀏覽結束時所選擇的PIDL?
LPCSTR lpszTitle)//瀏覽對話框中的提示文字?
{?
BROWSEINFO BrInfo ;?
ZeroMemory( &BrInfo, sizeof(BrInfo)) ;?
BrInfo.hwndOwner = HWND_DESKTOP ;?
BrInfo.pidlRoot = pidlRoot ;?
BrInfo.lpszTitle = lpszTitle ;?
//瀏覽文件夾?
*ppidlDestination= SHBrowseForFolder(&BrInfo);?
//用戶選擇了取消按鈕?
if(NULL == *ppidlDestination)?
return FALSE ;/**/?
return TRUE ;?
}?
//讀寫INI/ini文件*************************************************?
//寫入值 字段名 變量名 值 帶目錄文件名?
WritePrivateProfileString("目錄","path",cs, regpath);?
//寫入結構體 字段名 變量名 值 大小 帶目錄文件名?
WritePrivateProfileStruct("字體","font",&LF,sizeof(LOGFONT),regpath);//結構體?
//讀入字符 字段名 變量名 默認值 字符緩沖區(qū) 長度 帶目錄文件名?
GetPrivateProfileString("目錄","path","", buffer.GetBuffer(260),260,regpath);?
//讀入整數值 字段名 變量名 默認值 帶目錄文件名?
GetPrivateProfileInt("colors","red",255, regpath);?
//讀入結構體 字段名 變量名 值 大小 帶目錄文件名?
GetPrivateProfileStruct("字體","font",&LF,sizeof(LOGFONT),regpath);?
//位圖操作,畫圖*****************************************************?
CClientDC client(this);?
BITMAP bmpInfo;?
CDC memdc;?
CBitmap picture;?
memdc.CreateCompatibleDC(pdc);?
memdc.SelectObject(&picture);?
CRect re;?
GetClientRect(&re);?
client.BitBlt(0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,&memdc,0,0,SRCCOPY);?
client.SetBkMode(TRANSPARENT);?
client.SetTextColor(RGB(red,green,blue));?
CFont font;?
CFont *oldfont;?
font.CreateFontIndirect(&LF);?
oldfont=client.SelectObject(&font);?
client.DrawText("",//注意這個字符串里如果只有一連串的字母或數字,沒有空格或中文或標點符號,且總長度超過距形寬度,則不能自動換行!!?
&re,DT_CENTER |DT_WORDBREAK);?
client.SelectObject(oldfont);?
//打開EXE/exe文件**********************************************?
ShellExecute(GetSafeHwnd(),?
"open",?
"http://home.jlu.edu.cn/~ygsoft",//改這個文件名就可以了?
NULL,?
NULL,SW_SHOWNORMAL);?
//or?
LPITEMIDLIST pidlBeginAt;?
// 取得桌面的PIDL?
SHGetSpecialFolderLocation(AfxGetApp()->m_pMainWnd->m_hWnd,?
CSIDL_DRIVES//這個標志為我的電腦?
,&pidlBeginAt) ;?
SHELLEXECUTEINFO exe;?
ZeroMemory(&exe,sizeof(SHELLEXECUTEINFO));?
exe.cbSize=sizeof(SHELLEXECUTEINFO);?
exe.fMask=SEE_MASK_IDLIST;?
exe.lpVerb="open";?
exe.nShow=SW_SHOWNORMAL;?
exe.lpIDList=pidlBeginAt;?
ShellExecuteEx(&exe);?
//取得函數不能正常返回的原因字符***************************?
LPVOID lpMsgBuf;?
FormatMessage(?
FORMAT_MESSAGE_ALLOCATE_BUFFER |?
FORMAT_MESSAGE_FROM_SYSTEM |?
FORMAT_MESSAGE_IGNORE_InsertS,?
NULL,?
GetLastError(),?
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language?
(LPTSTR) &lpMsgBuf,?
0,?
NULL?
);?
// Process any inserts in lpMsgBuf.?
// ...?
// Display the string.?
MessageBox((LPCTSTR)lpMsgBuf);?
// Free the buffer.?
LocalFree( lpMsgBuf );?