m_hyperlink=new CMyHyperLink();
m_hyperlink->Create("click here",WS_CHILD|WS_VISIBLE,rect2,this,
   IDC_HYPERLINK);
注:CMyHyperLink是CStatic的继承类编译没问题,但是运行的时候运行不了。怎么回事啊!!!???我如果将m_hyperlink声明成一个CStatic,然后再m_hyperlink=new CStatic();
然后再m_hyperlink->Create("click here",WS_CHILD|WS_VISIBLE,rect2,this,
   IDC_HYPERLINK);
就可以。
但是照上面那样就不行,这是为什么?
别人的CMyHyperLink类也是继承CStatic的啊:class CMyHyperLink : public CStatic
继承的类为什么就不能create()了呢?
运行时候就是被终止了,在xp下运行时就是出现那个“发送错误报告”的对话框。补充:就是说如果将m_hyperlink声明为一个CMyHyperLink(CMyHyperLink是CStatic的继承类),再
m_hyperlink=new CMyHyperLink();
m_hyperlink->Create("click here",WS_CHILD|WS_VISIBLE,rect2,this,
   IDC_HYPERLINK);
就不行:(

解决方案 »

  1.   

    最好把这个继承类的Create函数代码贴出来,看看它是怎么创建的。
      

  2.   

    这个继承类CMyHyperLink本来没有写Create()函数,因为它是继承CStatic的,所以我简单的添加了一个虚函数Create(),是这样写的:
    在MyHyperLink.h中先声明
    public:
    virtual BOOL Create(LPCTSTR lpszText, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);然后再在MyHyperLink.cpp中简单定义:
    BOOL CMyHyperLink::Create(LPCTSTR lpszText, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) 
    {
    // TODO: Add your specialized code here and/or call the base class

    return CStatic::Create(lpszText,dwStyle,rect,pParentWnd,nID);
    }难道这样有问题吗?急!!!
      

  3.   

    或者将定义发到我得信箱
    [email protected]
    [email protected]
      

  4.   

    // MyHyperLink.cpp : implementation file
    //// Written By : Renjith.R
    // Email : [email protected]
    // Details :Derived from MFC CStatic 
    // Date :Nov 25 2002#include "stdafx.h"
    #include "ControlContainer.h"
    #include "MyHyperLink.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    /////////////////////////////////////////////////////////////////////////////
    // CMyHyperLinkCMyHyperLink::CMyHyperLink()
    {
    m_sLinkColor = RGB(0, 0 ,255);
    m_sHoverColor = RGB(255, 0, 0);
    m_sVisitedColor = RGB(5, 34, 143); m_bFireChild = false;
    m_bMouseOver = false;
    m_bEnableToolTip = false;
    m_bVisited =  false;

    //Create Tooltip
    }CMyHyperLink::~CMyHyperLink()
    {
    }
    BEGIN_MESSAGE_MAP(CMyHyperLink, CStatic)
    //{{AFX_MSG_MAP(CMyHyperLink)
    ON_WM_MOUSEMOVE()
    ON_WM_SETCURSOR()
    ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyHyperLink message handlers//Sets the Link Color
    void CMyHyperLink::SetLinkColor(COLORREF sLinkColor)
    {
    m_sLinkColor = sLinkColor;}//open the URL by Windows ShellExecute()
    bool CMyHyperLink::GoToLinkUrl(CString csLink)
    { HINSTANCE hInstance = (HINSTANCE)ShellExecute(NULL, _T("open"), csLink.operator LPCTSTR(), NULL, NULL, 2); if ((UINT)hInstance < HINSTANCE_ERROR){
    return false;
    }else
    return true;
    }//User can Active/Inactive the Tooltip already they set 
    void CMyHyperLink::ActiveToolTip(int nFlag)
    {
    if (nFlag)
    m_bEnableToolTip = true;
    else
    m_bEnableToolTip = false;
    }//change The Tooltip text
    void CMyHyperLink::SetTootTipText(LPCSTR szToolTip)
    {
    if (m_bEnableToolTip )
    {
    m_ToolTip.UpdateTipText(szToolTip,this,1001);
    }}//The Mouse Move Message
    void CMyHyperLink::OnMouseMove(UINT nFlags, CPoint point) 
    {
    CStatic::OnMouseMove(nFlags, point);
    if (m_bMouseOver)
    {
    CRect oRect;
    GetClientRect(&oRect); //check if the mouse is in the rect
    if (oRect.PtInRect(point) == false)
    {
    m_bMouseOver = false;
    //Release the Mouse capture previously take
    ReleaseCapture();
    RedrawWindow();
    return;
    }
    }else
    {
    m_bMouseOver = true;
    RedrawWindow();
    //capture the mouse
    SetCapture();
    }
    }//before Subclassing 
    void CMyHyperLink::PreSubclassWindow() 
    { //Enable the Static to send the Window Messages To its parent
    DWORD dwStyle = GetStyle();
    SetWindowLong(GetSafeHwnd() ,GWL_STYLE ,dwStyle | SS_NOTIFY); char szCurretText[MAX_PATH];
    GetWindowText(szCurretText, MAX_PATH);
    if ((szCurretText) == NULL){
    SetWindowText(m_csLinkText.operator LPCTSTR());
    }

    LOGFONT sLogFont;
    GetFont()->GetLogFont(&sLogFont);
    //Set the Link UnderLined
    sLogFont.lfUnderline = true;
    //Set the Font to  the Control
    m_oTextFont.CreateFontIndirect(&sLogFont);
    this->SetFont(&m_oTextFont, true);

    //Adjust the window
    //IsValidURL(); //Set the Cursor Hand
    //WinHlp32.exe in windows folder ResourceID 106
    //is a standard window HAND cursor 
    //courtesy www.codeguru.com
    //you can use a custom Hand cursor resourse also
    // i added that  as a resourse in this project with 
    // ID - IDC_CURSOR_HAND char szWindowsDir[MAX_PATH*2];
    GetWindowsDirectory(szWindowsDir ,MAX_PATH*2);
    strcat(szWindowsDir,"\\Winhlp32.exe");
    HMODULE hModule = LoadLibrary(szWindowsDir);

    if (hModule){
    m_hHyperCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
    } this->SetCursor(m_hHyperCursor); //free the module
    if (hModule)
    FreeLibrary(hModule); CStatic::PreSubclassWindow();
    this->SetCursor(m_hHyperCursor);

    m_ToolTip.Create(this,TTS_ALWAYSTIP);
    CRect oRect;
    GetClientRect(&oRect);
    m_ToolTip.AddTool(this,"",oRect,1001);
    m_ToolTip.ShowWindow(SW_HIDE);
    }void CMyHyperLink::SetLinkText(CString csLinkText)
    {
    m_csLinkText = csLinkText;
    this->SetWindowText(csLinkText.operator LPCTSTR());}BOOL CMyHyperLink::PreTranslateMessage(MSG* pMsg) 
    {
    m_ToolTip.RelayEvent(pMsg);
    return CStatic::PreTranslateMessage(pMsg);
    }
    BOOL CMyHyperLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
    { ::SetCursor(m_hHyperCursor);
    return true;
    //return CStatic::OnSetCursor(pWnd, nHitTest, message);
    }//////////////////EVENT WILL GET HERE //////////////////////
    void CMyHyperLink::OnClicked() 
    {
    if (m_bFireChild){
    //Fire the Event to Parent Window
    CWnd *pParent;
    pParent = GetParent();
    int nCtrlID = GetDlgCtrlID();
    ::SendMessage(pParent->m_hWnd, _HYPERLINK_EVENT, (WPARAM)nCtrlID, 0);
    //::PostMessage(pParent->m_hWnd, __EVENT_ID_, (WPARAM)nCtrlID, 0); }else
    {
    GoToLinkUrl(m_csUrl);
    } m_bVisited = true;
    //reddraw the control 
    this->Invalidate(true);
    }HBRUSH CMyHyperLink::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    if (m_bMouseOver){
    if (m_bVisited)
    pDC->SetTextColor(m_sVisitedColor);
    else
    pDC->SetTextColor(m_sHoverColor);
    }else {
    if (m_bVisited)
    pDC->SetTextColor(m_sVisitedColor);
    else
    pDC->SetTextColor(m_sLinkColor);
    }
    pDC->SetBkMode(TRANSPARENT);
    return((HBRUSH)GetStockObject(NULL_BRUSH));
    }void CMyHyperLink::SetToolTipTextColor(COLORREF sToolTipText) {
    m_ToolTip.SetTipTextColor(sToolTipText);
    }void CMyHyperLink::SetToolTipBgColor(COLORREF sToolTipBgColor)
    {
    m_ToolTip.SetTipBkColor(sToolTipBgColor);}CString CMyHyperLink::GetLinkText()  {
    if (m_csLinkText.IsEmpty())
    return CString("");
    return m_csLinkText;
    }void CMyHyperLink::SetLinkUrl(CString csUrl) {
    m_csUrl= csUrl;
    }CString CMyHyperLink::GetLinkUrl() {
    return m_csUrl;
    }void CMyHyperLink::SetVisitedColor(COLORREF sVisitedColor) {
    m_sVisitedColor = sVisitedColor ;
    }void CMyHyperLink::SetHoverColor(COLORREF cHoverColor) {
    m_sHoverColor = cHoverColor;
    }void CMyHyperLink::SetFireChild(int nFlag) {
    if (nFlag)
    m_bFireChild = true;
    else
    m_bFireChild = false;
    }BOOL CMyHyperLink::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
    {
    NMHDR* pMsgHdr;
    pMsgHdr = (NMHDR*) lParam; switch (pMsgHdr->code){
    case NM_RCLICK:
    break;
    default:
    ;
    }

    return CStatic::OnNotify(wParam, lParam, pResult);
    }
      

  5.   

    ////////////////////////////////////////////////////////
    // Class Name : CMyHyperLink
    // Written By : Renjith.R
    // Email : [email protected]
    // Details :Derived from MFC CStatic 
    // Date :Nov 25 2002
    // This can be used as a Hyperlink 
    //Feel free to use this class in your project///////////////////////////////////////////////////////////
    #if !defined(AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_)
    #define AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // MyHyperLink.h : header file
    ////This is the EventID , Which Will  send to the Parent
    //by the hyperlink  control# define _HYPERLINK_EVENT WM_USER + 101 /////////////////////////////////////////////////////////////////////////////
    // CMyHyperLink windowclass CMyHyperLink : public CStatic
    {
    // Construction
    public:
    CMyHyperLink();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyHyperLink)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    protected:
    virtual void PreSubclassWindow();
    virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
    //}}AFX_VIRTUAL// Implementation
    public:
    void SetFireChild(int nFlag);
    CString GetLinkText();
    CString GetLinkUrl();

    bool GoToLinkUrl(CString csLink);

    void SetHoverColor(COLORREF cHoverColor);
    void SetVisitedColor(COLORREF sVisitedColor);
    void SetLinkUrl(CString csUrl);
    void SetToolTipBgColor(COLORREF sToolTipBgColor);
    void SetToolTipTextColor(COLORREF sToolTipText);
    void SetLinkText(CString csLinkText);
    void SetTootTipText(LPCSTR szToolTip);
    void ActiveToolTip(int nFlag);
    void SetLinkColor(COLORREF sLinkColor);

    virtual ~CMyHyperLink(); // Generated message map functions
    protected:
    bool m_bFireChild;

    HCURSOR m_hHyperCursor;

    bool m_bEnableToolTip;
    bool m_bMouseOver;
    bool m_bVisited; CFont m_oTextFont;
    CToolTipCtrl m_ToolTip; CString m_csToolTipText;
    CString m_csLinkText;
    CString m_csUrl; COLORREF m_sHoverColor;
    COLORREF m_sLinkColor;
    COLORREF m_sVisitedColor; //{{AFX_MSG(CMyHyperLink)
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
    afx_msg void OnClicked();
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_)
      

  6.   

    今天下午,和同事一起做需求数据分析了(自己接的活)!就把你的问题放下了!晚上回来,发现的确有你说的那种怪现象存在
    我调试了以下你的程序,发现在getlogfont()处出了问题,是控件的问题,
      

  7.   

    哇,楼主的代码可真多啊,老长老长的.
    你Create()后为什么不ShowWindow()?
    你不Show它怎么会出来呢?
      

  8.   

    两颗星的psusong(爱因思念),我现在很着急,今天又来上班了,你是怎么发现getlogfont()处有问题的呢?用什么工具,能告诉我吗?你知道解决办法吗?我试了一下其他的CHyperLink,也出现这种问题。oiq(oiq),现在不是秀不秀的问题,用CStatic类不show它也能出来,所以这里也不用show,即使要show,不写这一条语句程序运行也不会运行不下去的。如果实在解决不了,谁能给我提供一个能动态创建的CHyperLink类吗?
      

  9.   

    LOGFONT sLogFont;
    GetFont()->GetLogFont(&sLogFont);
    //Set the Link UnderLined
    sLogFont.lfUnderline = true;
    //Set the Font to  the Control
    m_oTextFont.CreateFontIndirect(&sLogFont);
    this->SetFont(&m_oTextFont, true);
    在PRESUBCLASS里错了。
      

  10.   

    m_oTextFont.DeleteObject();
    LOGFONT sLogFont;
    memset(&sLogFont,0,sizeof(sLogFont));
    sLogFont.lfHeight = 20;
    sLogFont.lfCharSet = DEFAULT_CHARSET;//Set the Link UnderLined
    sLogFont.lfUnderline = true;
    //Set the Font to  the Control
    m_oTextFont.CreateFontIndirect(&sLogFont);
    this->SetFont(&m_oTextFont, true);
      

  11.   

    fingerfox(狐狸.兄弟会)师兄,怎么改,你能告诉我吗?提一点建议也可以。还有,你们是如何发现哪里错了的,授人以鱼不如授人以网~~~~~~~!!
      

  12.   

    改的贴出来了(你回复的上面),错的也贴了(我的第一个回复),
    因为在控件没有出来的没有出来的时候就错了,没有CREATE函数没有初试函数,自然去查PresubClass跟踪后发现 psusong(爱因思念) 的推断是有道理的,但是不是GetLogFont的问题
    而是GetFont()->m_hObject 引起的错误。
    如果if(GetFont()->m_hObject == NULL)这句话同样会出错。
    所以只要把PresubClass里的那段错误的替换掉就可以了。(见上面的回复)
      

  13.   

    to 搂主:
    你在Create()控件的地方设一个断点
    ,然后跟踪进去,你就会发现在什么地方出错了
    这里根本没有用什么别的工具!
      

  14.   

    to:fingerfox(狐狸.兄弟会)
    您说得对!!
    出错的地方是:
    GetFont()->GetLogFont(&sLogFont);
    关键的错误是由GetFont()的调用引起的
      

  15.   

    fingerfox(狐狸.兄弟会)师兄,你改得非常正确,我用release编译就可以运行了,但是,你能告诉我这是为什么吗?而且为什么只能在release下通过,而不能在debug下通过?不过再怎么样,也要谢谢你!我想再注册一个号,把新的100分也给你,还有psusong(爱因思念)。
    也谢谢psusong(爱因思念)的抛砖引玉,谢谢!