MFC 动态创建超链接 CStatic

本来觉得CStatic 我们很会少拿来大量的对其修改操作,但是今天自己却需要对其进行修改,说一下使用过程和遇到的问题

1)首先说下创建问题,查看很多网页,许多将动态的创建时,第一步仍是要先给其分配一个ID,由于我要完成的客户自己画出这个文本框,那么分配这个ID 就很难实现;

选择用Create,

CStatic *m_static  = new CStatic;

m_static->Create(_T("my static"), WS_CHILD|WS_VISIBLE|SS_CENTER,CRect(10,10,150,50),this,ID_MYSTATIC);

开始自己在.cpp中定义了 #define ID_MYSTATIC 9001; 因为给CStatic 添加响应事件时需要:

 .h 中:

afx_msg void OnClicked();

.cpp 中:

BEGIN_MESSAGE_MAP(CTextLinkDlg, CDialog)

   ON_STN_CLICKED(ID_MYSTATIC,&CTextLinkDlg::OnClicked)

END_MESSAGE_MAP()

.cpp中实现:

void CTextLinkDlg::OnClicked()

{

}

这里就会遇到一个问题,事件并不会响应,本来想自己定义的ID的问题,但是考虑不会啊,在MFC 自己生成的过程,也无非是定义一个ID ,然后create,仍然要走这个过程,

但是就是不会响应。然后用mfc生成的CStatic控件 测试了下,发现其也是不无法响应,这才知道CStatic控件默认是不接受事件响应的,其默认ID 为“IDC_STATIC ,但是如果把其ID 改了之后就可以接受ON_CLICK事件了,不过,需要同时把其属性设置为Notify 才能接收到系统的消息,从而响应。

修改: m_static->ModifyStyle(0,SS_NOTIFY);即可!

由于在之后的创建中,可能会建立很多这样的cstatic ,那么分配ID_MYSTATIC ,是一个问题,选择不为NULL ,因此之后这里的事件响应不可操作,之后在继承子CStatic的类得到了解决。

2)以上是自己动态创建的CStatic,下面借用网上下载的类来实现超链接

      之前转载的一篇讲SubClassWindow 就是捣鼓超链接时遇到的问题,是篇很好的文章。

     CSuperLabel.h

  #if !defined(AFX_SUPERLABEL_H__3027B777_6D85_416C_A768_EC7BC3C03E3A__INCLUDED_)
#define AFX_SUPERLABEL_H__3027B777_6D85_416C_A768_EC7BC3C03E3A__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// SuperLabel.h : header file
//


/
// CSuperLabel window


class CSuperLabel : public CStatic
{
// Construction
public:
CSuperLabel();


// Attributes
public:
CFont     m_Font;
CString   m_ConnectStr;
LOGFONT   lfont;
// Operations
public:


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSuperLabel)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL


// Implementation
public:
void SubWindow(HWND hwnd);
virtual ~CSuperLabel();



// Generated message map functions
protected:
//{{AFX_MSG(CSuperLabel)
afx_msg void OnPaint();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG


DECLARE_MESSAGE_MAP()
public:
int m_SelectPage;
virtual BOOL PreTranslateMessage(MSG* pMsg);
};


/


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.


#endif // !defined(AFX_SUPERLABEL_H__3027B777_6D85_416C_A768_EC7BC3C03E3A__INCLUDED_)

     .cpp 文件

// SuperLabel.cpp : implementation file
//


#include "stdafx.h"
#include "TextLink.h"
#include "SuperLabel.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/
// CSuperLabel


CSuperLabel::CSuperLabel()
: m_SelectPage(0)
{
// m_Font.CreateFont(18,18,0,0,400,0,1,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DRAFT_QUALITY,DEFAULT_PITCH,"楷体32");


}


CSuperLabel::~CSuperLabel()
{
}




BEGIN_MESSAGE_MAP(CSuperLabel, CStatic)
//{{AFX_MSG_MAP(CSuperLabel)
ON_WM_PAINT()
ON_WM_LBUTTONUP()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CSuperLabel message handlers


void CSuperLabel::OnPaint() 
{
CPaintDC dc(this); // device context for painting
CDC* pDC = GetDC();


pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,255));




pDC->SelectObject(&m_Font);
//pDC->TextOut(0,0,_T("点此查看链接"));
}


void CSuperLabel::OnLButtonUp(UINT nFlags, CPoint point) 
{
CStatic::OnLButtonUp(nFlags, point);
}


int CSuperLabel::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}


void CSuperLabel::OnLButtonDown(UINT nFlags, CPoint point) 
{
AfxMessageBox(_T("自己创建的"));
switch (m_SelectPage)
{
case 1:
m_ConnectStr = _T("http://www.google.com.hk/");
AfxMessageBox(_T("弹出关联的图片1"));
break;
case 2:
m_ConnectStr = _T("http://www.baidu.com/");
break;
case 3:
m_ConnectStr = _T("http://www.sina.com.cn/");
}


//ShellExecute(m_hWnd,NULL,m_ConnectStr,NULL,NULL,SW_SHOW);
CStatic::OnLButtonDown(nFlags, point);
}


void CSuperLabel::PreSubclassWindow() 
{
// GetWindowText(m_ConnectStr);
/*CFont* pFont = GetFont();


pFont->GetLogFont(&lfont);
lfont.lfUnderline =TRUE;
m_Font.CreateFontIndirect(&lfont);*/

CStatic::PreSubclassWindow();
}


void CSuperLabel::SubWindow(HWND hwnd) //获得IDC_STATIC_EDIT 的句柄,并通过SubClassWindow将该句柄赋值给CSuperLabel的数据成员m_hWnd;
{
this->SubclassWindow(hwnd);        //其实我们常用的DDX_Control 方法说到底也是调用了SubClassWindow函数;
}                                      //只所以需要这样CStatic控件在MFC 中没法用向导来生成变量


void CSuperLabel::OnMouseMove(UINT nFlags, CPoint point) 
{
CStatic::OnMouseMove(nFlags, point);

::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
}




BOOL CSuperLabel::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类


return CStatic::PreTranslateMessage(pMsg);
}

个人感觉这个类比较好理解,在消息响应时也并没有用到ID ,这样为Create提供了条件!

但是需要说明的是,在使用MFC 产生的CStatic ,然后修改为CSuperLink后,是需要使用SubWIndow的设置的,这里等同于DDX_CONTROL();的作用,

在OnInitDialog()中;

((CButton*)GetDlgItem(IDC_RADIO_GOOGLE))->SetCheck(1);
HWND hwnd;
GetDlgItem(IDC_STATIC_LINK,&hwnd);
m_Lebel.SubWindow(hwnd);
m_Lebel.m_SelectPage = 1;

但是在使用Create时,跟踪进去,发现已经调用内部SubClassWindow了,因此不需要再设置,因此用时可以修改CSuperLink类 

mm = new CSuperLabel;
mm->Create(_T("my static"), WS_CHILD|WS_VISIBLE|SS_CENTER,CRect(10,10,150,50),this/*,ID_MYSTATIC*/);
mm->m_SelectPage = 1;
mm->.ModifyStyle(0,SS_NOTIFY);

      mm->showWindow(trure);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值