//一、打开CD-ROM
mciSendString(“Set cdAudio door open wait”,NULL,0,NULL);
//二、关闭CD_ROM
mciSendString(“Set cdAudio door closed wait”,NULL,0,NULL);
//三、关闭计算机
OSVERSIONINFO OsVersionInfo; //包含操作系统版本信息的数据结构
OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVersionInfo); //获取操作系统版本信息
if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
//Windows98,调用ExitWindowsEx()函数重新启动计算机
DWORD dwReserved;
ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改变第一个参数,实现注销用户、
//关机、关闭电源等操作
// 退出前的一些处理程序
}
//四、重启计算机
typedef int (CALLBACK *SHUTDOWNDLG)(int); //显示关机对话框函数的指针
HINSTANCE hInst = LoadLibrary(“shell32.dll”); //装入shell32.dll
SHUTDOWNDLG ShutDownDialog; //指向shell32.dll库中显示关机对话框函数的指针
if(hInst != NULL)
{
//获得函数的地址并调用之
ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);
(*ShutDownDialog)(0);
}
//五、枚举所有字体
LOGFONT lf;
lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structure
strcpy(lf.lfFaceName,”");
CClientDC dc (this);
// Enumerate the font families
::EnumFontFamiliesEx((HDC) dc,&lf, (FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0);
//枚举函数
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,
LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam)
{
// Create a pointer to the dialog window
CDay7Dlg* pWnd = (CDay7Dlg*) lparam;
// add the font name to the list box
pWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName);
// Return 1 to continue font enumeration
return 1;
}
//其中m_ctlFontList是一个列表控件变量
//六、一次只运行一个程序实例,如果已运行则退出
if( FindWindow(NULL,”程序标题”)) exit(0);
//七、得到当前鼠标所在位置
CPoint pt;
GetCursorPos(&pt); //得到位置
//八、上下文菜单事件触发事件:OnContextMenu事件
//九、显示和隐藏程序菜单
CWnd *pWnd=AfxGetMainWnd();
if(b_m) //隐藏菜单
{
pWnd->SetMenu(NULL);
pWnd->DrawMenuBar();
b_m=false;
}
else
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME); 显示菜单 也可改变菜单项
pWnd->SetMenu(&menu);
pWnd->DrawMenuBar();
b_m=true;
menu.Detach();
}
//十、获取可执行文件的图标
HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T(“NotePad.exe”),0);
if (hIcon &&hIcon!=(HICON)-1)
{
pDC->DrawIcon(10,10,hIcon);
}
DestroyIcon(hIcon);
//十一、窗口自动靠边程序演示
BOOL AdjustPos(CRect* lpRect)
{//自动靠边
int iSX=GetSystemMetrics(SM_CXFULLSCREEN);
int iSY=GetSystemMetrics(SM_CYFULLSCREEN);
RECT rWorkArea;
BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkAre
a, 0);
CRect rcWA;
if(!bResult)
{//如果调用不成功就利用GetSystemMetrics获取屏幕面积
rcWA=CRect(0,0,iSX,iSY);
}
else
rcWA=rWorkArea;
int iX=lpRect->left;
int iY=lpRect->top;
if(iX < rcWA.left + DETASTEP && iX!=rcWA.left)
{//调整左
//pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.left-iX,0);
AdjustPos(lpRect);
return TRUE;
}
if(iY < rcWA.top + DETASTEP && iY!=rcWA.top)
{//调整上
//pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.top-iY);
AdjustPos(lpRect);
return TRUE;
}
if(iX + lpRect->Width() > rcWA.right – DETASTEP && iX !=rcWA.right-lpRect->Width())
{//调整右
//pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.right-lpRect->right,0);
AdjustPos(lpRect);
return TRUE;
}
if(iY + lpRect->Height() > rcWA.bottom – DETASTEP && iY !=rcWA.bottom-lpRect
->Height())
{//调整下
//pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
return TRUE;
}
return FALSE;
}
//然后在ONMOVEING事件中使用所下过程调用
CRect r=*pRect;
AdjustPos(&r);
*pRect=(RECT)r;
//十二、给系统菜单添加一个菜单项给系统菜单添加一个菜单项需要进行下述三个步骤:
//首先,使用Resource Symbols对话(在View菜单中选择Resource Symbols...可以显
//示该对话)定义菜单项ID,该ID应大于0x0F而小于0xF000;
//其次,调用CWnd::GetSystemMenu获取系统菜单的指针并调用CWnd:: Appendmenu将菜单
//项添加到菜单中。下例给系统菜单添加两个新的
int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct)
{
//…
//Make sure system menu item is in the right range.
ASSERT(IDM_MYSYSITEM<0xF000);
//Get pointer to system menu.
CMenu* pSysMenu=GetSystemMenu(FALSE);
ASSERT_VALID(pSysMenu);
//Add a separator and our menu item to system menu.
CString StrMenuItem(_T ("New menu item"));
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_MYSYSITEM, StrMenuItem);
//…
}
//十三、运行其它程序
//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); //运行计事本
//十四、动态增加或删除菜单
//1、 增加菜单
//添加
CMenu *mainmenu;
mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
(mainmenu->GetSubMenu (0))->AppendMenu (MF_SEPARATOR);//添加分隔符
(mainmenu->GetSubMenu (0))->AppendMenu(MF_STRING,ID_APP_ABOUT,
_T(“Always on &Top”)); //添加新的菜单项
DrawMenuBar(); //重画菜单
//2、 删除菜单
//删除
CMenu *mainmenu;
mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
CString str ;
for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i–) //取得菜单的项数。
{
(mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION);
//将指定菜单项的标签拷贝到指定的缓冲区。MF_BYPOSITION的解释见上。
if(str==”Always on &Top”) //如果是刚才我们增加的菜单项,则删除。
{
(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION);
break;
}
}
//十五、改变应用程序的图标静态更改:
//修改图标资源IDR_MAINFRAME。它有两个图标,一个是16*16的,另一个是32*32的,注意要一起修改。
//动态更改: 向主窗口发送WM_SETICON消息.代码如下:
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON);
ASSERT(hIcon);
AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);
//十六、另一种改变窗口标题的方法
//使用语句 CWnd* m_pCWnd = AfxGetMainWnd( ),然后,再以如下形式调用SetWindowText()函数:
SetWindowText( *m_pCWnd,(LPCTSTR)m_WindowText);// m_WindowText可以是一个CString类的变量。
//十七、剪切板上通过增强元文件拷贝图像数据下面代码拷贝通过元文件拷贝图像数据到任何应用程序,
//其可以放置在CView派生类的函数中。
CMetaFileDC * m_pMetaDC = new CMetaFileDC();
m_pMetaDC->CreateEnhanced(GetDC(),NULL,NULL,”whatever”);
//draw meta file
//do what ever you want to do: bitmaps, lines, text…
//close meta file dc and prepare for clipboard;
HENHMETAFILE hMF = m_pMetaDC->CloseEnhanced();
//copy to clipboard
OpenClipboard();
EmptyClipboard();
::SetClipboardData(CF_ENHMETAFILE,hMF);
CloseClipboard();
//DeleteMetaFile(hMF);
delete m_pMetaDC;
//十八、剪切板上文本数据的传送把文本放置到剪接板上:
CString source;
//put your text in source
if(OpenClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
//从剪接板上获取文本:
char * buffer;
if(OpenClipboard())
{
buffer = (char*)GetClipboardData(CF_TEXT);
//do something with buffer here
//before it goes out of scope
}
CloseClipboard();
//十九、将捕捉屏幕图像到剪切版中
void CShowBmpInDlgDlg::OnCutScreen()
{
ShowWindow(SW_HIDE);
RECT r_bmp={0,0,::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN)};
HBITMAP hBitmap;
hBitmap=CopyScreenToBitmap(&r_bmp);
//hWnd为程序窗口句柄
if (OpenClipboard())
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
}
ShowWindow(SW_SHOW);
}
HBITMAP CShowBmpInDlgDlg::CopyScreenToBitmap(LPRECT lpRect)
{
//lpRect 代表选定区域
{
HDC hScrDC, hMemDC;
// 屏幕和内存设备描述表
HBITMAP hBitmap, hOldBitmap;
// 位图句柄
int nX, nY, nX2, nY2;
// 选定区域坐标
int nWidth, nHeight;
// 位图宽度和高度
int xScrn, yScrn;
// 屏幕分辨率
// 确保选定区域不为空矩形
if (IsRectEmpty(lpRect))
return NULL;
//为屏幕创建设备描述表
hScrDC = CreateDC(“DISPLAY”, NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 获得选定区域坐标
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
// 获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//确保选定区域是可见的
if (nX<0)
nX = 0;
if (nY<0)
nY = 0;
if (nX2>xScrn)
nX2 = xScrn;
if (nY2>yScrn)
nY2 = yScrn;
nWidth = nX2 – nX;
nHeight = nY2 – nY;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap
(hScrDC, nWidth, nHeight);
// 把新位图选到内存设备描述表中
hOldBitmap =(HBITMAP)SelectObject(hMemDC, hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
//得到屏幕位图的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// 返回位图句柄
return hBitmap;
}
}
//二十、如何将位图缩放显示在Static控件中
//在Staic控件内显示位图
void CShowBmpInDlgDlg::ShowBmpInStaic()
{
CBitmap hbmp;
HBITMAP hbitmap;
//将pStatic指向要显示的地方
CStatic *pStaic;
pStaic=(CStatic*)GetDlgItem(IDC_IMAGE);
//装载资源 MM.bmp是我的一个文件名,用你的替换
hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),”MM.bmp”,
IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
hbmp.Attach(hbitmap);
//获取图片格式
BITMAP bm;
hbmp.GetBitmap(&bm);
CDC dcMem;
dcMem.CreateCompatibleDC(GetDC());
CBitmap *poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp);
CRect lRect;
pStaic->GetClientRect(&lRect);
//显示位图
pStaic->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(),
&dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
dcMem.SelectObject(&poldBitmap);
}
103 views 一月 11, 05 by Timothy
主要是为了实现像木马那样的生成自己需要配置的EXE文件。首先要自己写一个EXE,然后以资源的方式导入到工程中。
下边的例子是一个修改MessageBox标题和文字的程序.
HRSRC hResInfo;
HGLOBAL hResData;
DWORD dwSize, dwWritten;
LPBYTE pBuffer;
HANDLE hFile;
TCHAR szMsgBoxTitle[100],szMsgBoxText[100];
szMsgBoxTitle[0] =’\0′;
szMsgBoxText[0] = ‘\0′;
得到新数据,比如新的邮箱地址和密码
GetDlgItemText(IDC_EDIT_TITLE,szMsgBoxTitle,100);
GetDlgItemText(IDC_EDIT_TEXT,szMsgBoxText,100);
// 查找所需的资源
hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDR_DUMMY_EXE), “Dummy_Exe”);
if (hResInfo == NULL)
{
::MessageBox(NULL, “查找资源失败!”, “错误”, MB_OK | MB_ICONINFORMATION);
return;
}
// 获得资源尺寸
dwSize = SizeofResource(NULL, hResInfo);
// 装载资源
hResData = LoadResource(NULL, hResInfo);
if (hResData == NULL)
{
::MessageBox(NULL, “装载资源失败!”, “错误”, MB_OK | MB_ICONINFORMATION);
return;
}
// 为数据分配空间
pBuffer = (LPBYTE)GlobalAlloc(GPTR, dwSize);
if (pBuffer == NULL)
{
::MessageBox(NULL, “分配内存失败!”, “错误”, MB_OK | MB_ICONINFORMATION);
return;
}
// 复制资源数据
CopyMemory((LPVOID)pBuffer, (LPCVOID)LockResource(hResData), dwSize);
// 获取标题和文本,并复制数据,具体偏移地址见你的那个EXE资源
CopyMemory((LPVOID)(pBuffer + 0×400), (LPCVOID)szMsgBoxTitle, 100);
CopyMemory((LPVOID)(pBuffer + 0×464), (LPCVOID)szMsgBoxText, 100);
// 创建文件,写数据
hFile = CreateFile(“C:\\EnochShen.Exe”, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (hFile != NULL)
{
WriteFile(hFile, (LPCVOID)pBuffer, dwSize, &dwWritten, NULL);
}
else
{
::MessageBox(NULL, “创建文件失败!”, “错误”, MB_OK | MB_ICONINFORMATION);
GlobalFree((HGLOBAL)pBuffer);
return;
}
// 收尾工作,释放资源
CloseHandle(hFile);
GlobalFree((HGLOBAL)pBuffer);
::MessageBox(NULL, “创建文件C:\\cooldog.Exe!”, “成功” ,MB_OK | MB_ICONINFORMATION);
ShellExecute(m_hWnd, “open”, “C:\\”, NULL, NULL, SW_SHOWNORMAL);
下面是那个作为资源的EXE的源代码(MASM32写的):
; —————————————————————–
; 生成的exe只有1k,小巧玲珑,^O^
; —————————————————————–
; #########################################################################
.386
.model flat, stdcall
option casemap :none ; case sensitive
; #########################################################################
include c:\masm32\include\windows.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\kernel32.inc
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib
; #########################################################################
.data
szMsgBoxTitle db 100 dup(‘E’)
szMsgBoxText db 100 dup(‘S’)
.code
start:
; —————————————————————–
; 下面的代码生成的Exe只有1k,但是打包会出错
;
; szMsgBoxTitle db 100 dup(‘E’)
; szMsgBoxText db 100 dup(‘S’)
;
; —————————————————————–
invoke MessageBox,NULL,ADDR szMsgBoxText,ADDR szMsgBoxTitle,MB_OK or MB_ICONINFORMATION
invoke ExitProcess,NULL
end start
参考自CSDN开发高手第N期(具体哪一期我忘了)
184 views 一月 05, 05 by Timothy
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
.cpp中在函数中执行
//建立ado连接
HRESULT hr;
hr=m_pConnection.CreateInstance(__uuidof(Connection));
try
{
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open(_bstr_t(L”Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Viper;Data Source=Viper”),_bstr_t (L”sa”),_bstr_t (L”"),adModeUnknown);
}
}
catch(_com_error & err)
{
AfxMessageBox(err.Description(),MB_OK,0);
AfxMessageBox(err.ErrorMessage(),MB_OK,0);
AfxMessageBox(“无法连接SQL SERVER 服务器,程序将退出。请检查网络设备”,MB_OK,0);
exit(0);
}
//执行储存过程
CString cvar1,cvar2;
int cvar3;
cvar1=”ddd”;
cvar2=”";
cvar3=0;
try
{
m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection=app->m_pConnection;
m_pCommand->CommandType=adCmdStoredProc;
m_pCommand->CommandText=_bstr_t(“pr_zs_dzdy”);
_variant_t vvar1,vvar2,vvar3;
vvar1=_variant_t(_bstr_t(cvar1));
vvar2=_variant_t(_bstr_t(cvar2));
vvar3=_variant_t(cvar3);
_ParameterPtr mp_var1,mp_var2,mp_var3;
mp_var1.CreateInstance(__uuidof(Parameter));
mp_var2.CreateInstance(__uuidof(Parameter));
mp_var3.CreateInstance(__uuidof(Parameter));
mp_var1=m_pCommand->CreateParameter
(
_bstr_t(“var1″),
adVarChar,
adParamInput,
3,
vvar1
);
m_pCommand->Parameters->Append(mp_var1);
mp_var2=m_pCommand->CreateParameter
(
_bstr_t(“var2″),
adVarChar,
adParamOutput,
3,
vvar2
);
m_pCommand->Parameters->Append(mp_var2);
mp_var3=m_pCommand->CreateParameter
(
_bstr_t(“var3″),
adIntger,
adParamOutput,
9,
vvar3
);
m_pCommand->Parameters->Append(mp_var3);
_variant_t vNull;
vNull.vt=VT_ERROR;
vNull.scode=DISP_E_PARAMNOTFOUND;
m_pCommand->Execute(&vNull,&vNull,adCmdStoredProc);
cvar2=mp_var2->Value.bstrVal;
cvar3=mp_var3->Value;
}
catch(_com_error &error)
{
MessageBox(error.ErrorMessage(),”ADO错误!”);
MessageBox(error.Description(),”ADO错误!”);
}
108 views 一月 05, 05 by Timothy
酒店管理系统V1.0
这是我个人完成的一个酒店管理系统模型,拥有客户预订,客户登记,空房查询,服务消费记录和客户结帐,房间管理等多种功能。采用VC++6.0和SQL SERVER开发,大部分业务逻辑采用存储过程实现提高了数据存储的可靠性和速度。
程序界面预览:(点击查看全图)
客户预订界面
客户登记界面
空房查询界面
点击下载此文件
292 views 一月 04, 05 by Timothy
1.ACCESS 2000
_ConnectionPtr m_pConn;
CString m_sConn=”Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb”;
m_pConn.CreateInstance(“ADODB.Connection”);
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,”",”",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox(“不能连接数据库 source!”);
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox(“不能连接数据库 error!”);
return FALSE;
}
2.SQL Server 2000
_ConnectionPtr m_pConn;
CString m_sConn=”Provider=SQLOLEDB.1;Data Source=192.168.3.9;Initial
Catalog=sode”; //sode是数据库服务器192.168.3.9上的一个数据库
m_pConn.CreateInstance(“ADODB.Connection”);
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,”sa”,”mapper”,adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox(“不能连接数据库 source!”);
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox(“不能连接数据库 error!”);
return FALSE;
}
3.Oracle 9i
_ConnectionPtr m_pConn;
CString m_sConn=”Provider=MSDAORA.1;Data Source=sode_192.168.3.9″; //使用
ms连接库,sode为SID,192.168.3.9为机器ip
m_pConn.CreateInstance(“ADODB.Connection”);
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,”sodeUser”,”sodePw”,adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox(“不能连接数据库 source!”);
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox(“不能打开数据库 error!”);
return FALSE;
}