vc開發小技巧20個
一、開啟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)
五、列舉所有字型
logfont lf;
lf.lfcharset = default_charset; // initialize the logfont structure
strcpy(lf.lffacename,"");
cclientdc dc (this);
// enumerate the font families
::enumfontfamilie***((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) //隱藏選單
else
十、獲取可執行檔案的圖示
hicon hicon=::extracticon(afxgetinstancehandle(),_t("notepad.exe"),0);
if (hicon &&hicon!=(hicon)-1)
pdc->drawicon(10,10,hicon);
destroyicon(hicon);
十一、視窗自動靠邊程式演示
bool adjustpos(crect* lprect)
; 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);
lrect.normalizerect();
//顯示位圖
pstaic->getdc()->stretchblt(lrect.left ,lrect.top ,lrect.width(),lrect.height(),
&dcmem,0 ,0,bm.bmwidth,bm.bmheight,srccopy);
dcmem.selectobject(&poldbitmap);
}
20個VC開發小技巧
一 開啟cd rom mcisendstring set cdaudio door open wait null,0,null 二 關閉cd rom mcisendstring set cdaudio door closed wait null,0,null 三 關閉計算機 osversioninf...
vc 20個小技巧
一 開啟cd rom mcisendstring set cdaudio door open wait null,0,null 二 關閉cd rom mcisendstring set cdaudio door closed wait null,0,null 三 關閉計算機 osversioninf...
VC小技巧15個
vc小技巧15個 一 一次只執行乙個程式例項 二 裝載游標 三 獲得主框架 獲得應用程式主視窗的指標 example afxgetmainwnd showwindow sw showmaxmized 使程式最大化 四 重新建立字型的 if m fontlogo.m hobject m fontlog...