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);
就不行:(
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);
就不行:(
在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);
}难道这样有问题吗?急!!!
[email protected]
[email protected]
//// 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);
}
// 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_)
我调试了以下你的程序,发现在getlogfont()处出了问题,是控件的问题,
你Create()后为什么不ShowWindow()?
你不Show它怎么会出来呢?
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里错了。
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);
因为在控件没有出来的没有出来的时候就错了,没有CREATE函数没有初试函数,自然去查PresubClass跟踪后发现 psusong(爱因思念) 的推断是有道理的,但是不是GetLogFont的问题
而是GetFont()->m_hObject 引起的错误。
如果if(GetFont()->m_hObject == NULL)这句话同样会出错。
所以只要把PresubClass里的那段错误的替换掉就可以了。(见上面的回复)
你在Create()控件的地方设一个断点
,然后跟踪进去,你就会发现在什么地方出错了
这里根本没有用什么别的工具!
您说得对!!
出错的地方是:
GetFont()->GetLogFont(&sLogFont);
关键的错误是由GetFont()的调用引起的
也谢谢psusong(爱因思念)的抛砖引玉,谢谢!