编辑框(CEdit)汇总.

来源:互联网 发布:linux 改名字命令 编辑:程序博客网 时间:2024/05/17 04:51

目录:

0.常用消息截获

1.示例功能

       实现超链接效果.

       编辑框右键菜单


0.常用消息截获

WM_CONTEXTMENU<虚截获>,响应右键


1.示例功能

实现超链接效果

概要向导解析:

1.自定义文本控件<就是从CStatic进行类派生>

2.从工具栏直接拖原始控件就行.<但是在主界面Dialog中别忘了替换绑定也就是利用DDX_Control将资源控件ID和该派生出来的对象进行绑定>

3.重写相关虚函数.<这里有个概念认知,父类中的非虚拟函数是设计成不被子类改写的。根据有无virtual关键字,PreSubclassWindow重写>


好了,下面直接贴代码了:

.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:
virtual ~CSuperLabel();



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


DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////


//{{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 "SuperConnect.h"
#include "SuperLabel.h"


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


/////////////////////////////////////////////////////////////////////////////
// CSuperLabel


CSuperLabel::CSuperLabel()
{
// 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_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();


CString text;
GetWindowText(text);

if (m_ConnectStr.IsEmpty())
m_ConnectStr = text;


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




pDC->SelectObject(&m_Font);
pDC->TextOut(0,0,text);


// Do not call CStatic::OnPaint() for painting messages
}


void CSuperLabel::OnLButtonDown(UINT nFlags, CPoint point) 
{


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::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
CStatic::OnMouseMove(nFlags, point);
}



编辑框右键菜单

从CEdit中派生出CMyEdit类,添加WM_CONTEXTMENU消息,并添加消息响应函数.
void CMyEdit::OnContextMenu(CWnd* pWnd, CPoint point) 
{
if (point.x == -1 && point.y == -1)
{
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);

point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(m_menuID));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}