vc小技巧20个 1. 打开cd-rom mcisendstring(set cdaudio door openwait,null,0,null); 2. 关闭cd_rom mcisendstring(set cdaudio door closedwait,null,0,null); 3. 关闭计算机 osversioninfo osversioninfo; // 包含操作系统版本信息的数据结构 osversionin
vc小技巧20个
1. 打开cd-rom
mcisendstring(set cdaudio door openwait,null,0,null);
2. 关闭cd_rom
mcisendstring(set cdaudio door closedwait,null,0,null);
3. 关闭计算机
osversioninfo osversioninfo; //包含操作系统版本信息的数据结构
osversioninfo.dwosversioninfosize = sizeof(osversioninfo);
getversionex(&osversioninfo); //获取操作系统版本信息
if(osversioninfo.dwplatformid ==ver_platform_win32_windows)
{
//windows98,调用exitwindowsex()函数重新启动计算机
dword dwreserved;
exitwindowsex(ewx_reboot,dwreserved);//可以改变第一个参数,实现注销用户、
//关机、关闭电源等操作
// 退出前的一些处理程序
}
4. 重启计算机
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);
}
5. 枚举所有字体
logfont lf;
lf.lfcharset = default_charset; // initialize the logfontstructure
strcpy(lf.lffacename,);
cclientdc dc (this);
//enumerate the font families
::enumfontfamiliesex((hdc) dc,&lf,
(fontenumproc) enumfontfamproc,(lparam) this,0);
//枚举函数
int callback enumfontfamproc(lpenumlogfontlpelf,lpnewtextmetric
lpntm,dword nfonttype,long lparam)
{
// create a pointer to the dialogwindow
cday7dlg* pwnd = (cday7dlg*) lparam;
// add the font name to the list box
pwnd->m_ctlfontlist.addstring(lpelf ->elflogfont.lffacename);
// return 1 to continue fontenumeration
return 1;
}
其中m_ctlfontlist是一个列表控件变量
6. 一次只运行一个程序实例,如果已运行则退出
if( findwindow(null,程序标题)) exit(0);
7. 得到当前鼠标所在位置
cpoint pt;
getcursorpos(&pt); //得到位置
8. 上下文菜单事件触发事件:
oncontextmenu事件
9. 显示和隐藏程序菜单
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();
}
10. 获取可执行文件的图标
hicon
hicon=::extracticon(afxgetinstancehandle(),_t(notepad.exe),0);
if (hicon &&hicon!=(hicon)-1)
{
pdc->drawicon(10,10,hicon);
}
destroyicon(hicon);
11. 窗口自动靠边程序演示
bool adjustpos(crect* lprect)
{
//自动靠边
intisx=getsystemmetrics(sm_cxfullscreen);
intisy=getsystemmetrics(sm_cyfullscreen);
rect rworkarea;
bool bresult =systemparametersinfo(spi_getworkarea,
sizeof(rect), &rworkarea, 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
{
//调整左
//pwnd->setwindowpos(null,rcwa.left,iy,0,0,swp_nosize);
lprect->offsetrect(rcwa.left-ix,0);
adjustpos(lprect);
return true;
}
if(iy
{
//调整上
//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;
12. 给系统菜单添加一个菜单项
给系统菜单添加一个菜单项需要进行下述三个步骤:
首先,使用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 theright range.
assert(idm_mysysitem
//get pointer to system menu.
cmenu* psysmenu=getsystemmenu(false);
assert_valid(psysmenu);
//add a separator and our menu item tosystem menu.
cstring strmenuitem(_t (new menuitem));
psysmenu->appendmenu(mf_separator);
psysmenu->appendmenu(mf_string,idm_mysysitem, strmenuitem);
…
}
13. 运行其它程序
//运行email或网址
char szmailaddress[80];
strcpy(szmailaddress,);
shellexecute(null, open, szmailaddress, null,null,
sw_shownormal);
//2、运行可执行程序
winexec(notepad.exe,sw_show); //运行记事本
14. 动态增加或删除菜单
(1) 增加菜单
//添加
cmenu *mainmenu;
mainmenu=afxgetmainwnd()->getmenu(); //得到主菜单
(mainmenu->getsubmenu (0))->appendmenu(mf_separator);//添加分隔符
(mainmenu->getsubmenu
(0))->appendmenu(mf_string,id_app_about,_t(alwayson &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;
}
}
15. 测试alt键是否按下:
getkeystate(vk_menu);
getalt();
16. 检查是否按下鼠标左键
if((nflags&mk_lbutton)==mk_lbutton)
17. 检查键盘输入
在onkeydown中的参数nchar是一个数值,当显示的时候,需要转换成字符,使用如下的命令:
char lschar;
lschar=char(nchar);
if(lschar=='a');
{
.......
}
18. 调用另一个函数::getkeystate(),用一个特定的键代码来确定法键是否被按下。如果::getkeystate函数的返回值是负的,表示该键被按下。如果返回值是非负的,表示该留未被按下。例如:如果要确定shift键是否被按下,可以使用下面的代码:
if(::getkeystate(vk_shift)
{
afxmessagebox(shift ispressed);
}
19. 如何在编程的过程中随时结束应用程序(常规)
1)需要向窗口发送 wm_close/wm_quit消息,
调用 cwnd::onclose成员函数并允许对用户提示是否保存修改过的数据.
afxgetmainwnd()->sendmessage(wm_close); //别忘了先得到当前窗口的指针
2)使用函数: void postquitmessage( int nexitcode // exit code );
3)使用标准函数:void exit( int status ); //尽量不要在mfc中使用
20. 得到屏幕的尺寸大小
hwnd hwnd;
crect rect;
hwnd = ::getdesktopwindow();
::getclientrect(hwnd, &rect);
//---------------------------------------------------------
如何查询和设置系统参数
在windows 3.1sdk中介绍过sdk函数systemparametersinfo,调用该函数可以查询和设置系统参数,诸如按键的重复速率设置、鼠标双击延迟时间、图标字体以及桌面覆盖位图等等。
//create a font that is used for icon titles.
logfont stfont; :: systemparametersinfo(spif_geticontitlelogfont,
sizeof (logfont), &stfont,spif_sendwininichange);
m_font.createfontindirect (&stfont); //change thewallpaper to leaves.bmp.
:: systemparametersinfo (spi_setdeskwallpaper, 0,
_t(forest.bmp),spif_updateinifile);
//---------------------------------------------------------
如何使用一个预定义的windows光标?调用cwinapp:: loadstandardcursor并传送光标标识符。
bool csampledialog:: onsetcursor(cwnd* pwnd, uint nhittest,
uint message) { //display wait cursor if busy.
if (m_bbusy) {
setcursor (afxgetapp () ->loadstandardcursor(idc_wait));
return true; }
return cdialog:: onsetcursor (pwnd. nhittest,message); }