发现有些作用,所以转载过来.
在进行软件开发过程中间,有很多小功能的实现,虽然这些东西你可以不用,但是如果应用仂将会是你的程序更具有专业性。 一、设置程序自动运行。
很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能够启动运行,方法是写注册表Software\\Microsoft\\Windows\\CurrentVersion\\Run。参考程序可以见下:(查找程序目录的执行文件,存在则进行添加注册表操作)
HKEY RegKey; CString sPath; GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH); sPath.ReleaseBuffer(); int nPos; nPos=sPath.ReverseFind('\\'); sPath=sPath.Left(nPos); CString lpszFile=sPath+"\\****.exe";//这里加上你要查找的执行文件名称 CFileFind fFind; BOOL bSuccess; bSuccess=fFind.FindFile(lpszFile); fFind.Close(); if(bSuccess) { CString fullName; fullName=lpszFile; RegKey=NULL; RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&RegKey); RegSetValueEx(RegKey,"*****",0,REG_SZ,(const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());//这里加上你需要在注册表中注册的内容 this->UpdateData(FALSE); } else { theApp.SetMainSkin(); ::AfxMessageBox("没找到执行程序,自动运行失败"); exit(0); }
二、自动配置数据源
很多的程序都要用到数据库结合的操作,这里举例ACCESS,因为ACCESS在中小型VC系统开发中是最常用到的,如果程序的移植,如果对于很初级的用户来说,你还需要他到配置面板中进行数据源配置的话,那就有点说不过去了。
#include <odbcinst.h> //配置数据源,数据库在应用程序目录下,这里比如数据库文件名为***.mdb,程序运行时候可以将数据库文件拷贝到程序目录下面。 CString sPath; GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH); sPath.ReleaseBuffer(); int nPos; nPos=sPath.ReverseFind('\\'); sPath=sPath.Left(nPos); CString lpszFileName = sPath + "\\***.mdb";//这里修改成你的数据库文件名称 CFileFind fFind; if(!fFind.FindFile(lpszFileName)) { ::AfxMessageBox("没有找到数据库"); exit(0); } CString szDesc; szDesc.Format( "DSN=****;Description=****;DESCRIPTION=The DataBase For ***;FIL=MicrosoftAccess;DEFAULTDIR=%s;DBQ=%s;" ,sPath,lpszFileName);//这里***号可以添加成你的描述
//添加数据源 if(!::SQLConfigDataSource(NULL,ODBC_ADD_DSN, "Microsoft Access Driver (*.mdb)",(LPCSTR)szDesc)) { ::AfxMessageBox("32位ODBC数据源配置错误!"); exit(0); }
三、设置显示模式:
很多的程序的移植的运行环境是改变的。有可能你的原来开发环境是1024X768,但是到了那些显示器大于17的时候(分辨率超过你的开发时的分辨率时),程序的显示可能就不好看了。
DEVMODE lpDevMode; lpDevMode.dmPelsHeight=768;//Y方向象素点 lpDevMode.dmPelsWidth=1024;//X方向象素点 lpDevMode.dmDisplayFrequency=85;//屏幕刷新率 lpDevMode.dmFields=DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFREQUENCY; ChangeDisplaySettings(&lpDevMode,0);
四、在你的程序中间加载其他应用程序:
你的程序除了调用到各个模块进行协同工作外(DLL),还有可能调用不是同一个开发环境下的应用程序,比如VC环境下调用DELPHI,VB开发的执行程序,你就可以用到下面的方法(将调用的应用程序拷贝程序目录中):
CString sPath; GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH); sPath.ReleaseBuffer(); int nPos; nPos=sPath.ReverseFind('\\'); sPath=sPath.Left(nPos); CString lpszFileName = sPath + "\\***.exe";//这里修改成你的调用应用程序的文件名称 CFileFind fFind; if(!fFind.FindFile(lpszFileName)) { ::AfxMessageBox("没有找到调用的应用程序!"); return FALSE; } else ShellExecute(NULL,NULL,_T("***.exe"),NULL,sPath,NULL);
五、结束进程:
在你的程序中结束别的程序进程,采用的方法是进行进程列举,然后采用查找的方法进行:
#include "TLHELP32.H" DWORD ProcessID[50]; CString kkk[50]; HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  ROCESSENTRY32* info=new PROCESSENTRY32;  ROCESSENTRY32 ProcessInfo;//声明进程信息变量  rocessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小 //返回系统中第一个进程的信息 BOOL Status=Process32First(SnapShot,&rocessInfo); int m_nProcess=0; while(Status) { CString s,str1,str2; s.Format("%d",ProcessInfo.cntThreads); str1.Format("%s",ProcessInfo.szExeFile); str1=ProcessInfo.szExeFile; kkk[m_nProcess]=ProcessInfo.szExeFile;  rocessID[m_nProcess]=ProcessInfo.th32ProcessID; if(str1=="***.exe")//***.exe就是你要结束的进程的名称 { HANDLE ProcessHandle;  rocessHandle=OpenProcess (PROCESS_ALL_ACCESS,FALSE,ProcessID[m_nProcess]); TerminateProcess(ProcessHandle,0); } Status=Process32Next(SnapShot,&rocessInfo); m_nProcess++; }
出处:vchelp.net 责任编辑:leelee | [04-8-12 10:01] | 作者:戚高 |
posted @ 2006-12-06 14:33 ESHIDA 阅读(22) | 评论 (0) | 编辑 收藏 2006年11月30日 # 今天遇到一个两个CTime时间进行比较的问题 开始我把CTime 转成year,month,day,hour.... 进行比较, 后来发现用CTimeSpan可以很容易的比较.... CTime tmr1,tmr2; tmr1=... tmr2=//... CTimeSpan tmr3 = tmr2 - tmr1; LONGLONG longsum = tmr3.GetToalSecond(); if (longsum < 10 && longsum>-10) { //如果两个时间的秒数相差20秒之内.... } posted @ 2006-11-30 17:29 ESHIDA 阅读(67) | 评论 (0) | 编辑 收藏 2006年11月29日 # 2006年11月24日 # 以前做程序遇到的小问题 1. 将CString 转为WCHAR * CString str; WCHAR * p = str.AllocSysString() ; --------or - ----------- WCHAR *p = CT2W(str); 2. 转为LPCWSTR LPCWSTR strW= T2CW_EX( str, _ATL_SAFE_ALLOCA_DEF_THRESHOLD ); 3. 转为char * / LPSTR char *p = str.GetBuffer(str.GetLength()); 4. 转为int CString str = "23"; UINT uint; sscanf(str, "%d", uint); 5. 转为LPCTSTR LPCTSTR = str; //比较直接 6. 从string建立CString; CString str; string st; str = st.c_str () posted @ 2006-11-24 13:52 ESHIDA 阅读(238) | 评论 (4) | 编辑 收藏 CString timestr = "2000年04月05日"; int year,month,day; BYTE tt[5]; //get year memset(tt, 0, sizeof(tt)); tt[0] = timestr[0]; tt[1] = timestr[1]; tt[2] = timestr[2]; tt[3] = timestr[3]; year= atoi((char *)tt); //get month memset(tt, 0, sizeof(tt)); tt[0] = timestr[6]; tt[1] = timestr[7]; month = atoi((char *)tt); //get day memset(tt, 0, sizeof(tt)); tt[0] = timestr[10]; tt[1] = timestr[11]; CTime time(year,month,day,0,0,0); ---- or ----------- CString timestr = "2000年04月05日"; int a,b,c ; sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c); CTime time(a,b,c,0,0,0); --------or - --------------------- CString s("2001-8-29 19:06:23"); int nYear, nMonth, nDate, nHour, nMin, nSec; sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec); CTime t(nYear, nMonth, nDate, nHour, nMin, nSec); posted @ 2006-11-24 10:06 ESHIDA 阅读(32) | 评论 (0) | 编辑 收藏 |