本人目前在学习VC++,碰到了一个问题,就是想继承CEdit控件,自己完成右键单击后弹出的菜单的项以及消息处理事件。目前功能已完成最简单的部分。现在我想通过CWnd::CreateEx函数来创建这控件,不过该函数包含了一个参数lpszClassName,该参数就是一个字符串,我想通过传递字符串来创建继承后的控件,就要注册该控件的窗口类,可是怎么注册呢?试了好多方法一直都不行。代码如下:
/****************EditExtend.h*****************.
#pragma once
#define IDR_MENU_MAIN (40000)
#define IDR_MENU_COPY (40001)
#define IDR_MENU_PASTE (40002)
// CEditExtend
class CEditExtend : public CEdit
{
DECLARE_DYNAMIC(CEditExtend)
public:
CEditExtend();
virtual ~CEditExtend();
protected:
DECLARE_MESSAGE_MAP()
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
protected:
afx_msg void OnEditCopy();
afx_msg void OnEditPaste();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
protected:
virtual BOOL CreateMenu(CMenu &menu);
public:
static BOOL RegisterNewClass(HINSTANCE hInstance);
static LRESULT CALLBACK ExtendProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
};
// EditExtend.cpp : 实现文件
//
#include "stdafx.h"
#include "EditExtend.h"
// CEditExtend
IMPLEMENT_DYNAMIC(CEditExtend, CEdit)
LRESULT CALLBACK CEditExtend::ExtendProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
if (pWnd == NULL)
{
pWnd = new CEditExtend();
pWnd->Attach(hWnd);
}
return AfxCallWndProc(pWnd, hWnd, message, wParam, lParam);
}
CEditExtend::CEditExtend()
{
}
CEditExtend::~CEditExtend()
{
}
BEGIN_MESSAGE_MAP(CEditExtend, CEdit)
ON_COMMAND(IDR_MENU_COPY, OnEditCopy)
ON_COMMAND(IDR_MENU_PASTE, OnEditPaste)
ON_WM_CREATE()
END_MESSAGE_MAP()
LRESULT CEditExtend::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_CONTEXTMENU)
{
CMenu menu;
if (CreateMenu(menu) == FALSE)
{
return FALSE;
}
int startPos = 0;
int endPos = 0;
GetSel(startPos, endPos);
// 用户未选择文本
if (startPos == endPos ||
(GetStyle() & ES_READONLY))
{
menu.EnableMenuItem(IDR_MENU_COPY, MF_GRAYED);
}
BOOL isTextDataInClipboard = IsClipboardFormatAvailable(CF_UNICODETEXT);
// 判断剪贴板是否有值
if (!isTextDataInClipboard ||
(GetStyle() & ES_READONLY))
{
menu.EnableMenuItem(IDR_MENU_PASTE, MF_GRAYED);
}
menu.TrackPopupMenu(TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), this);
return TRUE;
}
return CEdit::DefWindowProc(message, wParam, lParam);
}
void CEditExtend::OnEditPaste()
{
Paste();
}
void CEditExtend::OnEditCopy()
{
Copy();
}
int CEditExtend::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
{
return -1;
}
return 1;
}
BOOL CEditExtend::CreateMenu(CMenu &menu)
{
if (menu)
{
// 删除原来的Menu
menu.DestroyMenu();
}
// 创建空的弹出菜单项
if (!menu.CreatePopupMenu())
{
return FALSE;
}
static struct MenuItemStruct
{
UINT nFlag;
UINT nID;
LPCTSTR menuName;
} MenuItem[] =
{
{0, IDR_MENU_COPY, _T("&复制")},
{0, IDR_MENU_PASTE, _T("&粘贴")}
};
int count = (int)(sizeof(MenuItem) / sizeof(MenuItemStruct));
for (int i = 0; i < count; i++)
{
menu.AppendMenu(
MenuItem[i].nFlag, MenuItem[i].nID, MenuItem[i].menuName);
}
return TRUE;
}
BOOL CEditExtend::RegisterNewClass(HINSTANCE hInstance)
{
//WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(GetSafeHwnd(), GWL_WNDPROC, (DWORD)AfxGetAfxWndProc());
WNDCLASS wcl;
if (!GetClassInfo(NULL, _T("EDIT"), &wcl))
{
return FALSE;
}
wcl.lpszClassName = _T("EDITEXTEND");
wcl.lpfnWndProc = ExtendProc;
wcl.hInstance = hInstance;
return RegisterClass(&wcl);
}