时间: 2021-07-31 作者:daque
同济大学大学 罗筱波 ---- 在现在的搜集期间,超联接无所不在,在形形色色的软硬件上咱们都不妨看到它的生存,这简直成了一个必不行少的一项传播实质。即使能在本人的步调中介入一个超联接,那么会使咱们的步调带上明显的搜集特性。然而在vc6.0中并没有供给一个用来创造超联接的控件,所以惟有本人编写实行此功效的步调。底下是自己编写的一个实行超联接的类,蓄意能与大师瓜分。 ---- 创造思绪是:从类cstatic 派生出一个子类csuperlinkctrl,而后为子类csuperlinkctrl介入符合的因变量使其能相应鼠目标动静和实行超联接的功效。创造进程如次: ---- 一、开始从类cstatic 派生出一个子类csuperlinkctrl,用classwizard天生的头文献简直代码如次: class csuperlinkctrl : public cstatic{// constructionpublic: csuperlinkctrl();csuperlinkctrl(colorref clickedtextcor,colorref moveontextcor,colorref ordinarytextcor);// attributespublic: colorref m_crtext; //超联接文本的脸色// operationspublic:// overrides// classwizard generated virtual function overrides //{{afx_virtual(csuperlinkctrl) //}}afx_virtual// implementationpublic: bool setlinkaddress(cstring linkaddress); //用来树立超联接的地方 void mouseleave(void); void mouseenter(void); void setlinkcursor(hcursor hcursor); //用来树立超联接的鼠标形势 void setsuperlink(bool setlink); virtual ~csuperlinkctrl(); // generated message map functionsprotected:cstring m_linktext; // shell 挪用的联接参数bool m_bclicked; // 鼠标能否单击超联接hcursor m_hcursor; // 鼠标挪动到超联接上时的鼠标形势//{{afx_msg(csuperlinkctrl)afx_msg hbrush ctlcolor(cdc* pdc, uint nctlcolor);afx_msg void onlbuttondown(uint nflags, cpoint point);afx_msg bool onsetcursor(cwnd* pwnd, uint nhittest, uint message);afx_msg void onmousemove(uint nflags, cpoint point);//}}afx_msg declare_message_map()private:bool m_bcaptured; //捕获鼠标挪动colorref m_clickedtextcor; //单击超联接后文本的脸色colorref m_moveontextcor; //鼠标挪动到超联接时的文本的脸色colorref m_ordinarytextcor; //未激活超联接时文本的脸色logfont m_lf; cfont m_font; //超联接上的文本的字体};---- 二、为类编写实行文献 ---- 在类的两个重载结构因变量中实行类的初始化处事 csuperlinkctrl::csuperlinkctrl(){ m_bclicked=false; m_bcaptured=false; m_clickedtextcor=rgb(0,255,255); m_moveontextcor=rgb(0,255,0); m_ordinarytextcor=rgb(0,0,255);m_crtext=m_ordinarytextcor; // 树立初始化字体脸色 ::getobject((hfont)getstockobject(default_gui_font),sizeof(m_lf),&m_lf); //博得暂时的缺省字体m_lf.lfunderline = true; //为暂时缺省字体介入下划线 bool bcreated = m_font.createfontindirect(&m_lf); // 创造超联接字体 assert(bcreated); m_hcursor=null; }csuperlinkctrl::csuperlinkctrl(colorrefclickedtextcor,colorref moveontextcor,colorref ordinarytextcor){ m_bclicked=false; m_bcaptured=false; m_clickedtextcor=clickedtextcor; m_moveontextcor=moveontextcor; m_ordinarytextcor=ordinarytextcor;m_crtext=m_ordinarytextcor; // 树立初始化字体脸色 ::getobject((hfont)getstockobject(default_gui_font),sizeof(m_lf),&m_lf); //博得暂时的缺省字体m_lf.lfunderline = true; //为暂时缺省字体介入下划线 bool bcreated = m_font.createfontindirect(&m_lf); // 创造超联接字体 assert(bcreated); m_hcursor=null;}在类的析构因变量中简略在类的结构因变量中创造的字体csuperlinkctrl::~csuperlinkctrl(){ m_font.deleteobject(); //简略在类的结构因变量中创造的字体}用classwizard为类介入如次动静映照并编写动静映照因变量begin_message_map(csuperlinkctrl, cstatic) //{{afx_msg_map(csuperlinkctrl) on_wm_ctlcolor_reflect() on_wm_lbuttondown() on_wm_setcursor() on_wm_mousemove() //}}afx_msg_mapend_message_map()编写动静映照因变量ctlcolor()用来实行超联接文本脸色的变革hbrush csuperlinkctrl::ctlcolor(cdc* pdc, uint nctlcolor) { // todo: change any attributes of the dc here if (ctlcolor_static == nctlcolor) { pdc->selectobject(&m_font); pdc->settextcolor(m_crtext); pdc->setbkmode(transparent); }hbrush hbrush = ::createsolidbrush(getsyscolor(color_3dface)); //创造一个刷子句柄return hbrush; // 归来一个用来画控件后台的刷子句柄}实行鼠标按下左键动静void csuperlinkctrl::onlbuttondown(uint nflags, cpoint point) { // todo: add your message handler code here and/or call defaultm_bclicked=true; m_crtext=m_clickedtextcor; //树立超联接文本的脸色redrawwindow(); // 重画控件实行超联接文本脸色的变革 if(m_linktext!="") //即使树立了超联接的地方则挪用shellexecute因变量实行超联接 shellexecute(null, "open", m_linktext, null, null, sw_shownormal); cstatic::onlbuttondown(nflags, point); // 挪用基类相应因变量}树立超联接文本上的鼠标形势的动静映照因变量bool csuperlinkctrl::onsetcursor(cwnd* pwnd, uint nhittest, uint message) {// todo: add your message handler code here and/or call defaultif (m_hcursor) // 即使已博得光标句柄则树立新的鼠标形势 { ::setcursor(m_hcursor); return true; } return cstatic::onsetcursor(pwnd, nhittest, message);}树立超联接文本上的光标句柄void csuperlinkctrl::setlinkcursor(hcursor hcursor){ m_hcursor = hcursor;}相应鼠标挪动动静void csuperlinkctrl::onmousemove(uint nflags, cpoint point) { // todo: add your message handler code here and/or call default setcapture(); // 捕获鼠标 point mpoint; getcursorpos(&mpoint); // 博得暂时鼠标场所 crect rect;getwindowrect(&rect); // 博得控件的窗口巨细if(!rect.ptinrect(mpoint)) // 确定鼠标能否在控件的窗口内{ // 鼠标不在控件的窗口内 m_bcaptured=false; releasecapture(); // 开释鼠标捕获 mouseleave(); // 挪用mouseleave()因变量 return; } // 鼠标在控件的窗口内if(m_bcaptured) // 即使鼠标在窗口内且仍旧被捕获则归来 return; m_bcaptured=true; mouseenter(); //挪用mouseenter()因变量 releasecapture(); // 开释鼠标捕获cstatic::onmousemove(nflags, point); // 挪用基类相应因变量}在mouseenter()和mouseleave()因变量中树立鼠标加入和摆脱时的超联接文本的脸色void csuperlinkctrl::mouseenter(){m_crtext=m_bclicked? m_clickedtextcor : m_moveontextcor;redrawwindow(); // 重画控件实行超联接文本脸色的变革}void csuperlinkctrl::mouseleave(){m_crtext=m_bclicked? m_clickedtextcor : m_ordinarytextcor;redrawwindow(); // 重画控件实行超联接文本脸色的变革}在setlinkaddress()因变量中树立超联接到地方bool csuperlinkctrl::setlinkaddress(cstring linkaddress){ // 考证地方的正当性 if(linkaddress.find("http")!=-1) m_linktext=linkaddress; else if(linkaddress.find("@")!=-1) m_linktext="mailto:"+linkaddress; else { messagebox("error: wrong superlink format"); m_linktext=""; return false; } return true;}---- 因为static控件并不相应鼠标动静,以是还需在派生的子类中作些窜改。底下因变量是使static控件能相应鼠标动静的要害(由于常常static控件只能相应很少一局部动静) void csuperlinkctrl::setsuperlink(bool setlink){if (setlink) // 此处必需介入modifystyle(),使static控件能相应鼠标动静 modifystyle(0,ss_notify); else modifystyle(ss_notify,0);}---- 至该类csuperlinkctrl已创造实行便可在其它步调中运用了。 ---- 运用本领有两种:一是用classwizard使一个static控件与一个csuperlinkctrl变量关系联,另一种本领是设置一个csuperlinkctrl变量而后用subclassdlgitem()使它与一个static控件关系联。 ---- 运用示例: ---- 开始将superlinkctrl1.h和superlinkctrl1.cpp介入到工程的目次中,而后设置一个变量csuperlinkctrl m_test; 而后在对话框中介入一个static控件,树立其id为idc_statictest,之后在对话框的oninitdialog()因变量中介入如次代码即可 m_test.subclassdlgitem(idc_statictest,this); // idc_statictest是一个须要子类化的static控件的idm_test.setsuperlink(true); //树立控件为超联接控件m_test.setlinkcursor(afxgetapp()->loadcursor(idc_cursorhand)); // 树立超联接控件的鼠标形势m_test.setlinkaddress("xxxx@citiz.net"); // 树立超联接的地方---- 如何样,赶快试一下吧!