我們現(xiàn)在所設計的程序中,或多或少都會涉及到Internet操作,這就需要我們的程
序具有初步的Internet處理能力。本文將向大家介紹把Internet的超級鏈接(
Hyper Link)能力加入到我們用VC++設計的程序中的方法。
Win32 API函數(shù)中有一個函數(shù)ShellExcute,它的原型是:
HINSTANCE ShellExecute(HWND hwnd, LPCTSTR lpOperation, LPCTSTR
lpFile,
LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd );
這個函數(shù)可以使用已經(jīng)在Windows注冊的相關(guān)程序打開Windows系統(tǒng)中的文件。
例如給出一個URL到lpFile參數(shù)中,系統(tǒng)會啟動瀏覽器去瀏覽網(wǎng)址;給出一個.DOC
文檔到lpFile中,系統(tǒng)會使用Word打開這個文檔。
我們利用Visual C++向?qū)梢粋€單文檔界面的窗口程序框架,如圖1。生成一個
新的對話框。用AppWizard建立一個基于通用窗口的新類CHyperLinkWnd,并修改代
碼如下:
CHyperLinkWnd::CHyperLinkWnd() //構(gòu)造函數(shù)
{ m_hLinkCursor = AfxGetApp()-〉LoadCursor(IDC_LINK); //引入手指形狀
的光標 }
void CHyperLinkWnd::OnMouseMove(UINT nFlags, CPoint point)
{ ::SetCursor(m_hLinkCursor);//當鼠標在窗口上移動,光標變成手指狀
CWnd::OnMouseMove(nFlags, point);}
BOOL CHyperLinkWnd::Create( const RECT&& rect, CWnd? pParentWnd)
//創(chuàng)建窗口
{ return CWnd::Create ( NULL, NULL, WS_CHILD|WS_VISIBLE, rect,
pParentWnd,NULL,NULL);}
BOOL CHyperLinkWnd::SetLinkString(CString m_LinkString)
{ LinkString
=m_LinkString;
//設置窗口的提示信息
return TRUE; }
void CHyperLinkWnd::SetLink(CString m_Link)
{ Link=m_Link; //設置鏈接網(wǎng)址信息 }
void CHyperLinkWnd::OnLButtonDown(UINT nFlags, CPoint point)
{ ::ShellExecute(this-〉m_hWnd,"open",Link,NULL,NULL,
SW_SHOWMAXIMIZED); }
void CHyperLinkWnd::OnPaint()
{ CFont? Font=NULL;
CPaintDC dc(this); //設置超級鏈接中的顯示字體信息,即藍色有下劃線
dc.SetTextColor(RGB(0, 0, 255));
dc.SetBkColor(::GetSysColor(COLOR_3DFACE));
m_font=dc.GetCurrentFont();
LOGFONT lFont;
m_font-〉GetObject(sizeof(lFont), &&lFont);
m_font=NULL;
m_font=new CFont();
lFont.lfUnderline=TRUE;
m_font-〉CreateFontIndirect(&&lFont);
dc.SelectObject(m_font);
dc.TextOut(0,0,LinkString);}
//使用這個類的一個范例,即在一個對話框中使用這些超級鏈接
BOOL CNetPrice::OnInitDialog()
{ RECT rect;
HyperLinkWnd[0]=new CHyperLinkWnd(); //使用這個類,可以先在對話框中
用一個CStatic框定位,并去掉它的Visible屬性,然后用代碼取得它的位置,并將
超級鏈接窗口放上去
m_Canton.GetWindowRect(&&rect);
this-〉ScreenToClient(&&rect);
HyperLinkWnd[0]-〉SetLinkString("廣州太平洋電腦城");
HyperLinkWnd[0]-〉SetLink("www.pconline.com.cnt");
HyperLinkWnd[0]-〉Create(rect,this);
……//略掉部分重復代碼
HyperLinkWnd[6]=new CHyperLinkWnd();//下面這個是使用郵件鏈接的例子
m_SendEMail.GetWindowRect(&&rect);
this-〉ScreenToClient(&&rect);
HyperLinkWnd[6]-〉SetLinkString("發(fā)郵件給總公司");
HyperLinkWnd[6]-〉SetLink("MailTo:Alexandrite@cmmail.com);
HyperLinkWnd[6]-〉Create(rect,this);
return TRUE;}
最后,編譯連接,得到一個可以使用超級鏈接的VC++例子。
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。