//1、运行EMAIL或网址
char szMailAddress[80];
strcpy(szMailAddress,"mailto:netvc@21cn.com");
ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL);
//2、运行可执行程序
WinExec("notepad.exe",SW_SHOW); //运行计事本
-
lpCmdLine
-
[in] Pointer to a null-terminated character string that contains the command line (file name plus optional parameters) for the application to be executed. If the name of the executable file in the
lpCmdLine parameter does not contain a directory path, the system searches for the executable file in this sequence:
- The directory from which the application loaded.
- The current directory.
- The Windows system directory. The tabindex="0" keywords="base.getsystemdirectory" errorurl="../common/badjump.htm"/>GetSystemDirectory function retrieves the path of this directory.
- The Windows directory. The tabindex="0" keywords="base.getwindowsdirectory" errorurl="../common/badjump.htm"/>GetWindowsDirectory function retrieves the path of this directory.
- The directories listed in the PATH environment variable.
uCmdShow
- [in] For a list of the acceptable values, see the description of the nCmdShow parameter of the tabindex="0" keywords="_win32_showwindow" errorurl="../common/badjump.htm"/> ShowWindow function.
2 OnSize()用法
void CBAView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if ((cx > 0) && (cy > 0))
{
CRect rect;
if (m_treeDevice.m_hWnd != NULL)
{
m_treeDevice.GetWindowRect(rect);
ScreenToClient(rect);
m_treeDevice.MoveWindow(rect.left, rect.top,cx / 4, cy);
}
}
}
3. 一次只运行一个程序实例,如果已运行则退出
if( FindWindow(NULL,"程序标题")) exit(0);
// 只允许应用程序的一个实例运行另一个专业方法( QssReadBot )
DWORD dwError = 0 ;
//会产生一个错误ERROR_ALREADY_EXISTS
HANDLE hMutex = CreateMutex(NULL, TRUE, TEXT("QssReadBot"));
if ( hMutex == NULL )
{
MessageBox( NULL, "Create Mutex error!", NULL, MB_OK);
return FALSE ;
}
else
{
dwError = GetLastError();
if ( ERROR_ALREADY_EXISTS == dwError )//会产生一个错误ERROR_ALREADY_EXISTS
{
MessageBox( NULL, "程序已运行!", "提示", MB_OK);
return FALSE ;
}
}
4.给树添加imagelist
//装入位图
CImageList *pImageList;
CBitmap bitmap;
UINT nID;
pImageList = new CImageList();
pImageList->Create(20, 20, ILC_COLOR32, 0, 4);
for (nID = IDB_BITMAP1; nID <= IDB_BITMAP15; nID++) // load bitmaps for dog, bird and fish
{
bitmap.LoadBitmap(nID);
pImageList->Add(&bitmap, RGB(255,192,255));
bitmap.DeleteObject();
}
m_treeDevice.SetImageList(pImageList, TVSIL_NORMAL);
//然后添加树控件的item,图片为0,1,2,3等
5.在控件上显示jpeg图片de函数
ShowPicture(CDC *pDC, CString strPath, CRect *rect)//显示jpg文件的函数
{
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
//打开文件并检测文件的有效性
if (file.Open(strPath,CFile::modeRead)&&
file.GetStatus(strPath,fstatus)&&
((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
pvData = GlobalLock(hGlobal);
if (pvData != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
}
}
else
{
return false;
} //打开文件结束
//显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
//要显示动画GIF请使用ACTIVE控//件。
IPicture *pPic;
//load image from file stream
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,FALSE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//在rect上拉伸显示图片
if(FAILED(pPic->Render(*pDC,rect->left ,rect->top ,rect->right,rect->bottom ,0,
hmHeight,hmWidth,-hmHeight,NULL)))
{
pPic->Release();
return false;
}
pPic->Release();
}
else
{
return false;
}
return true;
}
如下应用:
CRect rect;
// sPath=_T("C://Documents and Settings//All Users//Documents//My Pictures//示例图片//Sunset.jpg");
m_static1.GetClientRect(rect); //m_static1为控件
ShowPicture(pDC,_T("res//体育馆.jpg"),&rect );
,_T("res//体育馆.jpg")可以用sPath替换,显示固定位置的图片
,_T("res//体育馆.jpg"),有可能要写成,_T("..//res//体育馆.jpg")?
6树控件的控制作用
void CBAView::OnSelchangedTreeDevice(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
//if(m_treeDevice.GetSelectedItem() == hFan[1])
MessageBox(m_treeDevice.GetItemText(m_treeDevice.GetSelectedItem()),NULL,MB_OK);
*pResult = 0;
}
通过全局变量节点的handle来对树的选择进行反映
7使用ado组件
首先把 #import "C:/Program Files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
加入到stdafx.h中
然后在主.cpp文件 中添加
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
并在onnitial()中初始化com组件
::CoInitialize(NULL);//初始化com库环境
应用时初始化指针
m_pConnection.CreateInstance(_uuidof(Connection));//初始化指针
m_pRecordset.CreateInstance(_uuidof(Recordset));
然后打开m_pConnection
m_pConnection->Open("driver={SQL server};server=192.168.1.158;UID=sa;PWD=sa;database=AnyView","","",NULL);
使用
// m_pRecordset->Open("select * from Device",(IDispatch *)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
或者
m_pRecordset = m_pConnection->Execute("SELECT COUNT(DISTINCT BuildingNO) FROM Device",&RecordsAffected,adCmdText);
得到记录
在应用程序结束时
void CBAView::OnDestroy()
{
CFormView::OnDestroy();
::CoUninitialize(); //释放程序占用的com资源
// TODO: Add your message handler code here
}
8.sql语句的连接
A.char tempStr[10];
itoa(i+1,tempStr,10);
_bstr_t sqlStr;
sqlStr = "SELECT COUNT(DISTINCT FloorNO) FROM Device ";
sqlStr = sqlStr + "WHERE BuildingNO =";
sqlStr = sqlStr + tempStr;
m_pRecordset = m_pConnection->Execute(sqlStr ,&RecordsAffected,adCmdText);
_variant_t vCountF = m_pRecordset->GetCollect((_variant_t)((long)0));
floors = vCountF.lVal;
B.
SqlStr="delete from "+this->m_CurTblName+" where "+FldName+"='"+value+"'";
Sqlstr="Insert into "+TblName+" ("+Fldlist+") values("+ValueList+")";
9。取得系统时间及格式
char szYear[5], szMonth[5], szDay[5], szHour[5], szMinute[5], szSecond[5], szTemp[5] ;
CString strDateTime ;
CString strAirConditonID ;
CString strAirConSwitch_ID, strLX_datetime ;
CString strRunStatus, strWD, strSNWD, strPZFKD, strYXFS, strEquipeID ;
CString strErrorCode ;
CString strSql ;
CString strDBConnection ;
CString strQssNumber, strBh, strName ;
/// 取得当前系统本地时间
SYSTEMTIME stSystemDateTime ;
memset( &stSystemDateTime, 0, sizeof(SYSTEMTIME)) ;
GetLocalTime( & stSystemDateTime ) ;
itoa( stSystemDateTime.wYear, szYear, 10 ) ; // 年
if ( stSystemDateTime.wMonth < 10 ) // 月份
{
itoa( stSystemDateTime.wMonth, szTemp, 10 ) ;
strcpy( szMonth, "0" ) ;
strcat( szMonth, szTemp ) ;
}
else
{
itoa( stSystemDateTime.wMonth, szMonth, 10 ) ;
}
if ( stSystemDateTime.wDay < 10 ) // 日
{
itoa( stSystemDateTime.wDay, szTemp, 10 ) ;
strcpy( szDay, "0" ) ;
strcat( szDay, szTemp ) ;
}
else
{
itoa( stSystemDateTime.wDay, szDay, 10 ) ;
}
if ( stSystemDateTime.wHour < 10 ) // 小时
{
itoa( stSystemDateTime.wHour, szTemp, 10 ) ;
strcpy( szHour, "0" ) ;
strcat( szHour, szTemp ) ;
}
else
{
itoa( stSystemDateTime.wHour, szHour, 10 ) ;
}
if ( stSystemDateTime.wMinute < 10 ) // 分钟
{
itoa( stSystemDateTime.wMinute, szTemp, 10 ) ;
strcpy( szMinute, "0" ) ;
strcat( szMinute, szTemp ) ;
}
else
{
itoa( stSystemDateTime.wMinute, szMinute, 10 ) ;
}
if ( stSystemDateTime.wSecond < 10 ) // 秒
{
itoa( stSystemDateTime.wSecond, szSecond, 10 ) ;
strcpy( szSecond, "0" ) ;
strcat( szSecond, szTemp ) ;
}
else
{
itoa( stSystemDateTime.wSecond, szSecond, 10 ) ;
}
strDateTime = szYear ;
strDateTime += "-" ;
strDateTime += szMonth ;
strDateTime += "-";
strDateTime += szDay ;
strDateTime += " " ;
strDateTime += szHour ;
strDateTime += ":" ;
strDateTime += szMinute ;
strDateTime += ":" ;
strDateTime += szSecond ;
该博客提供了多个编程实用代码示例,包括运行EMAIL或网址、可执行程序,OnSize()用法,限制程序单实例运行,给树添加imagelist,在控件上显示jpeg图片,树控件控制,使用ado组件,sql语句连接以及获取系统时间及格式等内容。

被折叠的 条评论
为什么被折叠?



