VC++ 常用编程技巧总结
VC++6.0的使用以及編程技巧整理
1、MDI子窗口一啟動(dòng)就最大化 ??
? BOOL ? CChildFrame::PreCreateWindow(CREATESTRUCT& ? cs) ?
? { ?
? // ? TODO: ? Modify ? the ? Window ? class ? or ? styles ? here ? by ? modifying ?
? // ? ? the ? CREATESTRUCT ? cs ?
? cs.style=WS_CHILD ? | ? WS_VISIBLE ? | ? WS_OVERLAPPEDWINDOW|WS_MAXIMIZE; ?
? if( ? !CMDIChildWnd::PreCreateWindow(cs) ? ) ?
? return ? FALSE; ?
? ?
? return ? TRUE; ?
? } ? ?
?
2、主窗口最大化: ?
? ? ? ? ? 在 ? InitStance ? 函數(shù)中設(shè)定 ? m_nCmdShow的取值. ??
? ? ? ? ? ?m_nCmdShow ? = ? SW_SHOWMAXIMIZED; ? ?
? ? if ? (!ProcessShellCommand(cmdInfo)) ?
? ? ? return ? FALSE; ??
? MDI窗口: ?
? ? ? ? ? 重載 ? MDI ? Window ? 的PreCreateWindow函 ?
? 數(shù),設(shè)置WS_MAXIMIZE ?
? 文檔一生成就最大化: ?
? ? ? ? ? 在視類(lèi)重載的OnInitUpdate成員函數(shù)中加上: ? ?
? ? ? ? ? CMDIChildWnd ? *pWnd=(CMDIChildWnd ? *)GetParentFrame(); ? ?
? ? ? ? ? pWnd->MDIMaximize();
?3、設(shè)置多文檔的標(biāo)題
設(shè)置標(biāo)題在如下兩個(gè)函數(shù)
BOOL CTOLLGATE_OF_CITYDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
SetTitle ("你的文檔名");
return TRUE;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
m_strTitle ="標(biāo)題";
return TRUE;
}
?4 mdi怎樣可以使啟動(dòng)程序時(shí)不打開(kāi)子窗口?
在應(yīng)用程序的初始化函數(shù)InitInstance中的 ?
? CcommandLineInfo ? cmdInfo; ?
? 語(yǔ)句后加入: ?
? cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing; ? ? (要尤氳拇耄??
? ?
? ParseCommandLine(cmdInfo); ? ? (該句已有,不用加) ?
? 即可。 ?
? 是CWinApp類(lèi)中的函數(shù)
5 怎樣單擊窗體任何位置就可移動(dòng)窗體
加上左鍵按下消息 ?
? void ? CXXXDlg::OnLButtonDown(UINT ? nFlags, ? CPoint ? point) ? ?
? { ??
? ? //加上下面這句話(huà) ?
? PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); ? ??
? CDialog::OnLButtonDown(nFlags, ? point); ??
??
? } ?
6 VC下用ADO連接ORACLE數(shù)據(jù)庫(kù)
Oracle公司提供的連接方式: ?
? 使用標(biāo)準(zhǔn)安全級(jí)別: ?
? strConnect ? = ? _T("Provider=OraOLEDB.Oracle;Data ? Source=MyOracleDB;User ? Id=myUsername;Password=myPassword;"); ?
? 使用信任連接 ?
? 1.strConnect ? = ? _T("Provider=OraOLEDB.Oracle;Data ? Source=MyOracleDB;User ? Id=/;Password=;"); ? UID為'/' ?
? 2.strConnect ? = ? _T("Provider=OraOLEDB.Oracle;Data ? Source=MyOracleDB;OSAuthent=1;");使用OSAuthent=1 ?
? 對(duì)于連接字符串可以參考以下網(wǎng)頁(yè): ? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdreforacleprovspec.asp?frame=true ?
? http://download-west.oracle.com/otndoc/oracle9i/901_doc/win.901/a90171/using.htm ?
? ===>ODBC ? 配置數(shù)據(jù)源 ?
? strConnect ? = ? _T("DSN=ADOTest");
?
編程小技巧
http://hi.baidu.com/original/blog/item/c0f7e850e9b6545f1138c275.html
?
去掉單文檔的“無(wú)標(biāo)題”
? if( !CFrameWnd::PreCreateWindow(cs) )
? return FALSE;
?// TODO: Modify the Window class or styles here by modifying
?// ?the CREATESTRUCT cs
?cs.style ? &= ? ~FWS_ADDTOTITLE;
?VC編程做個(gè)超級(jí)鏈接的文本按鈕
http://sjtu.blog.sohu.com/71163647.html
VC下利用ADO連接Access數(shù)據(jù)庫(kù)?
http://blog.csdn.net/pixy0m0/archive/2007/04/17/1568224.aspx
SQL語(yǔ)句VC中的分行?
pcmd->CommandText ? = ? "SELECT ? * ? ? ? ? ? ? ? \ ?
? FROM ? OrderTable ? WHERE ? OrderID ? = ? ?";
各種類(lèi)型轉(zhuǎn)換
常用數(shù)據(jù)類(lèi)型使用轉(zhuǎn)換詳解
http://sjtu.blog.sohu.com/77555070.html
CString 轉(zhuǎn) int ?atoi
CString to double
Cstring與double char 的轉(zhuǎn)換
http://blog.csdn.net/paobo/archive/2007/05/15/1610032.aspx
四舍五入 ?int(num+0.5) ? %.2f
查詢(xún)Excel表要用$結(jié)尾不然找不到 ?select * ?FROM [Stockpool$]
數(shù)據(jù)庫(kù)表中字段含有括號(hào)的話(huà) 用中括號(hào)括起來(lái)表示特殊字符 例如 [EPS(T+1)]
CListCtrl使用技巧 ? ?http://sjtu.blog.sohu.com/72301523.html
打開(kāi)網(wǎng)頁(yè)文件
ShellExecute(hWnd,"open","ieexplorer",CString("file://")+filename ? + ? CString("#a1"), ? NULL,SW_SHOWNORMAL); ??
深入淺出ShellExecute
http://sjtu.blog.sohu.com/72316353.html
?Excel中時(shí)間字段的查詢(xún): Time>#2007-12-03 00:00:00#
?VC窗口自由的調(diào)整大小的實(shí)現(xiàn)
http://www.codeproject.com/dialog/easysize.asp
http://www.xfbbs.com/ArticleShow/85/Article_Show_72223.html
?
用VC++獲取系統(tǒng)時(shí)間幾種方法?
CTime:
%Y%m%d%h%M%S 年月日 時(shí)分秒
就用CTime::GetDayOfWeek() ? or ? COleDateTime::::GetDayOfWeek()啊,他返回一個(gè)int,如果返回值是1就說(shuō)明是星期天,如果是2表示星期一...如果是7表示星期六(每個(gè)星期的第一天是星期天)?
A:
1 使用time_t time( time_t * timer ) ? ?精確到秒
計(jì)算時(shí)間差使用double difftime( time_t timer1, time_t timer0 )
2 使用clock_t clock() 得到的是CPU時(shí)間 ? ?精確到1/CLOCKS_PER_SEC秒
3 使用DWORD GetTickCount() 得到的是系統(tǒng)運(yùn)行的時(shí)間 精確到毫秒
4 如果使用MFC的CTime類(lèi),可以用CTime::GetCurrentTime() 精確到秒
5 要獲取高精度時(shí)間,可以使用
? ? BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)獲取系統(tǒng)的計(jì)數(shù)器的頻率
? ? BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)獲取計(jì)數(shù)器的值
? ? 然后用兩次計(jì)數(shù)器的差除以Frequency就得到時(shí)間。
6 還有David的文章中提到的方法:
? ? Multimedia Timer Functions
? ? The following functions are used with multimedia timers.
? ? timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
? ? timeGetTime/timeKillEvent/TimeProc/timeSetEvent ?精度很高?
Q:GetTickCount()函數(shù),說(shuō)是毫秒記數(shù),是真的嗎,還是精確到55毫秒?
A:
GetTickCount()和GetCurrentTime()都只精確到55ms(1個(gè)tick就是55ms)。如果要精確到毫秒,應(yīng)該使用timeGetTime函數(shù)或QueryPerformanceCounter函數(shù)。具體例子可以參考QA001022 "VC++中使用高精度定時(shí)器"、QA001813 "如何在Windows實(shí)現(xiàn)準(zhǔn)確的定時(shí)"和QA004842 "timeGetTime函數(shù)延時(shí)不準(zhǔn)"。
Q:vc++怎樣獲取系統(tǒng)時(shí)間,返回值是什么類(lèi)型的變量呢??
GetSystemTime返回的是格林威志標(biāo)準(zhǔn)時(shí)間
GetLocalTime,和上面用法一樣,返回的是你所在地區(qū)的時(shí)間,中國(guó)返回的是北京時(shí)間
?
VOID GetSystemTime(
LPSYSTEMTIME lpSystemTime // address of system time structure
);
函數(shù)就可以獲得了,其中LPSYSTEMTIME 是個(gè)結(jié)構(gòu)體
含:年,月,日,周幾,小時(shí),分,秒,毫秒。
?
Disable一個(gè)控件:GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
?
VC中如何發(fā)布Release版本 工具欄上點(diǎn)右鍵,選擇編譯,將編譯win32debug改為win32 Release即可。如果需要調(diào)試的話(huà),還需要再改回來(lái)方可
?
VC程序可以再其它機(jī)子上運(yùn)行:
你的目標(biāo)程序需要?jiǎng)討B(tài)鏈接庫(kù)文件的支持, ? ?
? 把程序中用到的DLL文件放到程序的目錄下就可以運(yùn)行了。
project->setting->General->下拉框中選擇"Use ? MFC ? in ? a ? Static ? Library”
?
?
第一:private, public, protected 訪(fǎng)問(wèn)標(biāo)號(hào)的訪(fǎng)問(wèn)范圍。
private:只能由1.該類(lèi)中的函數(shù)、2.其友元函數(shù)訪(fǎng)問(wèn)。
不能被任何其他訪(fǎng)問(wèn),該類(lèi)的對(duì)象也不能訪(fǎng)問(wèn)。
protected:可以被1.該類(lèi)中的函數(shù)、2.子類(lèi)的函數(shù)、以及3.其友元函數(shù)訪(fǎng)問(wèn)。
但不能被該類(lèi)的對(duì)象訪(fǎng)問(wèn)。
public:可以被1.該類(lèi)中的函數(shù)、2.子類(lèi)的函數(shù)、3.其友元函數(shù)訪(fǎng)問(wèn),也可以由4.該類(lèi)的對(duì)象訪(fǎng)問(wèn)。
?
注:友元函數(shù)包括3種:設(shè)為友元的普通的非成員函數(shù);設(shè)為友元的其他類(lèi)的成員函數(shù);設(shè)為友元類(lèi)中的所有成員函數(shù)。
第二:類(lèi)的繼承后方法屬性變化。
private 屬性不能夠被繼承。
使用private繼承,父類(lèi)的protected和public屬性在子類(lèi)中變?yōu)閜rivate;
使用protected繼承,父類(lèi)的protected和public屬性在子類(lèi)中變?yōu)閜rotected;
使用public繼承,父類(lèi)中的protected和public屬性不發(fā)生改變;?
?
如下所示:?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public: ? ? ? ? ? ?protected: ? ? ? private:
public繼承 ? ? ? ? ? ? public ? ? ? ? ? ? protected ? ? ? ?不可用?
protected繼承 ? ? ? protected ? ? ? protected ? ? ? ?不可用?
private繼承 ? ? ? ? ? ?private ? ? ? ? ? private ? ? ? ? ? ? 不可用?
protected繼承和private繼承能降低訪(fǎng)問(wèn)權(quán)限。
?
?CTime COleDateTime的常用操作和比較
百分號(hào)的表示
用%%表示% ,或者CStrng ? str; ?
? str.format("select ? * ? from ? Name ? where ? address ? like ? %sabc%s","%","%");
viewform.cpp line 69 錯(cuò)誤: 應(yīng)該把dialog改為child。
?
取消所有斷點(diǎn) edit->breakpoints->remove all 或者 Ctrl+Shift+F9
?
設(shè)置checkBox屬性為選中 ?((CButton ? *)GetDlgItem(IDC_CHECK1+i))->SetCheck(1); ?
判斷是否選中 if(IsDlgButtonChecked(IDC_SEX1))
取得活動(dòng)視圖和主框架
? CMainFrame ? *pMainFrame=(CMainFrame ? *)AfxGetMainWnd(); ?
? CSRSFormView ? * pView ?=(CSRSFormView *)pMainFrame->GetActiveView();
關(guān)于missing ';' before identifier 'A' ?兩個(gè)文件的相互引用引起的...
?
VC中看不到類(lèi):刪除clw文件 重新建立
?
獲取控件的中心:GetDlgItem(IDC_SEARCH1)->GetWindowRect(&rc); rc.CenterPoint()
?
SendMessage 傳遞多個(gè)變量:
? ? ? ? tObj *fi=new tObj;
? ? ? ? fi->iItem=idx;
? ? ? ? fi->iFolder=iSelFolder;
? ?GetOwner()->SendMessage(WM_OUTBAR_NOTIFY, NM_OB_ITEMCLICK, (UINT)fi);
那邊接收到后再用(tObj*)轉(zhuǎn)換回來(lái)
?
問(wèn)題: 請(qǐng)問(wèn)何時(shí)用UpdateData(false),何時(shí)用UpdateData(true)呢?
答案:當(dāng)你使用了ClassWizard建立了控件和變量之間的聯(lián)系后:當(dāng)你修改了變量的值,而希望對(duì)話(huà)框控件更新顯示,就應(yīng)該在修改變量后調(diào)用UpdateData(FALSE);如果你希望知道用戶(hù)在對(duì)話(huà)框中到底輸入了什么,就應(yīng)該在訪(fǎng)問(wèn)變量前調(diào)用UpdateData(TRUE)。
設(shè)置控件標(biāo)題 ? GetDlgItem(IDC_GetCapProfit)->SetWindowText("全部顯示");
?
DateTime change事件被執(zhí)行二次的改進(jìn):
?static ? BOOL ? bOK=TRUE; ?
#if ? 1//判斷是否有CMonthCalCtrl,若有會(huì)發(fā)送兩次Change,截第二次即可 ?
?CDateTimeCtrl* ? pCtrl ? = ? (CDateTimeCtrl*) ? GetDlgItem(IDC_DATESELECTED); ?
?ASSERT(pCtrl ? != ? NULL); ?
?CMonthCalCtrl* ? pMoCalCtrl ? = ? pCtrl->GetMonthCalCtrl(); ?
?if(pMoCalCtrl ? != ? NULL) ?
? ? ? ? bOK ? = ? !bOK; ?
#endif ?
?if(bOK) ?
? AfxMessageBox(""); ?
? *pResult ? = ? 0; ? ?
無(wú)模式對(duì)話(huà)框:Cdlg dlg; dlg.Create(IDD_DIALOG1,this); dlg.ShowWindow(SW_SHOW);
先為對(duì)話(huà)框加上2個(gè)radio button,分別是Radio1和Radio2。
問(wèn)題1:如何讓Radio1或者Radio2默認(rèn)選上?如何知道哪個(gè)被選上了?
關(guān)鍵是選上,“默認(rèn)”只要放在OnInitDialog()即可。三種方法可以讓它選上,
第一種:
((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);//選上
((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(FALSE);//不選上
((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck();返回1表示選上,0表示沒(méi)選上
第二種:
關(guān)聯(lián)一個(gè)congtrol型變量(子類(lèi)化),好ctrl+W(即打開(kāi)classwizard),點(diǎn)開(kāi) Member Variables,咦?怎么沒(méi)有IDC_RADIO1這個(gè)ID?原來(lái)是沒(méi)有分組。因?yàn)閞adio button通常都是成組使用的,在一組里面是互斥的。取消,回到對(duì)話(huà)框資源面板,右鍵Radio1查看屬性把Group選上,那么,Radio1和Radio2就是一組了(怎么知道他們是一組的?后面說(shuō))。此時(shí),就可以為Radio1增加一congtrol型變量m_ctrlRadio1了。如下:
m_ctrlRadio1.SetCheck(TRUE);
同樣可以使用GetCheck()獲取狀態(tài)。
第三種:
關(guān)聯(lián)一個(gè)int型變量(同樣需要先分組)m_nRadio1,打開(kāi)對(duì)話(huà)框構(gòu)造函數(shù),你會(huì)發(fā)現(xiàn)有:
m_nRadio1 = -1;m_nRadio1別賦值-1表示哪個(gè)都沒(méi)有選上。如果你把-1改成0,就會(huì)發(fā)現(xiàn)Radio1默認(rèn)被選上了,依此類(lèi)推,m_nRadio1的值為1就是第二個(gè)被選上了(這里同樣有問(wèn)題,哪個(gè)是第一個(gè)?哪個(gè)是第二個(gè)?)。獲取狀態(tài)很簡(jiǎn)單,UpdateData(TRUE)后判斷m_nRadio1的值即可。
問(wèn)題2:如何使用多組?
多組和一組是一樣的使用,只要搞清楚哪個(gè)是哪一組的就行了。再為對(duì)話(huà)框添加Radio3和Radio4。很簡(jiǎn)單,先為這些Radio Button排個(gè)順序,就是排列他們的TAB ORDER。在對(duì)話(huà)框資源面板上Ctrl+D,然后按你自己的理想順序用鼠標(biāo)逐個(gè)點(diǎn)擊就可以了。不妨假設(shè)Radio1、Radio2、Radio3、Radio4分別是1、2、3、4。Radio1和Radio3都選上Group屬性,那么,1、2是一組,3、4是另外一組,因?yàn)榉纸M的原則是在選上Group屬性的這一個(gè)開(kāi)始直到碰到下一個(gè)選上Group屬性的。你不妨再Ctrl+D,令Radio1、Radio2、Radio3、Radio4分別是1、3、2、4,那么Radio1和Radio3是一組,如果m_nRadio1=1,此時(shí)是Radio3被選上而不是Radio2被選上。分好了組就分別使用它們吧。
嗯,也許你還要為它們添加鼠標(biāo)單擊事件,非常簡(jiǎn)單。
?
一、對(duì)單“磁ソ蟹腫椋
每組的第一個(gè)單“磁ド柚檬糶裕篏roup,Tabstop,Auto;其余按鈕設(shè)置屬性Tabstop,Auto。如:
Radio1、Radio2、Radio3為一組,Radio4、Radio5為一組
設(shè)定Radio1屬性:Group,Tabstop,Auto
設(shè)定Radio2屬性:Tabstop,Auto
設(shè)定Radio3屬性:Tabstop,Auto
設(shè)定Radio4屬性:Group,Tabstop,Auto
設(shè)定Radio5屬性:Tabstop,Auto
二、用ClassWizard為單選控件定義變量,每組只能定義一個(gè)。如:m_Radio1、m_Radio4。
三、用ClassWizard生成各單“磁サ牡セ饗⒑⒓尤肽諶藎
void CWEditView::OnRadio1()
{
? ? m_Radio1 = 0; ? ?//第一個(gè)單“磁ケ謊≈
}
void CWEditView::OnRadio2()
{
? ? m_Radio1 = 1; ? ?//第二個(gè)單“磁ケ謊≈
}
void CWEditView::OnRadio3()
{
? ? m_Radio1 = 2; ? ?//第三個(gè)單“磁ケ謊≈
}
void CWEditView::OnRadio4()
{
? ? m_Radio4 = 0; ? ?//第四個(gè)單“磁ケ謊≈
}
void CWEditView::OnRadio5()
{
? ? m_Radio4 = 1; ? ?//第五個(gè)單“磁ケ謊≈
}
四、設(shè)置默認(rèn)按鈕:
在定義控件變量時(shí),ClassWizard在構(gòu)造函數(shù)中會(huì)把變量初值設(shè)為-1,只需把它改為其它值即可。
如:
?//{{AFX_DATA_INIT(CUnitBlockTypeFlankPublicAdd)
?m_Radio1 = 0; ? ?//初始時(shí)第一個(gè)單“磁ケ謊≈
?m_Radio4 = 0; ? ?//初始時(shí)第四個(gè)單“磁ケ謊≈
?//}}AFX_DATA_INIT
終止線(xiàn)程有三種途徑
1.線(xiàn)程可以在自身內(nèi)部調(diào)用AfxEndThread()來(lái)終止自身的運(yùn)行;
?2.可以在線(xiàn)程的外部調(diào)用BOOL TerminateThread( HANDLE hThread, DWORD dwExitCode )來(lái)強(qiáng)行終止一個(gè)線(xiàn)程的運(yùn)行,然后調(diào)用CloseHandle()函數(shù)釋放線(xiàn)程所占用的堆棧
?3.改變?nèi)肿兞?#xff0c;使線(xiàn)程的執(zhí)行函數(shù)返回,則該線(xiàn)程終止。這種方法最安全。
全局函數(shù)調(diào)用: extern ?UINT ThreadFunc(LPVOID lpParam);
?
在VC中,如果我們想更新UI信息,我們一般調(diào)用UpdateData(FALSE),這在
窗口線(xiàn)程中調(diào)用是正確的,但如果在后臺(tái)進(jìn)程中這樣調(diào)用主窗口的UpdateData(FALSE),則會(huì)出現(xiàn)問(wèn)題,原因很簡(jiǎn)單:兩個(gè)線(xiàn)程更新同一個(gè)窗口的UI信息,會(huì)引起UI的線(xiàn)程安全問(wèn)題,當(dāng)然也可以更多的線(xiàn)程。
我們可以借助消息機(jī)制,來(lái)實(shí)現(xiàn)多線(xiàn)程的UI更新問(wèn)題。當(dāng)線(xiàn)程要更新主窗口的UI時(shí),發(fā)送一個(gè)自定義消息給主窗口,主窗口在消息處理中UpdateData(FALSE);
因?yàn)橄⒌奶幚聿扇£?duì)列形式,挨個(gè)處理,線(xiàn)程安全是肯定的!
主窗口:
.h文件:
?
afx_msg ?LRESULT OnUpdateData(LPARAM lParam, WPARAM wParam);
.cpp文件:
BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
? ON_MESSAGE(WM_UPDATEDATA, OnUpdateData)
END_MESSAGE_MAP()
#define WM_UPDATEDATA ? WM_USER + 1
LRESULT OnUpdateData(LPARAM lParam, WPARAM wParam)
{
? return UpdateData(FALSE);
}
線(xiàn)程:
SendMessage(WM_UPDATEDATA)
或者
SendMessageW(hMainWnd, WM_UPDATEDATA, 0, 0)
?
更改XP風(fēng)格的程序:
?
The following steps introduces XP Theme Style to your project:
Insert Resource, choose "Custom", input "24" (without quotes) as resource type
Copy and paste the following XML sheets into the editor.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity processorArchitecture="x86" version="5.1.0.0" type="win32" name="test.exe"/> <description>Test Application</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="x86"/> </dependentAssembly> </dependency> </assembly>
You can replace test.exe and Test Application with any string you like. This will not affect the behaviour of the application.
Change resource ID to 1.
Add calls to InitCommonControls() in your WinMain(). Don't forget to include commctrl.h and link comctl32.lib. Rebuild your project, all done :-)
This special resource type is only recognized by Windows XP. In any other version of Windows (Windows 9x/NT/2000), the program simply runs as before.
You can also create a manifest file with the same name of the EXE file, plus an extension .manifest, which contains the same lines as the ones above. For our test.exe, the manifest file is test.exe.manifest.
Excel中的NULL值,應(yīng)該用ISNULL來(lái)判斷
?sSql="select Name,Code,Price,Date,Market,Total,[M_Weight(%)],[Re_Weight(%)],[PE(T)],[Profit(T)],[Profit(TTM)],[PE(TTM)] from [HSsheet$] where ISNULL([GICS_4]) ?and ISNULL([GICS_C4]) and Date='"+sDate+"'";
ComboBox的用法: ? http://www.codeproject.com/KB/combobox/combobox_tut.aspx
?打開(kāi)對(duì)話(huà)框并復(fù)制文件:
? ? ?CFile ? file; ?
? ? ? CString ? m_FileName; ?
? ? ? CFileDialog ? m_FileOpen(FALSE,".mdb",NULL,OFN_OVERWRITEPROMPT,"Result ? Files(*.mdb)|*.mdb|所有文件(*.*)|*.*||"); ?
? ? ? if(m_FileOpen.DoModal()==IDOK){ ?
? ? ? m_FileName=m_FileOpen.GetPathName(); ?
? ? ? } ?
? ?
? ? ? CString ? m_Path=m_FileName.Left(m_FileName.ReverseFind('\\')); ?
? ?
? ? ? CString ? strFileName="C:\\project\\Files\\Result.mdb"; ?
? ?
? ? ? LPCTSTR ? lpNewFileName ? = ? (LPCTSTR)(m_FileName); ?
? ? ? int ? a=CopyFile( ?
? ? ? (LPCTSTR)strFileName, ? ? // ? pointer ? to ? name ? of ? an ? existing ? file ?
? ? ? m_FileName, ? ? // ? pointer ? to ? filename ? to ? copy ? to ?
? ? ? FALSE ? ? ? ? ? ? // ? flag ? for ? operation ? if ? file ? exists ?
? ? ? ); ?
找到窗口中的控件:
? // ? TODO: ? Add ? your ? control ? notification ? handler ? code ? here ?
? ? ? UINT ? k=WinExec("rundll32.exe ? shell32.dll,Control_RunDLL ? desk.cpl",SW_SHOW); ?
? ? ? if(k<32) ?
? ? ? { ?
? ? ? CString ? x; ?
? ? ? x.Format("%d",k); ?
? ? ? MessageBox(x); ? ? ? ? ? ? ? ? ? ? // ? 要在error后返回,下同 ?
? ? ? } ?
? ? ? HWND ? wd=::FindWindow(NULL,"顯示 ? 屬性"); ?
? ? ? if ? (wd==NULL) ?
? ? ? MessageBox("Error1"); ?
? ? ? ::ShowWindow(wd,SW_HIDE); ?
? ?
? ? ? HWND ? wtd=FindWindowEx(wd,NULL,"SysTabControl32",NULL); ?
? ? ? if ? (wtd==NULL) ?
? ? ? MessageBox("Error2"); ?
? ?
? ? ? TabCtrl_SetCurFocus(wtd,2); ?
? ?
? ? ? HWND ? wd1=FindWindowEx(wd,NULL,NULL,"外觀(guān)"); ?
? ? ? if ? (wd1==NULL) ?
? ? ? MessageBox("Error3"); ?
? ? ? HWND ? cb1=FindWindowEx(wd1,NULL,"ComboBox",NULL); ?
? ? ? if ? (cb1==NULL) ?
? ? ? MessageBox("Error4"); ?
? ? ? ::SendMessage(cb1,CB_SELECTSTRING,-1,(long)"淡綠色"); ?
? ? ? long ? id=::GetDlgCtrlID(cb1); ?
? ? ? ::SendMessage(wd1,WM_COMMAND, ? MAKELONG(id,CBN_SELCHANGE),(long)cb1); ?
? ? ? HWND ? cb2=FindWindowEx(wd1,cb1,"ComboBox",NULL); ?
? ? ? if ? (cb2==NULL) ?
? ? ? MessageBox("Error5"); ?
? ? ? ?
? ? ? //同上可在這修改項(xiàng)目 ?
? ? ? // ? 或其它屬性 ?
? ? ? HWND ? bb1=FindWindowEx(wd,NULL,NULL,"確定"); ?
? ? ? if ? (bb1==NULL) ?
? ? ? MessageBox("Error6"); ?
? ? ? id=::GetDlgCtrlID(bb1); ?
? ? ? ::SendMessage(wd,WM_COMMAND, ? MAKELONG(id,BN_CLICKED),(long)bb1); ?
?
CTime或者COleDateTime,Format方法的使用
關(guān)鍵詞: CTime ? ?COleDateTime ? ?Format ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
CTime ct = CTime::GetCurrentTime();
CString str = ct.Format("%Y-%m_%d %H-%M-%S");
輸出為:str="2006-04-23 15-21-30"
%a
Abbreviated weekday name
%A
Full weekday name
%b
Abbreviated month name
%B
Full month name
%c
Date and time representation appropriate for locale
%d
Day of month as decimal number (01 – 31)
%H
Hour in 24-hour format (00 – 23)
%I
Hour in 12-hour format (01 – 12)
%j
Day of year as decimal number (001 – 366)
%m
Month as decimal number (01 – 12)
%M
Minute as decimal number (00 – 59)
%p
Current locale's A.M./P.M. indicator for 12-hour clock
%S
Second as decimal number (00 – 59)
%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w
Weekday as decimal number (0 – 6; Sunday is 0)
%W
Week of year as decimal number, with Monday as first day of week (00 – 53)
%x
Date representation for current locale
%X
Time representation for current locale
%y
Year without century, as decimal number (00 – 99)
%Y
Year with century, as decimal number
%z, %Z
Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%%
Percent sign
// Example for CTime::Format and CTime::FormatGmt
CTime t( 1999, 3, 19, 22, 15, 0 );
// 10:15 PM March 19, 1999
CString s = t.Format( "%A, %B %d, %Y" );
ATLASSERT( s == "Friday, March 19, 1999" );
請(qǐng)問(wèn)如何讓vc程序在沒(méi)有安裝vc 機(jī)子上運(yùn)行
?有三種方式可以解決
1:采用靜態(tài)連接模式發(fā)布執(zhí)行文件:這個(gè)alt+f9看設(shè)置
2:采用打包模式發(fā)布安裝程序
3:就是采用DEPENDS.EXE工具(vc6自帶的)打開(kāi)執(zhí)行文件,查看運(yùn)行所需動(dòng)態(tài)庫(kù),把這些都與執(zhí)行文件放在一起發(fā)布
前兩種如樓上幾位說(shuō)的
?
Double變量和分?jǐn)?shù)相乘時(shí)候要注意1/4應(yīng)該表示成1.0/4.
multimon.h頭文件的include比較例外,格式如下:
#pragma comment(linker, "/FORCE:MULTIPLE")
#pragma comment(linker, "/OPT:NOREF")
#define COMPILE_MULTIMON_STUBS
#include "multimon.h"
一些常用的代碼:
http://sjtu.blog.sohu.com/77568810.html
ListBox的使用:
http://sjtu.blog.sohu.com/79741748.html
響應(yīng)CEdit控件的鼠標(biāo)單擊事件:
BOOL CStockDLG::PreTranslateMessage(MSG* pMsg)
{
?// TODO: Add your specialized code here and/or call the base class
?switch(pMsg->message) ?
?{ ?
?case ? WM_LBUTTONDOWN:
? {
? ?CPoint ? pt; ?
? ?GetCursorPos(&pt); ?
? ?ScreenToClient(&pt);
? ?
? ?CRect rect;
? ?GetDlgItem(IDC_STOCKID)->GetWindowRect(&rect);
? ?ScreenToClient(&rect);
? ?if(rect.PtInRect(pt))
? ?{
? ? ? ? ? ?PostMessage(WM_OPENSSDLG,0,0);
? ?}
? ?break;
? }
?} ?
?
?return CDialog::PreTranslateMessage(pMsg);
}
PreTranslateMessage中不能使用domodal的原因:?
mfc的PreTranslateMessage是由一個(gè)全局函數(shù)AfxInternalPreTranslateMessage來(lái)執(zhí)行的,這個(gè)函數(shù)是不可重入的,也就是不能嵌套調(diào)用。 ?
? ?
? 你在PreTranslateMessage中DoModal,那么DoModal中又有自己的消息循環(huán),必然會(huì)導(dǎo)致AfxInternalPreTranslateMessage的重入。 ??
? ??
?解決方法:
void ? CMyWnd::OnKeyUp(UINT ? nChar,UINT ? nRepCnt,UINT ? nFlags) ?
? { ?
? ? ? CWnd::OnKeyUp(nChar,nRepCnt,nFlags); ?
? ? ? if(nChar ? == ? VK_SPACE) ?
? ? ? { ?
? ? ? ? ? CChsRollDlg ? dlg; ?
? ? ? ? ? dlg.DoModal(); ?
? ? ? } ?
? } ?
? ?
? 改成這樣 ?
? ?
? void ? CMyWnd::OnKeyUp(UINT ? nChar,UINT ? nRepCnt,UINT ? nFlags) ?
? { ?
? ? ? CWnd::OnKeyUp(nChar,nRepCnt,nFlags); ?
? ? ? if(nChar ? == ? VK_SPACE) ?
? ? ? ? ? PostMessage ? (WM_USER, ? 0, ? 0); ?
? } ?
? ?
? // ? WM_USER的消息函數(shù) ?
? void ? CMyDlg::OnUser ? (...) ?
? { ?
? ? ? CChsRollDlg ? dlg; ?
? ? ? dlg.DoModal(); ?
? }; ?
屏蔽Dialog的Enter和ESC鍵:
BOOL CAShareDlg::PreTranslateMessage(MSG* pMsg)
{
?// TODO: Add your specialized code here and/or call the base class
? ? if(pMsg -> message == WM_KEYDOWN)
?{
? ? ? ? if(pMsg -> wParam == VK_ESCAPE)
? ?return TRUE;
? if(pMsg -> wParam == VK_RETURN)
? ?return TRUE;
?} ?
?return CDialog::PreTranslateMessage(pMsg);
}
點(diǎn)擊對(duì)話(huà)框外面時(shí)候?qū)υ?huà)框關(guān)閉的方法:
if(!bActive)
? if(!bOkClosed)
? EndDialog(IDCANCEL);
WM_NCACTIVATE響應(yīng) ?
? wParam參數(shù)為FALSE時(shí)關(guān)閉
一個(gè)很好用的CXFileDialog:
http://www.codeproject.com/KB/dialog/xfiledialog.aspx
MFC學(xué)習(xí)總結(jié) (90個(gè)技巧)
http://www.cnblogs.com/cy163/archive/2006/10/18/532871.html
如何讓沒(méi)有TitleBar的對(duì)話(huà)框,能響應(yīng)任務(wù)欄左鍵最小化事件以及任務(wù)欄右鍵菜單?
?ModifyStyle(0, ? WS_SYSMENU);
?ModifyStyle(0, ? WS_MINIMIZEBOX); ?
運(yùn)行Dos命令并判斷其運(yùn)行結(jié)束:
?STARTUPINFO ? si; ?
?PROCESS_INFORMATION ? pi; ?
?ZeroMemory( ? &si, ? sizeof(si) ? ); ?
?si.cb ? = ? sizeof(si); ?
?ZeroMemory( ? &pi, ? sizeof(pi) ? ); ?
?si.dwFlags ? = ? STARTF_USESHOWWINDOW; ? ? //設(shè)置隱藏執(zhí)行窗口 ?
?si.wShowWindow ? = ? SW_HIDE; ?
? ?
? ? CreateProcess(NULL, ? (char*)(LPCTSTR)strCmd, ? NULL, ? NULL, ? FALSE, ? 0, ? NULL, ? NULL, ? &si, ? &pi);
?while(true)
?{
? if(WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0)
? ?break;
?}
Vector的序列化:
? 保存 ?
? copy(vi.begin(), ? vi.end(), ? ostream_iterator<int>(...)); ?
? ...省略號(hào)處可以改為ofstream的話(huà),就將vi對(duì)象中的各個(gè)值存入ofstream所幫定的文件對(duì)象; ?
? ?
? 取出 ?
? copy(istream_iterator(ifstream(...)), ? ifstream_iterator(ifstream_iterator<int> ? eof, ? back_insert(vi) ? ); ?
? ?
? 此處是從文件中順序讀入vi的值,并且使用back_insert()方法 ? ?
========
VC++常用編程技巧
1如何獲取應(yīng)用程序的實(shí)例句柄?
應(yīng)用程序的實(shí)例句柄保存在CWinApp m_hInstance 中,可以這么調(diào)用AfxGetInstancdHandle獲得句柄.
HANDLE hInstance=AfxGetInstanceHandle()
2 如何通過(guò)代碼獲得應(yīng)用程序主窗口的指針?
主窗口的 指針保存在CWinThread::m_pMainWnd中,調(diào)用AfxGetMainWnd實(shí)現(xiàn)。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED) //使程序最大化.
3 如何在程序中獲得其他程序的圖標(biāo)?
兩種方法:
· ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? SDK函數(shù) SHGetFileInfo 或使用 ExtractIcon獲得圖標(biāo)資源的handle.
void CSampleView:
OnDraw(CDC * pDC)
{
if( :: SHGetFileInfo(_T("c:\\pwin95\\notepad.exe"),0,
&stFileInfo,sizeof(stFileInfo),SHGFI_ICON))
{
pDC ->DrawIcon(10,10,stFileInfo.hIcon)
}
}
· ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? SDK函數(shù) SHGetFileInfo 獲得有關(guān)文件的很多信息,如大小圖標(biāo),屬性,類(lèi)型等.
void CSampleView:: OnDraw(CDC *pDC)
{
HICON hIcon=:: ExtractIcon(AfxGetInstanceHandle(),_T
("NotePad.exe"),0)
if (hIcon &&hIcon!=(HICON)-1)
pDC->DrawIcon(10,10,hIcon)
}
?
說(shuō)明: 獲得notepad.exe的路徑正規(guī)上來(lái)說(shuō)用GetWindowsDirectory函數(shù)得到,如果是調(diào)用 win95下的畫(huà)筆,應(yīng)該用訪(fǎng)問(wèn)注冊(cè)表的方法獲得其路徑,要作成一個(gè)比較考究的程序,考慮應(yīng)該全面點(diǎn).
4 如何編程結(jié)束應(yīng)用程序?
這是個(gè)很簡(jiǎn)單又是編程中經(jīng)常要遇到的問(wèn)題. 向窗口發(fā)送 WM_CLOSE消息,調(diào)用 CWnd::OnClose成員函數(shù).允許對(duì)用戶(hù)提示是否保存修改過(guò)的數(shù)據(jù).
//發(fā)送關(guān)閉窗口消息
AfxGetMainWindow()->SendMessage(WM_CLOSE)
?
//通過(guò)標(biāo)題欄尋找需要關(guān)閉的窗口,然后關(guān)閉找到的窗口
void Terminate_Window(LPCSTR pCaption)
{
CWnd *pWnd=Cwnd::FindWindow(NULL,pCaption)
if (pWnd)
pWnd ->SendMessage(WM_CLOSE)
}
說(shuō)明: FindWindow函數(shù)不是提倡的做法,因?yàn)樗鼰o(wú)法處理標(biāo)題欄自動(dòng)改變,比如我們要檢測(cè) Notepad是不是已運(yùn)行而事先不知道Notepad的標(biāo)題欄,這時(shí)FindWindow就無(wú)能為力了,可以通過(guò)枚舉 windows任務(wù)列表的辦法來(lái)實(shí)現(xiàn)。在機(jī)械出版社”Windows 95 API開(kāi)發(fā)人員指南”一書(shū)有比較詳細(xì)的介紹,這里就不再多說(shuō)了。
5 怎樣加載其他的應(yīng)用程序?
三個(gè)SDK函數(shù) winexec, shellexecute,createprocess可以使用。 WinExec最簡(jiǎn)單,兩個(gè)參數(shù),前一個(gè)指定路徑,后一個(gè)指定顯示方式.后一個(gè)參數(shù)值得說(shuō)一下,比如泥用 SW_SHOWMAXMIZED方式去加載一個(gè)無(wú)最大化按鈕的程序,就是Neterm,calc等等,就不會(huì)出現(xiàn)正常的窗體,但是已經(jīng)被加到任務(wù)列表里了。
ShellExecute較 WinExex靈活一點(diǎn),可以指定工作目錄,下面的Example就是直接打開(kāi) c:\temp\1.txt,而不用加載與 txt文件關(guān)聯(lián)的應(yīng)用程序,很多安裝程序完成后都會(huì)打開(kāi)一個(gè)窗口,來(lái)顯示Readme or Faq,我猜就是這么作的啦.
ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOWMAXMIZED)
CreateProcess最復(fù)雜,一共有十個(gè)參數(shù),不過(guò)大部分都可以用NULL代替,它可以指定進(jìn)程的安全屬性,繼承信息,類(lèi)的優(yōu)先級(jí)等等.來(lái)看個(gè)很簡(jiǎn)單的Example:
STARTUPINFO stinfo
//啟動(dòng)窗口的信息
PROCESSINFO procinfo //進(jìn)程的信息
?
CreateProcess(NULL,_T("notepad.exe"),NULL,NULL.FALSE,
NORMAL_PRIORITY_
CLASS,NULL,NULL, &stinfo,&procinfo)
?
6 如何確定應(yīng)用程序的路徑?
Use GetModuleFileName 獲得應(yīng)用程序的路徑,然后去掉可執(zhí)行文件名。 Example:
TCHAR exeFullPath[MAX_PATH]
GetModuleFileName(NULL,exeFullPath,MAX_PATH)
7 如何獲得各種目錄信息?
Windows目錄: Use “GetWindowsDirectory” Windows下的system目錄: Use “GetSystemDirectory” temp目錄: Use “GetTempPath” 當(dāng)前目錄: Use “GetCurrentDirectory”
請(qǐng)注意前兩個(gè)函數(shù)的第一個(gè)參數(shù)為目錄變量名,后一個(gè)為緩沖區(qū)后兩個(gè)相反
8 如何自定義消息?
(1) 手工定義消息,可以這么寫(xiě) #define WM_MY_MESSAGE(WM_USER+100), MS 推薦的至少是 WM_USER+100
(2)寫(xiě)消息處理函數(shù),用 WPARAM,LPARAM返回LRESULT. LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)
9 如何改變窗口的圖標(biāo)?
向窗口發(fā)送 WM_SECTION消息。 Example:
HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)
ASSERT(hIcon)
AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM)hIcon)
10 如何改變窗口的缺省風(fēng)格?
重載 CWnd:: PreCreateWindow 并修改CREATESTRUCT結(jié)構(gòu)來(lái)指定窗口風(fēng)格和其他創(chuàng)建信息.
//Delete "Max" Button and Set Original Window's Position and Size
BOOL CMainFrame:: PreCreateWindow
(CREATESTRUCT &cs)
{
cs.style &=~WS_MAXINIZEMOX
?
cs.x=cs.y=0
cs.cx=GetSystemMetrics(SM_CXSCREEN/2)
cs.cy=GetSystemMetrics(SM_CYSCREEN/2)
?
return CMDIFramewnd ::PreCreateWindow(cs)
}
?
11如何將窗口居中顯示?
調(diào)用窗口函數(shù) CWnd::Center_window() Example(1):
Center_Window( ) //Relative to it's parent // Relative to Screen
Example(2):
Center_Window(CWnd:: GetDesktopWindow( ))
//Relative to Application's MainWindow
AfxGetMainWnd( )->Center_Window( );
12 如何讓窗口和 MDI窗口一啟動(dòng)就最大化和最小化?
先說(shuō)窗口。 在 InitStance 函數(shù)中設(shè)定 m_nCmdShow的取值. m_nCmdShow=SW_SHOWMAXMIZED 最大化 m_nCmdShow=SW_SHOWMINMIZED 最小化 m_nCmdShow=SW_SHOWNORMAL 正常方式 MDI窗口: 如果是創(chuàng)建新的應(yīng)用程序,可以用MFC AppWizard 的Advanced 按鈕并在MDI子窗口風(fēng)格組中檢測(cè)最大化或最小化還可以重載 MDI Window 的PreCreateWindow函數(shù),設(shè)置WS_MAXMIZE or WS_MINMIZE 如果從 CMDIChildWnd派生,調(diào)用 OnInitialUpdate函數(shù)中的 CWnd::Show Window來(lái)指定 MDI Child Window的風(fēng)格。 OnQueryOpen() ,add following code Bool CMainFrame:: OnQueryOpen( ) { Return false } </code>
13 如何使程序保持極小狀態(tài)?
這么辦: 在恢復(fù)程序窗體大小時(shí),Windows會(huì)發(fā)送WM_QUERY-OPEN消息,用 ClassWizard設(shè)置成員函數(shù) <code cpp>
14 如何限制窗口的大小?
也就是 FixedDialog形式。 Windows發(fā)送 WM_GETMAXMININFO消息來(lái)跟蹤, 響應(yīng)它,在 OnGetMAXMININFO 中寫(xiě)代碼:
15 如何使窗口不可見(jiàn)?
很簡(jiǎn)單,用SW_HIDE 隱藏窗口,可以結(jié)合 FindWindow,ShowWindow控制.
16 如何使窗口始終在最前方?
BringWindowToTop(Handle) SetWindowPos函數(shù),指定窗口的 最頂風(fēng)格,用WS_EX_TOPMOST擴(kuò)展窗口的風(fēng)格
void ToggleTopMost(CWnd *pWnd)
{
ASSERT_VALID(pWnd)
?
pWnd ->SetWindowPos(pWnd-> GetStyle( ) &WS_EX_TOPMOST)?
?
&wndNoTopMOST: &wndTopMost,0,0,0,0,SSP_NOSIZE|WSP_NOMOVE)
}
17 如何創(chuàng)建一個(gè)字回繞的CEditView?
重載CWnd : : PreCreateWindow和修改CREATESTRUCT結(jié)構(gòu),關(guān)閉CEditView對(duì)象的ES_AUTOHSCROLL和WS_HSCROLL 風(fēng)格位, 由于CEditView : : PreCreateWindow顯示設(shè)置cs. style,調(diào)用基類(lèi)函數(shù)后要修改cs . style。
BOOL CSampleEDitView : : PreCreateWindow (CREATESTRUCT&cs)
{
//First call basse class function .
BOOL bResutl =CEditView : : PreCreateWindow (cs)
?
// Now specify the new window style .
cs.style &= ~ (ES_AUTOHSCROLL |WS_HSCROLL)
return bResult
}
18 通用控件的顯示窗口
MFC提供了幾個(gè)CView派生的視窗類(lèi),封裝了通用控件的功能,但仍然使用工作框文檔顯示窗口體系結(jié)構(gòu):CEditView封裝了編輯控件,CTreeView保持了樹(shù)列表控件,CListView封裝了列表顯示窗口控件,CRichEditView可以處理多種編輯控件。
19 如何移動(dòng)窗口?
調(diào)用CWnd : : SetWindowPos并指定SWP_NOSIZE標(biāo)志。目的位置與父窗口有關(guān)(頂層窗口與屏幕有關(guān))。調(diào)用CWnd : : MoveWindow時(shí)必須要指定窗口的大小。
//Move window to positoin 100 , 100 of its parent window .
SetWindowPos (NULL, 100 , 100 , 0 , 0 , SWP_NOSIZE |SWP_NOAORDER)
20 如何重置窗口的大小?
調(diào)用CWnd: : SetWindowPos并指定SWP_NOMOVE標(biāo)志, 也可調(diào)用CWnd : : MoveWindow 但必須指定窗口的位置。
// Get the size of the window .
Crect reWindow
GetWindowRect (reWindow )
?
//Make the window twice as wide and twice as tall .
SetWindowPos (NULL , 0 , 0 , reWindow . Width ( ) *2,
?
reWindow . Height () * 2,
SWP_NOMOVE |SWP_NOZORDER )
21如何單擊除了窗口標(biāo)題欄以外的區(qū)域使窗口移動(dòng)?
當(dāng)窗口需要確定鼠標(biāo)位置時(shí)Windows向窗口發(fā)送WM_NCHITTEST信息,可以處理該信息使Windows認(rèn)為鼠標(biāo)在窗口標(biāo)題上。對(duì)于對(duì)話(huà)框和基于對(duì)話(huà)的應(yīng)用程序,可以使用ClassWizard處理該信息并調(diào)用基類(lèi)函數(shù), 如果函數(shù)返回HTCLIENT 則表明鼠標(biāo)在客房區(qū)域,返回HTCAPTION表明鼠標(biāo)在Windows的標(biāo)題欄中。
UINT CSampleDialog : : OnNcHitTest (Cpoint point )
{
UINT nHitTest =Cdialog: : OnNcHitTest (point )
return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest
}
?
上述技術(shù)有兩點(diǎn)不利之處, 其一是在窗口的客戶(hù)區(qū)域雙擊時(shí),窗口將極大; 其二, 它不適合包含幾個(gè)視窗的主框窗口。還有一種方法,當(dāng)用戶(hù)按下鼠標(biāo)左鍵使主框窗口認(rèn)為鼠標(biāo)在其窗口標(biāo)題上,使用ClassWizard在視窗中處理WM_LBUTTODOWN信息并向主框窗口發(fā)送一個(gè)WM_NCLBUTTONDOWN信息和一個(gè)單擊測(cè)試HTCAPTION。
void CSampleView : : OnLButtonDown (UINT nFlags , Cpoint point
)
{
CView : : OnLButtonDow (nFlags , pont )
?
//Fool frame window into thinking somene clicked
on
its caption bar .
GetParentFrame ( ) —> PostMessage (
WM_NCLBUTTONDOWN ,
HTCAPTION , MAKELPARAM (poitn .x , point .y) )
?
}
該技術(shù)也適用于對(duì)話(huà)框和基于對(duì)的應(yīng)用程序,只是不必調(diào)用 CWnd: :GetParentFrame 。
void CSampleDialog : : OnLbuttonDown (UINT nFlags, Cpoint point )
{
Cdialog : : OnLButtonDow (nFlags, goint )
//Fool dialog into thinking simeone clicked on its
caption bar .
PostMessage (WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARM (point.x
, point. y
) )
}
22 如何改變視窗的背景顏色?
Windows向窗口發(fā)送一個(gè)WM_ERASEBKGND消息通知該窗口擦除背景,可以使用ClassWizard重載該消息的缺省處理程序來(lái)擦除背景(實(shí)際是畫(huà)),并返回TRUE以防止Windows擦除窗口。
//Paint area that needs to be erased.
BOOL CSampleView : : OnEraseBkgnd (CDC* pDC)
{
// Create a pruple brush.
CBrush Brush (RGB (128 , 0 , 128) )
?
// Select the brush into the device context .
CBrush* pOldBrush = pDC—>SelcetObject (&brush)
?
// Get the area that needs to be erased .
CRect reClip
pDC—>GetCilpBox (&rcClip)
//Paint the area.
pDC—> PatBlt (rcClip.left , rcClip.top , rcClip.Width ( ) , rcClip.Height( ) , PATCOPY )
?
//Unselect brush out of device context .
pDC—>SelectObject (pOldBrush )
?
// Return nonzero to half fruther processing .
return TRUE
}
23 如何改變窗口標(biāo)題?
調(diào)用CWnd : : SetWindowText可以改變?nèi)魏未翱?#xff08;包括控件)的標(biāo)題。
//Set title for application's main frame window .
AfxGetMainWnd ( ) —> SetWindowText (_T("Application title") )
//Set title for View's MDI child frame window .
GetParentFrame ( ) —> SetWindowText ("_T ("MDI Child Frame new title")
)
//Set title for dialog's push button control.
GetDigitem (IDC_BUTTON) —> SetWindowText (_T ("Button new title ") )
?
如果需要經(jīng)常修改窗口的標(biāo)題(注:控件也是窗口),應(yīng)該考慮使用半文檔化的函數(shù)AfxSetWindowText。該函數(shù)在A(yíng)FXPRIV.H中說(shuō)明,在WINUTIL.CPP中實(shí)現(xiàn),在聯(lián)機(jī)幫助中找不到它,它在A(yíng)FXPRIV.H中半文檔化, 在以后發(fā)行的MFC中將文檔化。 AfxSetWindowText的實(shí)現(xiàn)如下:
voik AFXAPI AfxSetWindowText (HWND hWndCtrl , LPCTSTR IpszNew )
{
itn nNewLen= Istrlen (Ipaznew)
TCHAR szOld [256]
//fast check to see if text really changes (reduces
flash in the
controls )
if (nNewLen >_contof (szOld)
|| : : GetWindowText (hWndCrtl, szOld , _countof (szOld) !=nNewLen
|| Istrcmp (szOld , IpszNew)! = 0
{
//change it
: : SetWindowText(hWndCtrl , IpszNew )
}
}
24 如何防止主框窗口在其說(shuō)明中顯示活動(dòng)的文檔名
創(chuàng)建主框窗口和MDI子窗口進(jìn)通常具有FWS_ADDTOTITLE風(fēng)格位,如果不希望在說(shuō)明中自動(dòng)添加文檔名, 必須禁止該風(fēng)格位, 可以使用ClassWizard重置
CWnd: : PreCreateWindow并關(guān)閉FWS_ADDTOTITLE風(fēng)格。
BOOL CMainFrame : : PreCreateWindow (CREATESTRUCT&cs)
{
//Turn off FWS_ADDTOTITLE in main frame .
cs.styel & = ~FWS_ADDTOTITLE
return CMDIFrameWnd : : PreCreateWindow (cs )
}
關(guān)閉MDI子窗口的FWS _ADDTOTITLE風(fēng)格將創(chuàng)建一個(gè)具有空標(biāo)題的窗口,可以調(diào)用CWnd: : SetWindowText來(lái)設(shè)置標(biāo)題。記住自己設(shè)置標(biāo)題時(shí)要遵循接口風(fēng)格指南。
25 如何獲取有關(guān)窗口正在處理的當(dāng)前消息的信息?
調(diào)用CWnd: : GetCurrentMessage可以獲取一個(gè)MSG指針。例如,可以使用ClassWizard將幾個(gè)菜單項(xiàng)處理程序映射到一個(gè)函數(shù)中,然后調(diào)用GetCurrentMessage來(lái)確定所選中的菜單項(xiàng)。
viod CMainFrame : : OnCommmonMenuHandler ( )
{
//Display selected menu item in debug window .
TRACE ("Menu item %u was selected . \n" ,GetCruuentMessage ( ) —> wParam )
}
26 如何創(chuàng)建一個(gè)不規(guī)則形狀的窗口 ?
可以使用新的SDK函數(shù)SetWindowRgn。該函數(shù)將繪畫(huà)和鼠標(biāo)消息限定在窗口的一個(gè)指定的區(qū)域,實(shí)際上使窗口成為指定的不規(guī)則形狀。使用AppWizard創(chuàng)建一個(gè)基于對(duì)的應(yīng)用程序并使用資源編輯器從主對(duì)話(huà)資源中刪除所在的缺省控件、標(biāo)題以及邊界。給對(duì)話(huà)類(lèi)增加一個(gè)CRgn數(shù)據(jù)成員,以后要使用該數(shù)據(jù)成員建立窗口區(qū)域。
Class CRoundDlg : public CDialog
{
…
private :
Crgn m_rgn : // window region
…
}
修改OnInitDialog函數(shù)建立一個(gè)橢圓區(qū)域并調(diào)用SetWindowRgn將該區(qū)域分配給窗口:
BOOL CRoundDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
?
//Get size of dialog .
CRect rcDialog
GetClientRect (rcDialog )
?
// Create region and assign to window .
m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width( ) , rcDialog.Height ( ) )
SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn ,TRUE )
?
return TRUE
}
通過(guò)建立區(qū)域和調(diào)用SetWindowRgn,已經(jīng)建立一個(gè)不規(guī)則形狀的窗口,下面的例子程序是修改OnPaint函數(shù)使窗口形狀看起來(lái)象一個(gè)球形體。
voik CRoundDlg : : OnPaint ( )
{
CPaintDC de (this) // device context for painting
.
//draw ellipse with out any border
dc. SelecStockObject (NULL_PEN)
//get the RGB colour components of the sphere color
COLORREF color= RGB( 0 , 0 , 255)
BYTE byRed =GetRValue (color)
BYTE byGreen = GetGValue (color)
BYTE byBlue = GetBValue (color)
?
// get the size of the view window
Crect rect
GetClientRect (rect)
?
// get minimun number of units
int nUnits =min (rect.right , rect.bottom )
?
//calculate he horiaontal and vertical step size
float fltStepHorz = (float) rect.right /nUnits
float fltStepVert = (float) rect.bottom /nUnits
int nEllipse = nUnits/3 // calculate how many to
draw
int nIndex
// current ellipse that is being draw
?
CBrush brush
// bursh used for ellipse fill color
CBrush *pBrushOld // previous
brush that was selected into dc
//draw ellipse , gradually moving towards upper-right
corner
for (nIndex = 0 nIndes < + nEllipse nIndes++)
{
//creat solid brush
brush . CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ).
( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue)
/nEllipse ) ) )
?
//select brush into dc
pBrushOld= dc .SelectObject (&brhsh)
?
//draw ellipse
dc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex ,
rect. right -( (int) fltStepHorz * nIndex )+ 1,
rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1)
?
//delete the brush
brush.DelecteObject ( )
}
}
最后,處理WM_NCHITTEST消息,使當(dāng)擊打窗口的任何位置時(shí)能移動(dòng)窗口。
UINT CRoundDlg : : OnNchitTest (Cpoint point )
{
//Let user move window by clickign anywhere on thewindow .
UINT nHitTest = CDialog : : OnNcHitTest (point)
rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest
?
}
27 如何在代碼中獲取工具條和狀態(tài)條的指針?
缺省時(shí),工作框創(chuàng)建狀態(tài)條和工具條時(shí)將它們作為主框窗口的子窗口,狀態(tài)條有一個(gè)AFX_IDW_STATUS_BAR標(biāo)識(shí)符,工具條有一個(gè) AFX_IDW_TOOLBAR標(biāo)識(shí)符,下例說(shuō)明了如何通過(guò)一起調(diào)用CWnd: : GetDescendantWindow和AfxGetMainWnd來(lái)獲取這些子窗口的指針:
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
->GetDescendantWindow(AFX_IDW_STUTUS_BAR)
//Get pointer to toolbar .
CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( )
->GetDescendantWindow(AFX_IDW_TOOLBAR)?
?
========
VC編程中20種各種編程技巧和方法
http://blog.csdn.net/worldy/article/details/13771607
1. ? ?如何激活當(dāng)前屏幕保護(hù)程序
2. ? ?如何禁止/啟用屏幕保護(hù)及電源管理
3. ? ?如何激活和關(guān)閉IE瀏覽器
4. ? ?如何給樹(shù)控件加入工具提示
5. ? ?如何獲取系統(tǒng)信息框的路徑
6. ? ?如何直接運(yùn)行一個(gè)資源中的程序
7. ? ?如何遍歷整個(gè)目錄
8. ? ?如何禁止/啟用系統(tǒng)熱鍵
9. ? ?如何隱藏/顯示W(wǎng)INDOWS系統(tǒng)任務(wù)欄
10. ? ?如何實(shí)現(xiàn)窗口到系統(tǒng)區(qū)圖標(biāo)間的動(dòng)畫(huà)效果
11. ? ?如何判斷當(dāng)前操作系統(tǒng)的版本
12. ? ?如何在指定矩形框內(nèi)水平/垂直顯示多行文字
13. ? ?如何在指定矩形中旋轉(zhuǎn)顯示文字
14. ? ?如何將32 x 32像素圖標(biāo)轉(zhuǎn)換為16 x 16像素值的圖標(biāo)
15. ? ?如何建立一個(gè)灰度級(jí)圖標(biāo)
16. ? ?如何按指定角度旋轉(zhuǎn)顯示內(nèi)存位圖(用法和BitBlt類(lèi)似)
17. ? ?如何將指定的窗體,以位圖形式復(fù)制到系統(tǒng)剪切板上
18. ? ?如何替換HBITMAP中的顏色值
19. ? ?如何轉(zhuǎn)換并保存位圖
20. ? ?如何獲取局域網(wǎng)上計(jì)算機(jī)名及它們的IP地址
1. ? ?如何激活當(dāng)前屏幕保護(hù)程序
// 激活當(dāng)前屏幕保護(hù)程序, jingzhou xu
? ? PostMessage(WM_SYSCOMMAND,SC_SCREENSAVE,0);
2. ? ?如何禁止/啟用屏幕保護(hù)及電源管理
static UINT dss_GetList[] = {SPI_GETLOWPOWERTIMEOUT, SPI_GETPOWEROFFTIMEOUT, S
PI_GETSCREENSAVETIMEOUT};
static UINT dss_SetList[] = {SPI_SETLOWPOWERTIMEOUT, SPI_SETPOWEROFFTIMEOUT, S
PI_SETSCREENSAVETIMEOUT};
static const int dss_ListCount = _countof(dss_GetList);
l ? ?禁止屏幕保護(hù)及電源管理
{
m_pValue = new int[dss_ListCount];
for (int x=0;x<dss_ListCount;x++)
{
// 禁止屏幕保護(hù)及電源管理
VERIFY(SystemParametersInfo (dss_SetList[x], 0, NULL, 0));
}
delete[] m_pValue;
}
l ? ?啟用屏幕保護(hù)及電源管理
{
m_pValue = new int[dss_ListCount];
for (int x=0;x<dss_ListCount;x++)
{
//啟用屏幕保護(hù)及電源管理
VERIFY(SystemParametersInfo (dss_SetList[x], m_pValue[x], NULL, 0));
}
delete[] m_pValue;
}
3. ? ?如何激活和關(guān)閉IE瀏覽器
//激活并打開(kāi)IE
void lounchIE()
{
? HWND h=FindWindowEx(NULL,NULL,NULL,
? ? ? ? ? ? ? ? ? ? ? "Microsoft Internet Explorer") ;
? ShellExecute(h,"open","C:\simple.html",
? ? ? ? ? ? ? ?NULL,NULL,SW_SHOWNORMAL);
}
//關(guān)閉IE及其它應(yīng)用
void CloseIE()
{
? int app=BSM_APPLICATIONS;
? unsigned long ?bsm_app=(unsigned long )app;
? BroadcastSystemMessage(BSF_POSTMESSAGE,&bsm_app,
? ? ? ? ? ? ? ? ? ? ? ? ?WM_CLOSE,NULL,NULL);
}
4. ? ?如何給樹(shù)控件加入工具提示
l ? ?首先給樹(shù)控件加入TVS_INFOTIP屬性風(fēng)格,如下所示:
if (!m_ctrlTree.Create(WS_CHILD|WS_VISIBLE|
? ? TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT|TVS_SHOWSELALWAYS|TVS_INFOTIP,
?//加入提示TVS_INFOTIP,jingzhou xu(樹(shù)控件ID:100)
? ? ? ? CRect(0, 0, 0, 0), &m_wndTreeBar, 100))
? ? {
? ? ? ? TRACE0("Failed to create instant bar child\n");
? ? ? ? return -1;
? ? }
l ? ?其次加入映射消息聲明,如下所示:
afx_msg void OnGetInfoTip(NMHDR* pNMHDR,LRESULT* pResult); ? ? ? //樹(shù)控件上加入
提示消息,jingzhou xu ??
ON_NOTIFY(TVN_GETINFOTIP, 100, OnGetInfoTip) ? ? ? ? ? ? ? ? ?//樹(shù)控件條目上加
入提示,jingzhou xu
l ? ?最后加入呼應(yīng)涵數(shù)處理:
void CCreateTreeDlg::OnGetInfoTip(NMHDR* pNMHDR,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LRESULT* pResult)?
? {
? *pResult = 0;
? NMTVGETINFOTIP* pTVTipInfo = (NMTVGETINFOTIP*)pNMHDR;
? LPARAM itemData = (DWORD) pTVTipInfo->lParam;
? //對(duì)應(yīng)每個(gè)條目的數(shù)據(jù)
? HTREEITEM hItem = pTVTipInfo->hItem;
? CString tip;
? HTREEITEM hRootItem = m_chassisTree.GetRootItem();
? if (hRootItem != pTVTipInfo->hItem)
? {
? ? tip = "樹(shù)結(jié)點(diǎn)的提示";
? }
? else
? {
? ? tip = "樹(shù)根上的提示";
? }
? strcpy(pTVTipInfo->pszText, (LPCTSTR) tip);
}
5. ? ?如何獲取系統(tǒng)信息框的路徑
#include <atlbase.h>
#define IDS_REG_KEY_MSINFO_PATH1 _T( "Software\Microsoft\Shared Tools\MSInfo"?
)
#define IDS_REG_KEY_MSINFO_PATH2 _T( "Software\Microsoft\Shared Tools Location
" )
#define IDS_REG_VAL_MSINFO_PATH1 _T( "Path" )
#define IDS_REG_VAL_MSINFO_PATH2 _T( "MSInfo" )
#define IDS_MSINFO_EXE_NAME ? ? ?_T( "MSInfo32.exe" )
//...
BOOL GetSysInfoPath( CString& strPath )
{ ? ? ? ?
? ? strPath.Empty();
? ? LPTSTR ?pszPath = strPath.GetBuffer( MAX_PATH );
? ? ? ??
? ? CRegKey reg;
? ? DWORD ? dwSize ?= MAX_PATH;
? ? LONG ? ?nRet ? ?= reg.Open( HKEY_LOCAL_MACHINE, IDS_REG_KEY_MSINFO_PATH1,?
KEY_READ );
? ? ? ? ? ? ? ??
? ? // 在注冊(cè)表中尋找第一個(gè)"MSInfo32.exe" 位置
? ? if ( nRet == ERROR_SUCCESS )
? ? {
? ? ? ? #if ( _MFC_VER >= 0x0700 )
? ? ? ? ? ? nRet = reg.QueryStringValue( IDS_REG_VAL_MSINFO_PATH1, pszPath, &d
wSize );
? ? ? ? #else
? ? ? ? ? ? nRet = reg.QueryValue( pszPath, IDS_REG_VAL_MSINFO_PATH1, &dwSize?
);
? ? ? ? #endif
? ? ? ? reg.Close();
? ? }
? ??
? ? // 如果第一次尋找失敗,則進(jìn)行第二次尋找
? ? if ( nRet != ERROR_SUCCESS )
? ? {
? ? ? ? nRet = reg.Open( HKEY_LOCAL_MACHINE, IDS_REG_KEY_MSINFO_PATH2, KEY_REA
D );
? ? ? ? if ( nRet == ERROR_SUCCESS )
? ? ? ? {
? ? ? ? ? ? #if ( _MFC_VER >= 0x0700 )
? ? ? ? ? ? ? ? reg.QueryStringValue( IDS_REG_VAL_MSINFO_PATH2, pszPath, &dwSi
ze );
? ? ? ? ? ? #else
? ? ? ? ? ? ? ? reg.QueryValue( pszPath, IDS_REG_VAL_MSINFO_PATH2, &dwSize );
? ? ? ? ? ? #endif
? ? ? ? ? ??
? ? ? ? ? ? // 路徑名不包括EXE文件名
? ? ? ? ? ? if ( nRet == ERROR_SUCCESS )
? ? ? ? ? ? ? ? VERIFY( ::PathAppend( pszPath, IDS_MSINFO_EXE_NAME ) );
? ? ? ? ? ? reg.Close();
? ? ? ? }
? ? }
? ? ? ??
? ? strPath.ReleaseBuffer();
? ? strPath.FreeExtra();
? ??
? ? // 檢查文件是否有效. ? ?
? ? return ::PathFileExists( strPath );
}
6. ? ?如何直接運(yùn)行一個(gè)資源中的程序
bool Run()
? ?{
? ? CFile f;?
? ? char* pFileName = "Execution.exe";
? ? if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, NULL ) )
? ? {
? ? ? ? AfxMessageBox("Can not create file!");
? ? ? ? return 0;
? ? }
? ? ? ? CString path = f.GetFilePath();
? ? HGLOBAL hRes;
? ? HRSRC hResInfo;
? ? ?//獲取應(yīng)用實(shí)例 ?
? ? HINSTANCE insApp = AfxGetInstanceHandle();
? ? ?//尋找EXE資源名
? ? hResInfo = FindResource(insApp,(LPCSTR)IDR_EXE4,"EXE");
? ? hRes = LoadResource(insApp,hResInfo ); ? // Load it
? ? DWORD dFileLength = SizeofResource( insApp, hResInfo ); ?//計(jì)算EXE文件大小
?
? ? f.WriteHuge((LPSTR)hRes,dFileLength); ?//寫(xiě)入臨時(shí)文件?
? ? f.Close();
? ? HINSTANCE HINSsd = ShellExecute(NULL, "open",path, NULL, NULL, SW_SHOWNORM
AL);> //運(yùn)行它. ?
? ? return 1;
}
7. ? ?如何遍歷整個(gè)目錄
#include <windows.h>
#include <shlobj.h>
//瀏覽目錄.
void ? ?BrowseFolder( void )
{
? ? TCHAR path[MAX_PATH];
? ? BROWSEINFO bi = { 0 };
? ? bi.lpszTitle = ("遞歸調(diào)用所有目錄");
? ? LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
? ??
? ? if ( pidl != 0 )
? ? {
? ? ? ? // 獲取目錄路徑
? ? ? ? SHGetPathFromIDList ( pidl, path );
? ? ? ? //設(shè)置為當(dāng)前路徑
? ? ? ? SetCurrentDirectory ( path );
? ??
? ? ? ? //搜索所有子目錄
? ? ? ? SearchFolder( path );
? ? ? ? // 釋放內(nèi)存
? ? ? ? IMalloc * imalloc = 0;
? ? ? ? if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
? ? ? ? {
? ? ? ? ? ? imalloc->Free ( pidl );
? ? ? ? ? ? imalloc->Release ( );
? ? ? ? }
}
//搜索其下所有子目錄及文件.
void ? ?SearchFolder( TCHAR * path )
{
? ? ? ? WIN32_FIND_DATA FindFileData;
? ? ? ? HANDLE ? ? ? ? ? ?hFind;
? ? ? ? TCHAR ? ?filename[ MAX_PATH + 256 ];
? ? ? ? TCHAR ? ?pathbak[ MAX_PATH ];
? ? ? ? //復(fù)制初始用戶(hù)選擇目錄
? ? ? ? strcpy( pathbak, path );
? ? ? ? //尋找第一個(gè)文件
? ? ? ? hFind = FindFirstFile ( "*.*", &FindFileData );
? ? ? ? //搜索所有文件及子目錄
? ? ? ? do
? ? ? ? {
? ? ? ? ? ? if ( hFind != INVALID_HANDLE_VALUE )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //如果是當(dāng)前目錄或父目錄,跳過(guò)
? ? ? ? ? ? ? ? if ( ! ( strcmp( FindFileData.cFileName, "." ) ) || ! ( strcmp
( FindFileData.cFileName, ".." ) ) )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //恢復(fù)初始用戶(hù)選擇目錄
? ? ? ? ? ? ? ? strcpy( path, pathbak );
? ? ? ? ? ? ? ? //列出所有發(fā)現(xiàn)的文件
? ? ? ? ? ? ? ? sprintf( path, "%s\%s", path, FindFileData.cFileName );
? ? ? ? ? ? ? ? //如果 SetCurrentDirectory 成功的話(huà),則它是一個(gè)目錄,遞歸調(diào)用繼
續(xù)搜索子目錄
? ? ? ? ? ? ? ? if ( ( SetCurrentDirectory( path ) ) )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? SearchFolder( path );
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? //插入文件及路徑名到列表框m_listbox_hwnd中
? ? ? ? ? ? ? ? ? ?SendMessage( m_listbox_hwnd, LB_ADDSTRING, 0, path ); //<--
INSERT WHAT YOU WANT DONE HERE!
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? while ( FindNextFile ( hFind, &FindFileData ) && hFind != INVALID_HAND
LE_VALUE );
? ? ? ? FindClose ( hFind );
}
8. ? ?如何禁止/啟用系統(tǒng)熱鍵
bool bOld;
l ? ?禁止系統(tǒng)熱鍵
//屏蔽掉系統(tǒng)鍵
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&bOld,SPIF_UPDATEINIFILE);
l ? ?啟用系統(tǒng)熱鍵
//恢復(fù)系統(tǒng)熱鍵 ? ?
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&bOld,SPIF_UPDATEINIFILE)
;
9. ? ?如何隱藏/顯示W(wǎng)INDOWS系統(tǒng)任務(wù)欄
l ? ?隱藏系統(tǒng)任務(wù)欄
//隱藏WINDOWS系統(tǒng)任務(wù)欄
? ? ::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);
l ? ?顯示系統(tǒng)任務(wù)欄
//恢復(fù)WINDOWS系統(tǒng)任務(wù)欄正常顯示
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
10. ? ?如何實(shí)現(xiàn)窗口到系統(tǒng)區(qū)圖標(biāo)間的動(dòng)畫(huà)效果
//****************************************************************************
****
//* 名稱(chēng):FindTrayWnd
//* 作者:徐景周(jingzhou_xu@163.NET)
//* 功能:在顯示窗體動(dòng)畫(huà)效果前,先尋找系統(tǒng)區(qū)位置
//****************************************************************************
****
? ? BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
{
? ? TCHAR szClassName[256];
? ? GetClassName(hwnd, szClassName, 255);
? ? // 比較窗口類(lèi)名
? ? if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
? ? {
? ? ? ? CRect *pRect = (CRect*) lParam;
? ? ? ? ::GetWindowRect(hwnd, pRect);
? ? ? ? return TRUE;
? ? }
? ? // 當(dāng)找到時(shí)鐘窗口時(shí)表示可以結(jié)束了
? ? if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
? ? {
? ? ? ? CRect *pRect = (CRect*) lParam;
? ? ? ? CRect rectClock;
? ? ? ? ::GetWindowRect(hwnd, rectClock);
? ? ? ? pRect->right = rectClock.left;
? ? ? ? return FALSE;
? ? }
? ? return TRUE;
}
//****************************************************************************
****
//* 名稱(chēng):WinAnimation
//* 作者:徐景周(jingzhou_xu@163.Net)
//* 功能:顯示窗口動(dòng)畫(huà)效果的涵數(shù)
//****************************************************************************
****
void CScreenSnapDlg::WinAnimation(BOOL ShowFlag)?
{
? ? CRect rect(0,0,0,0);
? ? // 查找托盤(pán)窗口?
? ? CWnd* pWnd = FindWindow("Shell_TrayWnd", NULL);
? ? if (pWnd)
? ? {
? ? ? ? pWnd->GetWindowRect(rect);
? ? ? ? EnumChildWindows(pWnd->m_hWnd, FindTrayWnd, (LPARAM)&rect);
? ? ? ? //rect 為托盤(pán)區(qū)矩形
? ? ? ? CRect rcWnd;
? ? ? ? GetWindowRect(rcWnd);
? ? ? ? if(ShowFlag) ? ? ? ? ? ? ? ? ? ?//窗體滑向系統(tǒng)區(qū)
? ? ? ? ? DrawAnimatedRects(GetSafeHwnd(),IDANI_CAPTION,rcWnd,rect);
? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ?//窗體從系統(tǒng)區(qū)滑出
? ? ? ? ? DrawAnimatedRects(GetSafeHwnd(),IDANI_CAPTION,rect,rcWnd);
? ? }
}
用法如下:
if(IsWindowVisible()) ? ? ? ? ? ? ? ? //窗體是否已隱藏
{
? ? ShowWindow(SW_HIDE); ? ? ? ? ? //先隱藏窗體
? ? WinAnimation(true); ? ? ? ? ? ? ? ? //窗體動(dòng)畫(huà)滑入到系統(tǒng)區(qū)中?
}
else
{
WinAnimation(false); ? ? ? ? //窗體動(dòng)畫(huà)從系統(tǒng)區(qū)滑出?
? ? ShowWindow(SW_SHOW);
}
11. ? ?如何判斷當(dāng)前操作系統(tǒng)的版本
//----------------------------------------------------------------------------
--------------------
//判斷操作系統(tǒng)涵數(shù)及變量,jingzhou xu
typedef enum tagWin32SysType{
? ? Windows32s,
? ? WindowsNT3,
? ? Windows95,
? ? Windows98,
? ? WindowsME,
? ? WindowsNT4,
? ? Windows2000,
? ? WindowsXP
}Win32SysType;
//判斷操作系統(tǒng)涵數(shù)及變量,jingzhou xu
Win32SysType IsShellSysType()
{
? ? Win32SysType ?ShellType;
? ? DWORD winVer;
? ? OSVERSIONINFO *osvi;
? ??
? ? winVer=GetVersion();
? ? if(winVer<0x80000000){/*NT */
? ? ? ? ShellType=WindowsNT3;
? ? ? ? osvi= (OSVERSIONINFO *)malloc(sizeof(OSVERSIONINFO));
? ? ? ? if (osvi!=NULL){
? ? ? ? ? ? memset(osvi,0,sizeof(OSVERSIONINFO));
? ? ? ? ? ? osvi->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
? ? ? ? ? ? GetVersionEx(osvi);
? ? ? ? ? ? if(osvi->dwMajorVersion==4L)ShellType=WindowsNT4;
? ? ? ? ? ? else if(osvi->dwMajorVersion==5L&&osvi->dwMinorVersion==0L)ShellTy
pe=Windows2000;
? ? ? ? ? ? else if(osvi->dwMajorVersion==5L&&osvi->dwMinorVersion==1L)ShellTy
pe=WindowsXP;
? ? ? ? ? ? free(osvi);
? ? ? ? }
? ? }
? ? else if ?(LOBYTE(LOWORD(winVer))<4)
? ? ? ? ShellType=Windows32s;
? ? else{
? ? ? ? ShellType=Windows95;
? ? ? ? osvi= (OSVERSIONINFO *)malloc(sizeof(OSVERSIONINFO));
? ? ? ? if (osvi!=NULL){
? ? ? ? ? ? memset(osvi,0,sizeof(OSVERSIONINFO));
? ? ? ? ? ? osvi->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
? ? ? ? ? ? GetVersionEx(osvi);
? ? ? ? ? ? if(osvi->dwMajorVersion==4L&&osvi->dwMinorVersion==10L)ShellType=W
indows98;
? ? ? ? ? ? else if(osvi->dwMajorVersion==4L&&osvi->dwMinorVersion==90L)ShellT
ype=WindowsME;
? ? ? ? ? ? free(osvi);
? ? ? ? }
? ? }
? ? return ShellType;
}
//----------------------------------------------------------------------------
--------------------
12. ? ?如何在指定矩形框內(nèi)水平/垂直顯示多行文字
///
//說(shuō)明:
// ?在矩形框中水平或垂直顯示多行文字,jingzhou xu.
// ?lMode: 排列方式,0:水平方式; 1:垂直對(duì)齊 ? ?
// ?lHori: 水平對(duì)齊方式, 0:左對(duì)齊; 1:居中; 2:右對(duì)齊; 3:自定義
// ?lVert: 垂直對(duì)齊方式, 0:頂對(duì)齊; 1:居中; 2:底對(duì)齊; 3:自定義
///
CRect DrawTitleInRect(CDC *pDC, CString szString, LPRECT lpRect, long lMode, l
ong lHori, long lVert)
{
? ? TEXTMETRIC tm;
? ? pDC->GetTextMetrics(&tm);
? ? int tmpWidth=tm.tmAveCharWidth, tmpHeight=tm.tmHeight;
? ? CRect rcInner(lpRect);
? ? if(lMode==0)
? ? {
? ? ? ? rcInner.left+=tmpWidth;
? ? ? ? rcInner.right-=tmpWidth;
? ? ? ? rcInner.top-=tmpWidth;
? ? ? ? rcInner.bottom+=tmpWidth;
? ? }
? ? if(lMode==1)
? ? {
? ? ? ? rcInner.left+=tmpWidth;
? ? ? ? rcInner.right=rcInner.left+tmpWidth;
? ? ? ? rcInner.top-=tmpWidth;
? ? ? ? rcInner.bottom+=tmpWidth;
? ? }
? ? pDC->DrawText(szString, rcInner,DT_CALCRECT);
? ? switch(lHori)
? ? {
? ? case 0:
? ? ? ? break;
? ? case 1:
? ? ? ? {
? ? ? ? ? ? long xOutCent=(lpRect->right+lpRect->left)/2;
? ? ? ? ? ? long xInnCent=(rcInner.right+rcInner.left)/2;
? ? ? ? ? ? rcInner.left+=(xOutCent-xInnCent);
? ? ? ? ? ? rcInner.right+=(xOutCent-xInnCent);
? ? ? ? }
? ? ? ? break;
? ? case 2:
? ? ? ? {
? ? ? ? ? ? long lInWidth=rcInner.right-rcInner.left;
? ? ? ? ? ? rcInner.right=lpRect->right-tmpWidth;
? ? ? ? ? ? rcInner.left=rcInner.right-lInWidth;
? ? ? ? }
? ? ? ? break;
? ? default:
? ? ? ? break;
? ? }
? ??
? ? switch(lVert)
? ? {
? ? case 0:
? ? ? ? break;
? ? case 1:
? ? ? ? {
? ? ? ? ? ? long yOutCent=(lpRect->bottom+lpRect->top)/2;
? ? ? ? ? ? long yInnCent=(rcInner.bottom+rcInner.top)/2;
? ? ? ? ? ? rcInner.top-=(yInnCent-yOutCent);
? ? ? ? ? ? rcInner.bottom-=(yInnCent-yOutCent);
? ? ? ? }
? ? ? ? break;
? ? case 2:
? ? ? ? {
? ? ? ? ? ? long lInHeigh=rcInner.top-rcInner.bottom;
? ? ? ? ? ? rcInner.bottom=lpRect->bottom+tmpWidth;
? ? ? ? ? ? rcInner.top=rcInner.bottom+lInHeigh;
? ? ? ? }
? ? ? ? break;
? ? default:
? ? ? ? break;
? ? }
? ? //------------------------------------------------------------------------
---------------------
? ? //功能:根據(jù)新、老矩形,重新計(jì)算行數(shù),使文字多行顯示,jingzhou xu
? ? //------------------------------------------------------------------------
---------------------
? ? //一行中最大字符數(shù)
? ? int nMaxLineChar = abs(lpRect->right - lpRect->left) / tmpWidth ; ? ? ??
? ? //記錄當(dāng)前行的寬度
? ? short theLineLength=0;?
? ? //記錄當(dāng)前行中漢字字節(jié)數(shù),以防止將一半漢字分為兩行
? ? unsigned short halfChinese=0;
? ? for(int i=0; i<=szString.GetLength()-1; i++)
? ? {
? ? ? ? if(((unsigned char)szString.GetAt(i) == 0x0d) && ((unsigned char)szStr
ing.GetAt(i+1) == 0x0a))
? ? ? ? ? ? theLineLength=0;
? ? ? ? //大于0xa1的字節(jié)為漢字字節(jié)
? ? ? ? if((unsigned char)szString.GetAt(i) >= 0xA1)
? ? ? ? ? ? halfChinese++;
? ? ? ? theLineLength++;
? ? ? ? //如果行寬大于每行最大寬度,進(jìn)行特殊處理
? ? ? ? if(theLineLength > nMaxLineChar)
? ? ? ? {
? ? ? ? ? ? //防止將一個(gè)漢字分為兩行,回溯
? ? ? ? ? ? if(halfChinese%2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? szString.Insert(i,(unsigned char)0x0a);
? ? ? ? ? ? ? ? szString.Insert(i,(unsigned char)0x0d);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? szString.Insert(i-1,(unsigned char)0x0a);
? ? ? ? ? ? ? ? szString.Insert(i-1,(unsigned char)0x0d);
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? theLineLength = 0;
? ? ? ? }
? ? }
? ? //重新計(jì)算矩形邊界范圍
// ? ?int tmpLine = int(abs(szString.GetLength()*tmpWidth / abs(lpRect->right?
- lpRect->left)-0.5));
// ? ?tmpLine += (szString.GetLength()*tmpWidth % abs(lpRect->right - lpRect->
left))? 1 : 0;
// ? ?if(tmpLine == 0)
// ? ? ? ?tmpLine = 1;
? ? if(rcInner.bottom > lpRect->bottom)
? ? ? ? rcInner.bottom = lpRect->bottom;
? ? if(rcInner.top < lpRect->top)
? ? ? ? rcInner.top = lpRect->top;
? ? //------------------------------------------------------------------------
---------------------
? ? if(lHori==0)
? ? ? ? pDC->DrawText(szString, rcInner, DT_WORDBREAK|DT_LEFT);
? ? else if(lHori==1)
? ? ? ? pDC->DrawText(szString, rcInner, DT_WORDBREAK|DT_CENTER);
? ? else if(lHori==2)
? ? ? ? pDC->DrawText(szString, rcInner, DT_WORDBREAK|DT_RIGHT);
? ? return rcInner;
}
13. ? ?如何在指定矩形中旋轉(zhuǎn)顯示文字
///
//說(shuō)明:
// ?在矩形框中旋轉(zhuǎn)方式顯示文字,jingzhou xu
//參數(shù): ? ??
// ?pDC: ? ? ? ?DC指針
// ?str: ? ? ? ?顯示文字
// ?rect: ? ? ? ?顯示范圍
// ?angle: ? ? ? ?旋轉(zhuǎn)角度
// ? ?nOptions: ? ?ExtTextOut()中相應(yīng)設(shè)置<ETO_CLIPPED 和 ETO_OPAQUE>
///
void DrawRotatedText(CDC* pDC, const CString str, CRect rect,?
? ? ? ? ? ? ? ? ? ? ?double angle, UINT nOptions)
{
? ?//按比例轉(zhuǎn)換角度值
? ?double pi = 3.141592654;
? ?double radian = pi * 2 / 360 * angle;
? ?//獲取顯示文字中心點(diǎn)
? ?CSize TextSize = pDC->GetTextExtent(str);
? ?CPoint center;
? ?center.x = TextSize.cx / 2;
? ?center.y = TextSize.cy / 2;
? ?//計(jì)算顯示文字新的中心點(diǎn)
? ?CPoint rcenter;
? ?rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
? ?rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);
? ?//繪制文字
? ?pDC->SetTextAlign(TA_BASELINE);
? ?pDC->SetBkMode(TRANSPARENT);
? ?pDC->ExtTextOut(rect.left + rect.Width() / 2 - rcenter.x,?
? ? ? ? ? ? ? ? ? ?rect.top + rect.Height() / 2 + rcenter.y,
? ? ? ? ? ? ? ? ? ?nOptions, rect, str, NULL);
}
14. ? ?如何將32 x 32像素圖標(biāo)轉(zhuǎn)換為16 x 16像素值的圖標(biāo)
HICON Convert32x32IconTo16x16(HICON h32x32Icon)
{
? HDC hMainDC, hMemDC1, hMemDC2;
? HICON h16x16Icon;
? BITMAP bmp;
? HBITMAP hOldBmp1, hOldBmp2;
? ICONINFO IconInfo32x32, IconInfo16x16;
? GetIconInfo(h32x32Icon, &IconInfo32x32);
? hMainDC = ::GetDC(m_hWnd);
? hMemDC1 = CreateCompatibleDC(hMainDC);
? hMemDC2 = CreateCompatibleDC(hMainDC);
? GetObject(IconInfo32x32.hbmColor, sizeof(BITMAP), &bmp);
? IconInfo16x16.hbmColor = CreateBitmap( 16, 16,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?bmp.bmPlanes,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?bmp.bmBitsPixel,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NULL);
? hOldBmp1 = (HBITMAP) SelectObject( hMemDC1,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?IconInfo32x32.hbmColor);
? hOldBmp2 = (HBITMAP) SelectObject( hMemDC2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?IconInfo16x16.hbmColor);
? StretchBlt(hMemDC2,
? ? ? ?0, 0,
? ? ? ?16, 16,
? ? ? ?hMemDC1,
? ? ? ?0, 0,
? ? ? ?32, 32,
? ? ? ?SRCCOPY
? ? ? ?);
? GetObject(IconInfo32x32.hbmMask, sizeof(BITMAP), &bmp);
? IconInfo16x16.hbmMask = CreateBitmap( 16, 16,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bmp.bmPlanes,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bmp.bmBitsPixel,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NULL);
? SelectObject(hMemDC1, IconInfo32x32.hbmMask);
? SelectObject(hMemDC2, IconInfo16x16.hbmMask);
? StretchBlt(hMemDC2,
? ? ? ? ? ? ?0, 0,
? ? ? ? ? ? ?16, 16,
? ? ? ? ? ? ?hMemDC1,
? ? ? ? ? ? ?0, 0,
? ? ? ? ? ? ?32, 32,
? ? ? ? ? ? ?SRCCOPY
? ? ? ?);
? SelectObject(hMemDC1, hOldBmp1);
? SelectObject(hMemDC2, hOldBmp2);
? IconInfo16x16.fIcon = TRUE;
? h16x16Icon = CreateIconIndirect(&IconInfo16x16);
? DeleteObject(IconInfo32x32.hbmColor);
? DeleteObject(IconInfo16x16.hbmColor);
? DeleteObject(IconInfo32x32.hbmMask);
? DeleteObject(IconInfo16x16.hbmMask);
? DeleteDC(hMemDC1);
? DeleteDC(hMemDC2);
? ::ReleaseDC(m_hWnd, hMainDC);
? return h16x16Icon;
}
15. ? ?如何建立一個(gè)灰度級(jí)圖標(biāo)
HICON CreateGrayscaleIcon(HICON hIcon)
{
? HICON ? ? ? hGrayIcon = NULL;
? HDC ? ? ? ? hMainDC = NULL,?
? ? ? ? ? ? ? hMemDC1 = NULL,?
? ? ? ? ? ? ? hMemDC2 = NULL;
? BITMAP ? ? ?bmp;
? HBITMAP ? ? hOldBmp1 = NULL,
? ? ? ? ? ? ? hOldBmp2 = NULL;
? ICONINFO ? ?csII, csGrayII;
? BOOL ? ? ? ?bRetValue = FALSE;
? bRetValue = ::GetIconInfo(hIcon, &csII);
? if (bRetValue == FALSE) return NULL;
? hMainDC = ::GetDC(m_hWnd);
? hMemDC1 = ::CreateCompatibleDC(hMainDC);
? hMemDC2 = ::CreateCompatibleDC(hMainDC);
? if (hMainDC == NULL ||?
? ? hMemDC1 == NULL ||
? ? hMemDC2 == NULL)?
? ? ? return NULL;
? if (::GetObject(csII.hbmColor,?
? ? ? ? ? ? ? ? sizeof(BITMAP), &
? ? ? ? ? ? ? ? amp;bmp))
? {
? ? csGrayII.hbmColor =?
? ? ? ? ?::CreateBitmap(csII.xHotspot*2,
? ? ? ? ? ? ? ? ? ? ? ? csII.yHotspot*2,?
? ? ? ? ? ? ? ? ? ? ? ? bmp.bmPlanes,?
? ? ? ? ? ? ? ? ? ? ? ? bmp.bmBitsPixel,?
? ? ? ? ? ? ? ? ? ? ? ? NULL);
? ? if (csGrayII.hbmColor)
? ? {
? ? ? hOldBmp1 =
? ? ? ? ?(HBITMAP)::SelectObject(hMemDC1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?csII.hbmColor);
? ? ? hOldBmp2 =?
? ? ? ? ?(HBITMAP)::SelectObject(hMemDC2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?csGrayII.hbmColor);
? ? ? ::BitBlt(hMemDC2, 0, 0, csII.xHotspot*2,
? ? ? ? ? ? ? ?csII.yHotspot*2, hMemDC1, 0, 0,
? ? ? ? ? ? ? ?SRCCOPY);
? ? ? DWORD ? ?dwLoopY = 0, dwLoopX = 0;
? ? ? COLORREF crPixel = 0;
? ? ? BYTE ? ? byNewPixel = 0;
? ? ? for (dwLoopY = 0; dwLoopY < csII.yHotspot*2; dwLoopY++)
? ? ? {
? ? ? ? for (dwLoopX = 0; dwLoopX < csII.xHotspot*2; dwLoopX++)
? ? ? ? {
? ? ? ? ? crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
? ? ? ? ? byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) +
? ? ? ? ? ? ? ?(GetGValue(crPixel) * 0.587) +
? ? ? ? ? ? ? ?(GetBValue(crPixel) * 0.114));
? ? ? ? ? if (crPixel) ::SetPixel(hMemDC2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dwLoopX,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dwLoopY,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RGB(byNewPixel,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byNewPixel,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byNewPixel));
? ? ? ? } // for
? ? ? } // for
? ? ? ::SelectObject(hMemDC1, hOldBmp1);
? ? ? ::SelectObject(hMemDC2, hOldBmp2);
? ? ? csGrayII.hbmMask = csII.hbmMask;
? ? ? csGrayII.fIcon = TRUE;
? ? ? hGrayIcon = ::CreateIconIndirect(&csGrayII);
? ? } // if
? ? ::DeleteObject(csGrayII.hbmColor);
? ? //::DeleteObject(csGrayII.hbmMask);
? } // if
? ::DeleteObject(csII.hbmColor);
? ::DeleteObject(csII.hbmMask);
? ::DeleteDC(hMemDC1);
? ::DeleteDC(hMemDC2);
? ::ReleaseDC(m_hWnd, hMainDC);
? return hGrayIcon;
}
16. ? ?如何按指定角度旋轉(zhuǎn)顯示內(nèi)存位圖(用法和BitBlt類(lèi)似)
void RotBlt(HDC destDC, int srcx1, int srcy1, int srcx2, int srcy2,
? HDC srcDC , int destx1, int desty1 ,int thetaInDegrees ,DWORD mode)
{
? double theta = thetaInDegrees * (3.14159/180);
? //原圖像原始大小
? int width = srcx2 - srcx1;
? int height = srcy2 - srcy1;
? //原圖像中心點(diǎn)
? int centreX = int(float(srcx2 + srcx1)/2);
? int centreY = int(float(srcy2 + srcy1)/2);
? //判斷出圖像可以沿任意方向旋轉(zhuǎn)的矩形框
? if(width>height)height = width;
? else
? ? width = height;
? HDC memDC = CreateCompatibleDC(destDC);
? HBITMAP memBmp = CreateCompatibleBitmap(destDC, width, height);
? HBITMAP obmp = (HBITMAP) SelectObject(memDC, memBmp);
? //內(nèi)存DC新在中心點(diǎn)
? int newCentre = int(float(width)/2);
? //開(kāi)始旋轉(zhuǎn)
? for(int x = srcx1; x<=srcx2; x++)
? ? for(int y = srcy1; y<=srcy2; y++)
? ? {
? ? ? COLORREF col = GetPixel(srcDC,x,y);
? ? ? int newX = int((x-centreX)*sin(theta)+(y-centreY)*cos(theta));
? ? ? int newY = int((x-centreX)*cos(theta)-(y-centreY)*sin(theta));
? ? ? SetPixel(memDC , newX + newCentre, newY + newCentre, col);
? ? }
? //復(fù)制到目標(biāo)DC上
? BitBlt(destDC, destx1, desty1, width, height, memDC, 0,0,mode);
? //釋放內(nèi)存
? SelectObject(memDC, obmp);
? DeleteDC(memDC);
? DeleteObject(memBmp);
}
用法:
RotBlt(dc, 0,0,150,150,memDC,200,0, 45, SRCCOPY);
17. ? ?如何將指定的窗體,以位圖形式復(fù)制到系統(tǒng)剪切板上
void CScreenSnapDlg::toClipboard_Bio(CWnd * wnd, BOOL FullWnd)
{
? ? ?CDC *dc;
? ? ?if(FullWnd)
? ? ? ? { /* 抓取整個(gè)窗口 */
? ? ? ? ?dc = new CWindowDC(wnd);
? ? ? ? } /* 抓取整個(gè)窗口 */
? ? ?else
? ? ? ? { /* 僅抓取客戶(hù)區(qū)時(shí) */
? ? ? ? ?dc = new CClientDC(wnd);
? ? ? ? } /* 僅抓取客戶(hù)區(qū)時(shí) */
? ? ?CDC memDC;
? ? ?memDC.CreateCompatibleDC(dc);
? ? ?CBitmap bm;
? ? ?CRect r;
? ? ?if(FullWnd)
? ? ? ? wnd->GetWindowRect(&r);
? ? ?else
? ? ? ? ?wnd->GetClientRect(&r);
? ? ?CString s;
? ? ?wnd->GetWindowText(s);
? ? ?CSize sz(r.Width(), r.Height());
? ? ?bm.CreateCompatibleBitmap(dc, sz.cx, sz.cy);
? ? ?CBitmap * oldbm = memDC.SelectObject(&bm);
? ? ?memDC.BitBlt(0, 0, sz.cx, sz.cy, dc, 0, 0, SRCCOPY);
? ? ?//直接調(diào)用OpenClipboard(),而不用wnd->GetParent()->OpenClipboard();
? ? ?wnd->OpenClipboard();
? ? ?::EmptyClipboard();
? ? ?::SetClipboardData(CF_BITMAP, bm.m_hObject);
? ? ?CloseClipboard();
? ? ?//恢復(fù)原始環(huán)境
? ? ?memDC.SelectObject(oldbm);
? ? ?bm.Detach();?
? ? ?delete dc;
}
18. ? ?如何替換HBITMAP中的顏色值
#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) \
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ((Color << 16) & 0xff0000)
HBITMAP ReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor)
{
? ? HBITMAP RetBmp=NULL;
? ? if (hBmp)
? ? { ? ?
? ? ? ? HDC BufferDC=CreateCompatibleDC(NULL); ? ? ? ?// 源位圖DC
? ? ? ? if (BufferDC)
? ? ? ? {
? ? ? ? ? ? SelectObject(BufferDC,hBmp); ? ? ? ?// 選入DC中
? ? ? ? ? ??
? ? ? ? ? ? HDC DirectDC=CreateCompatibleDC(NULL); ? ? ?// 目標(biāo)DC
? ? ? ? ? ? if (DirectDC)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 獲取源位圖大小
? ? ? ? ? ? ? ? BITMAP bm;
? ? ? ? ? ? ? ? GetObject(hBmp, sizeof(bm), &bm);
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? // 初始化BITMAPINFO信息,以便使用CreateDIBSection
? ? ? ? ? ? ? ? BITMAPINFO RGB32BitsBITMAPINFO;?
? ? ? ? ? ? ? ? ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
? ? ? ? ? ? ? ? RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
? ? ? ? ? ? ? ? RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
? ? ? ? ? ? ? ? RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
? ? ? ? ? ? ? ? RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
? ? ? ? ? ? ? ? RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
? ? ? ? ? ? ? ? UINT * ptPixels; ??
? ? ? ? ? ? ? ? HBITMAP DirectBitmap= CreateDIBSection(DirectDC,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (BITMAPINFO *)&RGB32BitsBITMAPIN
FO,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? DIB_RGB_COLORS,(void **)&ptPixel
s, NULL, 0);
? ? ? ? ? ? ? ? if (DirectBitmap)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap
);
? ? ? ? ? ? ? ? ? ? BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SR
CCOPY);
? ? ? ? ? ? ? ? ? ? // 轉(zhuǎn)換 COLORREF 為 RGB
? ? ? ? ? ? ? ? ? ? cOldColor=COLORREF2RGB(cOldColor);
? ? ? ? ? ? ? ? ? ? cNewColor=COLORREF2RGB(cNewColor);
? ? ? ? ? ? ? ? ? ? // 替換顏色
? ? ? ? ? ? ? ? ? ? for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (ptPixels[i]==cOldColor) ptPixels[i]=cNewColor;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? // 修改位圖 DirectBitmap
? ? ? ? ? ? ? ? ? ? SelectObject(DirectDC,PreviousObject);
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? // 完成
? ? ? ? ? ? ? ? ? ? RetBmp=DirectBitmap;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 釋放DC
? ? ? ? ? ? ? ? DeleteDC(DirectDC);
? ? ? ? ? ? }
? ? ? ? ? ? // 釋放DC
? ? ? ? ? ? DeleteDC(BufferDC);
? ? ? ? }
? ? }
? ? return RetBmp;
}
用法:
HBITMAP hBmp2 = LoadBitmap(g_hinstance,MAKEINTRESOURCE(IDB_SAMPLEBITMAP));
HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // 替換藍(lán)色為綠色
......
DeleteObject(hBmp2);
DeleteObject(hBmp);
19. ? ?如何轉(zhuǎn)換并保存位圖
//****************************************************************************
****
//* 名稱(chēng):DDBToDIB
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:設(shè)備相關(guān)轉(zhuǎn)換為設(shè)備無(wú)關(guān)位圖
//****************************************************************************
****
HANDLE CScreenSnapDlg::DDBToDIB( CBitmap& bitmap, DWORD dwCompression /* = BI_
RGB */)?
{
? ? BITMAP ? ? ? ? ? ? ? ?bm;
? ? BITMAPINFOHEADER ? ?bi;
? ? LPBITMAPINFOHEADER ?lpbi;
? ? DWORD ? ? ? ? ? ? ? ?dwLen;
? ? HANDLE ? ? ? ? ? ? ? ?hDIB;
? ? HANDLE ? ? ? ? ? ? ? ?handle;
? ? HDC ? ? ? ? ? ? ? ? ? ?hDC;
? ? HPALETTE ? ? ? ? ? ?hPal;
? ? CWindowDC ? ? ? ? ? ?dc( this );
? ? CPalette ? ? ? ? ? ?pal;
? ? //如果支持調(diào)色板的話(huà),則建立它
? ? if( dc.GetDeviceCaps( RASTERCAPS ) & RC_PALETTE )
? ? {
? ? ? ? UINT ? ? ? ?nSize ? = sizeof(LOGPALETTE) + ( sizeof(PALETTEENTRY) * 25
6 );
? ? ? ? LOGPALETTE* pLP ? ? = (LOGPALETTE*)new BYTE[nSize];
? ? ? ? pLP->palVersion ? ? = 0x300;
? ? ? ? pLP->palNumEntries = (unsigned short)GetSystemPaletteEntries( dc, 0, 2
55,?
? ? ? ? pLP->palPalEntry );
? ? ? ? pal.CreatePalette( pLP );
? ? ? ? //釋放
? ? ? ? delete[] pLP;
? ? }
? ? ASSERT( bitmap.GetSafeHandle() );
? ? //不支持BI_BITFIELDS類(lèi)型
? ? if( dwCompression == BI_BITFIELDS )
? ? ? ? return NULL;
? ? //如果調(diào)色板為空,則用默認(rèn)調(diào)色板
? ? hPal = (HPALETTE) pal.GetSafeHandle();
? ? if (hPal==NULL)
? ? ? ? hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
? ? //獲取位圖信息
? ? bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
? ? //初始化位圖信息頭
? ? bi.biSize ? ? ? ?= sizeof(BITMAPINFOHEADER);
? ? bi.biWidth ? ? ? ?= bm.bmWidth;
? ? bi.biHeight ? ? ? ? = bm.bmHeight;
? ? bi.biPlanes ? ? ? ? = 1;
? ? bi.biBitCount ? ? ? ?= (unsigned short)(bm.bmPlanes * bm.bmBitsPixel) ;
? ? bi.biCompression ? ?= dwCompression;
? ? bi.biSizeImage ? ? ? ?= 0;
? ? bi.biXPelsPerMeter ? ?= 0;
? ? bi.biYPelsPerMeter ? ?= 0;
? ? bi.biClrUsed ? ? ? ?= 0;
? ? bi.biClrImportant ? ?= 0;
? ? //計(jì)算信息頭及顏色表大小
? ? int nColors = 0;
? ? if(bi.biBitCount <= 8)
? ? ? ? {
? ? ? ? nColors = (1 << bi.biBitCount);
? ? ? ? }
? ? dwLen ?= bi.biSize + nColors * sizeof(RGBQUAD);
? ? hDC = ::GetDC(NULL);
? ? hPal = SelectPalette(hDC,hPal,FALSE);
? ? RealizePalette(hDC);
? ? //為信息頭及顏色表分配內(nèi)存
? ? hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
? ? if (!hDIB){
? ? ? ? SelectPalette(hDC,hPal,FALSE);
? ? ? ? ::ReleaseDC(NULL,hDC);
? ? ? ? return NULL;
? ? }
? ? lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
? ? *lpbi = bi;
? ? //調(diào)用 GetDIBits 計(jì)算圖像大小
? ? GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
? ? ? ? ? ? (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
? ? bi = *lpbi;
? ? //圖像的每一行都對(duì)齊(32bit)邊界
? ? if (bi.biSizeImage == 0){
? ? ? ? bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)?
? ? ? ? ? ? ? ? ? ? ? ? * bi.biHeight;
? ? ? ? if (dwCompression != BI_RGB)
? ? ? ? ? ? bi.biSizeImage = (bi.biSizeImage * 3) / 2;
? ? }
? ? //重新分配內(nèi)存大小,以便放下所有數(shù)據(jù)
? ? dwLen += bi.biSizeImage;
? ? handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE) ;
? ? if (handle != NULL)
? ? ? ? hDIB = handle;
? ? else
? ? ? ? {
? ? ? ? GlobalFree(hDIB);
? ? ? ? //重選原始調(diào)色板
? ? ? ? SelectPalette(hDC,hPal,FALSE);
? ? ? ? ::ReleaseDC(NULL,hDC);
? ? ? ? return NULL;
? ? ? ? }
? ? //獲取位圖數(shù)據(jù)
? ? lpbi = (LPBITMAPINFOHEADER)hDIB;
? ? //最終獲得的DIB
? ? BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
? ? ? ? ? ? ? ? 0L, ? ? ? ? ? ? ? ? ? ? ?//掃描行起始處
? ? ? ? ? ? ? ? (DWORD)bi.biHeight, ? ? ?//掃描行數(shù)
? ? ? ? ? ? ? ? (LPBYTE)lpbi ? ? ? ? ? ? //位圖數(shù)據(jù)地址
? ? ? ? ? ? ? ? + (bi.biSize + nColors * sizeof(RGBQUAD)),
? ? ? ? ? ? ? ? (LPBITMAPINFO)lpbi, ? ? ?//位圖信息地址
? ? ? ? ? ? ? ? (DWORD)DIB_RGB_COLORS); ?//顏色板使用RGB
? ? if( !bGotBits )
? ? {
? ? ? ? GlobalFree(hDIB);
? ? ? ??
? ? ? ? SelectPalette(hDC,hPal,FALSE);
? ? ? ? ::ReleaseDC(NULL,hDC);
? ? ? ? return NULL;
? ? }
? ? SelectPalette(hDC,hPal,FALSE);
? ? ::ReleaseDC(NULL,hDC);
? ? return hDIB;
}
//****************************************************************************
****
//* 名稱(chēng):SaveBitmapToFile
//* 修改:徐景周(jingzhou_xu@163.net)
//* 功能:保存為位圖文件
//****************************************************************************
****
BOOL CScreenSnapDlg::SaveBitmapToFile(HBITMAP hBitmap , CString lpFileName)?
{ ? ? ? ??
? ? HDC ? ? ? ? ? ? ? ?hDC; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//設(shè)備描述表 ?
? ? int ? ? ? ? ? ? ? ?iBits; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//當(dāng)前顯示分辨率下每個(gè)
像素所占字節(jié)數(shù)
? ? WORD ? ? ? ? ? ?wBitCount; ? ? ? ? ? ? ? ? ? ? ? ?//位圖中每個(gè)像素所占字節(jié)
數(shù)
? ? DWORD ? ? ? ? ? dwPaletteSize=0, ? ? ? ? ? ? ? ?//定義調(diào)色板大小, 位圖中像
素字節(jié)大小 ,位圖文件大小 , 寫(xiě)入文件字節(jié)數(shù)
? ? ? ? ? ? ? ? ? ? dwBmBitsSize,
? ? ? ? ? ? ? ? ? ? dwDIBSize, dwWritten;
? ? BITMAP ? ? ? ? ?Bitmap; ? ? ? ?
? ? BITMAPFILEHEADER ? bmfHdr; ? ? ? ? ? ? ? ? ? ? ? ?//位圖屬性結(jié)構(gòu) ? ?
? ? BITMAPINFOHEADER ? bi; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//位圖文件頭結(jié)構(gòu) ? ? ?
? ? LPBITMAPINFOHEADER lpbi; ? ? ? ? ? ? ? ? ? ? ? ?//位圖信息頭結(jié)構(gòu) ? ??
? ? HANDLE ? ? ? ? ?fh, hDib, hPal,hOldPal=NULL; ? ?//指向位圖信息頭結(jié)構(gòu),定義文
件,分配內(nèi)存句柄,調(diào)色板句柄
??
? ?//計(jì)算位圖文件每個(gè)像素所占字節(jié)數(shù)
? ?hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
? ?iBits = GetDeviceCaps(hDC, BITSPIXEL) *?
? ?GetDeviceCaps(hDC, PLANES);
? ?DeleteDC(hDC);
? ?if (iBits <= 1)
? ? ? wBitCount = 1;
? ?else if (iBits <= 4)
? ? ? wBitCount = 4;
? ?else if (iBits <= 8)
? ? ? wBitCount = 8;
? ?else if (iBits <= 24)
? ? ? wBitCount = 24;
? ?//計(jì)算調(diào)色板大小
? ?if (wBitCount <= 8)
? ? ? dwPaletteSize = (1 << wBitCount) *sizeof(RGBQUAD);
? ?
? ?//設(shè)置位圖信息頭結(jié)構(gòu)
? ?GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
? ?bi.biSize ? ? ? ? ? ?= sizeof(BITMAPINFOHEADER);
? ?bi.biWidth ? ? ? ? ? = Bitmap.bmWidth;
? ?bi.biHeight ? ? ? ? ?= Bitmap.bmHeight;
? ?bi.biPlanes ? ? ? ? ?= 1;
? ?bi.biBitCount ? ? ? ? = wBitCount;
? ?bi.biCompression ? ? ?= BI_RGB;
? ?bi.biSizeImage ? ? ? ?= 0;
? ?bi.biXPelsPerMeter ? ? = 0;
? ?bi.biYPelsPerMeter ? ? = 0;
? ?bi.biClrUsed ? ? ? ? = 0;
? ?bi.biClrImportant ? ? ?= 0;
? ?dwBmBitsSize = ((Bitmap.bmWidth *
? ? wBitCount+31)/32)* 4
? ? ?*Bitmap.bmHeight ;
? ?//為位圖內(nèi)容分配內(nèi)存
? ?hDib ?= GlobalAlloc(GHND,dwBmBitsSize+
? ? dwPaletteSize+sizeof(BITMAPINFOHEADER));
? ?lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
? ?*lpbi = bi;
? ?// 處理調(diào)色板 ??
? ?hPal = GetStockObject(DEFAULT_PALETTE);
? ?if (hPal)
? ?{
? ? ? ?hDC ?= ::GetDC(NULL);
? ? ? ?hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
? ? ? ?RealizePalette(hDC);
? ?}
? ?// 獲取該調(diào)色板下新的像素值
? ?GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,
? ? ?(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize,
? ? ?(LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
? ?//恢復(fù)調(diào)色板 ??
? ?if (hOldPal)
? ?{
? ? ? SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
? ? ? RealizePalette(hDC);
? ? ? ::ReleaseDC(NULL, hDC);
? ?}
? ?//創(chuàng)建位圖文件 ? ?
? ? fh = CreateFile(lpFileName, GENERIC_WRITE,?
? ? ? ? ?0, NULL, CREATE_ALWAYS,
? ? ? ? ?FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
? ?if (fh == INVALID_HANDLE_VALUE)
? ? ? return FALSE;
? ?// 設(shè)置位圖文件頭
? ?bmfHdr.bfType = 0x4D42; ?// "BM"
? ?dwDIBSize ? ?= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPale
tteSize + dwBmBitsSize; ?
? ?bmfHdr.bfSize = dwDIBSize;
? ?bmfHdr.bfReserved1 = 0;
? ?bmfHdr.bfReserved2 = 0;
? ?bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)?
? ? ? + (DWORD)sizeof(BITMAPINFOHEADER)
? ? ?+ dwPaletteSize;
? ?// 寫(xiě)入位圖文件頭
? ?WriteFile(fh, (LPSTR)&bmfHdr, sizeof
? ? (BITMAPFILEHEADER), &dwWritten, NULL);
? ?// 寫(xiě)入位圖文件其余內(nèi)容
? ?WriteFile(fh, (LPSTR)lpbi, dwDIBSize,?
? ?&dwWritten, NULL);
? ?//消除內(nèi)存分配 ?
? ?GlobalUnlock(hDib);
? ?GlobalFree(hDib);
? ?CloseHandle(fh);
? ?return TRUE;
}
20. ? ?如何獲取局域網(wǎng)上計(jì)算機(jī)名及它們的IP地址
l ? ?連接ws2_32.lib和 mpr.lib庫(kù)
l ? ?#include winsock2.h
CString strTemp;
struct hostent *host;
struct in_addr *ptr; // 檢索IP地址
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL,?
? ? ? ? ? ? ?NULL, &hEnum );
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if ( hEnum )
{
? ? DWORD Count = 0xFFFFFFFF;
? ? DWORD BufferSize = 2048;
? ? LPVOID Buffer = new char[2048];
? ? WNetEnumResource( hEnum, &Count,?
? ? ? ? Buffer, &BufferSize );
? ? NetResource = (NETRESOURCE*)Buffer;
? ? char szHostName[200];
? ? unsigned int i;
? ? for ( i = 0;?
? ? ? ? i < BufferSize/sizeof(NETRESOURCE);?
? ? ? ? i++, NetResource++ )
? ? {
? ? ? ? if ( NetResource->dwUsage ==?
? ? ? ? ? ? RESOURCEUSAGE_CONTAINER &&?
? ? ? ? ? ? NetResource->dwType ==?
? ? ? ? ? ? RESOURCETYPE_ANY )
? ? ? ? {
? ? ? ? ? ? if ( NetResource->lpRemoteName )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CString strFullName =?
? ? ? ? ? ? ? ? ? ? NetResource->lpRemoteName;
? ? ? ? ? ? ? ? if ( 0 ==?
? ? ? ? ? ? ? ? ? ? strFullName.Left(2).Compare("\\") ) ??
? ? ? ? ? ? ? ? ? ? strFullName =?
? ? ? ? ? ? ? ? ? ? ? ? strFullName.Right(
? ? ? ? ? ? ? ? ? ? ? ? ? ? strFullName.GetLength()-2);
? ? ? ? ? ? ? ? gethostname( szHostName,?
? ? ? ? ? ? ? ? ? ? strlen( szHostName ) );
? ? ? ? ? ? ? ? host = gethostbyname(strFullName);
? ? ? ? ? ? ? ? if(host == NULL) continue;?
? ? ? ? ? ? ? ? ptr = (struct in_addr *)?
? ? ? ? ? ? ? ? ? ? host->h_addr_list[0]; ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? // =. 分隔開(kāi)IP:211.40.35.76. ? ? ? ? ? ??
? ? ? ? ? ? ? ? int a = ptr->S_un.S_un_b.s_b1; ?// 211 ? ? ? ? ??
? ? ? ? ? ? ? ? int b = ptr->S_un.S_un_b.s_b2; ?// 40
? ? ? ? ? ? ? ? int c = ptr->S_un.S_un_b.s_b3; ?// 35
? ? ? ? ? ? ? ? int d = ptr->S_un.S_un_b.s_b4; ?// 76
? ? ? ? ? ? ? ? strTemp.Format("%s --> ?%d.%d.%d.%d",
? ? ? ? ? ? ? ? ? ? strFullName,a,b,c,d);
? ? ? ? ? ? ? ? AfxMessageBox(strTemp);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? delete Buffer;
? ? WNetCloseEnum( hEnum );?
}
WSACleanup();
總結(jié)
以上是生活随笔為你收集整理的VC++ 常用编程技巧总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。