MFC 实现richedit右键菜单的复制粘贴功能

     需要用到Richedit的右键菜单复制,粘贴功能。百度谷歌了一下,发现网上的文章都是零零碎碎,借鉴意义不大。于是,发扬我们桂电勇于挑战困难,努力进取的精神,终于完成了右键添加菜单和复制粘贴等功能。

   大致分三个步骤:1.MFC添加右键菜单;2.添加菜单消息响应函数;3.菜单消息响应里使用剪切板实现复制,粘贴功能。


1.MFC添加右键菜单 

1.建立一个基于对话框的的MFC程序。

2.Insert->Resource->Menu。

3.可以给menu改一个id,也可以不改。

4.把菜单搞成这样的(搞成别的样的也可以)。

5.在对话框对应的类上点右键 -> Add ClassWizard( WindowsMessage Handler) -> WM_CONTEXTMENU->编辑之。

6.修改OnContextMenu函数


{CSDN:COED:27}

voidCRButtonMenuDlg::OnContextMenu(CWnd* pWnd, CPoint point)

{

       // TODO: Add your message handler codehere

       CMenu popMenu;

       popMenu.LoadMenu(IDR_RBTN_MENU);         //载入菜单

       CMenu *pPopup;

       pPopup=popMenu.GetSubMenu(0);     //获得子菜单指针

      

       pPopup->EnableMenuItem(ID_1,MF_BYCOMMAND|MF_ENABLED);  //允许菜单项使用

       pPopup->EnableMenuItem(ID_2,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);       //不允许菜单项使用

 

       //ClientToScreen(&point);            //将客户区坐标转换成屏幕坐标

       pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);                //显示弹出菜单,参数依次为(鼠标在菜单左边|跟踪右键,x,y,this)

       pPopup->Detach();

       popMenu.DestroyMenu();

}


2.添加菜单消息响应函数

1.在资源窗口插入菜单,编辑菜单。(菜单名称后面加(&字母)就是快捷键)

2.在对话框右键>属性>常规     菜单选框中选择上面建立的菜单。

运行可以见到,对话框已建立起了菜单,下面开始添加响应事件

菜单每一项都有一个ID,可以在菜单窗口查看。1.在对话框的CPP文件里的响应事件列表中BEGIN_MESSAGE_MAP 加上:     ON_COMMAND(ID_1, OnC1)         // 第一个参数为菜单项ID,第二个参数为事件名称,这里命名为OnC1;

2.在对话框的类中(.h文件)添加成员函数声明:  afx_msg void OnC1();  

3.在对话框的CPP文件中加成员函数(菜单响应事件)的定义void CMyDlg::OnC1(){       MessageBox("Test ok");}

3. 菜单消息响应里 使用剪切板实现复制,粘贴功能。

下面的代码示范了如何将文本内容复制到剪贴板:

C++代码:
Cstring source;
//文本内容保存在source变量中
if(openClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, dource.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
Strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}

下面的代码显示了如何从剪贴板上获得文本内容:

C++代码:
char * buffer = NULL;
//打开剪贴板
CString fromClipboard;
if ( OpenClipboard() )
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
fromClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}


本人在网上找到的参考文章

1.MFC添加右键菜单 ----http://blog.csdn.net/tianbo77/article/details/44600719

2.添加菜单消息响应函数----http://jingyan.baidu.com/article/656db918edc91ee381249c2f.html

3.菜单消息响应里使用剪切板实现复制,粘贴功能----http://blog.csdn.net/xlm289348/article/details/8081554


本人自己实现的源码:

//鼠标右键响应函数
void CMSR009BTDlg::OnContextMenu(CWnd* pWnd, CPoint point) 
{
// TODO: Add your message handler code here
CMenu popMenu;
CRect rect1,rect2,rect3;//定义矩形区域
int k=0;


if(MyDevFound)
{
TrackSelect1 = NoSelect; //编辑框右键选择
TrackSelect2 = NoSelect; //编辑框右键选择
TrackSelect3 = NoSelect; //编辑框右键选择


GetDlgItem(IDC_EDIT_Track1)->GetWindowRect(&rect1);//获取控件区域的矩形
GetDlgItem(IDC_EDIT_Track2)->GetWindowRect(&rect2);//获取控件区域的矩形
GetDlgItem(IDC_EDIT_Track3)->GetWindowRect(&rect3);//获取控件区域的矩形


if(rect1.PtInRect(point))
{
TrackSelect1 = EditSelect;
}
else if(rect2.PtInRect(point))
{
TrackSelect2 = EditSelect;
}
else if(rect3.PtInRect(point))
{
TrackSelect3 = EditSelect;
}


if((TrackSelect1 == EditSelect)||(TrackSelect2 == EditSelect)||(TrackSelect3 == EditSelect))
{
CMenu *pPopup;
popMenu.LoadMenu(IDR_MENU1); //载入菜单

pPopup=popMenu.GetSubMenu(0); //获得子菜单指针


pPopup->EnableMenuItem(ID_Copy,MF_BYCOMMAND|MF_ENABLED); //允许菜单项使用
pPopup->EnableMenuItem(ID_Paste,MF_BYCOMMAND|MF_ENABLED); //允许菜单项使用
pPopup->EnableMenuItem(ID_Select,MF_BYCOMMAND|MF_ENABLED); //允许菜单项使用
pPopup->EnableMenuItem(ID_Deselect,MF_BYCOMMAND|MF_ENABLED); //允许菜单项使用
//pPopup->EnableMenuItem(ID_3,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED); //不允许菜单项使用


//ClientToScreen(&point); //将客户区坐标转换成屏幕坐标
pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this); //显示弹出菜单,参数依次为(鼠标在菜单左边|跟踪右键,x,y,this)
pPopup->Detach();
popMenu.DestroyMenu();
}
}
}


//复制编辑框数据
void CMSR009BTDlg::OnCopyData()
{       
char cData[255] = {0};
int Maxsize = 255;
int track1len,track2len,track3len;


if(TrackSelect1 == EditSelect)
{
//鼠标拖选
((CEdit *)GetDlgItem(IDC_EDIT_Track1))->GetSel(WrtieTrack1.Start, WrtieTrack1.End);
memset(cData,0,sizeof(cData));
track1len = m_ctrlTrack1.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack1.End > track1len)
{
WrtieTrack1.End = track1len;
}
GetDlgItem(IDC_EDIT_Track1)->GetWindowText(WrtieTrack1.Allstring);
WrtieTrack1.Editdtring = WrtieTrack1.Allstring.Left(WrtieTrack1.End).Right(WrtieTrack1.End - WrtieTrack1.Start);
}
else if(TrackSelect2 == EditSelect)
{
//鼠标拖选
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->GetSel(WrtieTrack2.Start, WrtieTrack2.End);
memset(cData,0,sizeof(cData));
track2len = m_ctrlTrack2.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack2.End > track2len)
{
WrtieTrack2.End = track2len;
}
GetDlgItem(IDC_EDIT_Track2)->GetWindowText(WrtieTrack2.Allstring);
WrtieTrack2.Editdtring = WrtieTrack2.Allstring.Left(WrtieTrack2.End).Right(WrtieTrack2.End - WrtieTrack2.Start);
}
else if(TrackSelect3 == EditSelect)
{
//鼠标拖选
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->GetSel(WrtieTrack3.Start, WrtieTrack3.End);
memset(cData,0,sizeof(cData));
track3len = m_ctrlTrack3.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack3.End > track3len)
{
WrtieTrack3.End = track3len;
}
GetDlgItem(IDC_EDIT_Track3)->GetWindowText(WrtieTrack3.Allstring);
WrtieTrack3.Editdtring = WrtieTrack3.Allstring.Left(WrtieTrack3.End).Right(WrtieTrack3.End - WrtieTrack3.Start);
}


//文本内容保存在source变量中,复制到粘贴板中
if(OpenClipboard())
{
HGLOBAL clipbuffer;
char * buffer;


EmptyClipboard();


if(TrackSelect1 == EditSelect)
{
clipbuffer = GlobalAlloc(GMEM_DDESHARE, WrtieTrack1.Editdtring.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(WrtieTrack1.Editdtring));
}
else if(TrackSelect2 == EditSelect)
{
clipbuffer = GlobalAlloc(GMEM_DDESHARE, WrtieTrack2.Editdtring.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(WrtieTrack2.Editdtring));
}
else if(TrackSelect3 == EditSelect)
{
clipbuffer = GlobalAlloc(GMEM_DDESHARE, WrtieTrack3.Editdtring.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(WrtieTrack3.Editdtring));
}


GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
}


//粘贴数据到编辑框
void CMSR009BTDlg::OnPasteData()
{    
int track1len,track2len,track3len;
char cData[255] = {0};
int Maxsize = 255;
char * buffer = NULL;
//打开剪贴板
CString fromClipboard;


if(OpenClipboard())
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
fromClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}


CHARFORMAT cf; 
ZeroMemory(&cf, sizeof(CHARFORMAT)); 
cf.cbSize = sizeof(CHARFORMAT); 
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE; 
cf.dwEffects = 0; 


if(TrackSelect1 == EditSelect)
{
m_ctrlTrack1.Paste();

if(m_brush1 != NULL)
{
m_brush1 = NULL;
track1len = m_ctrlTrack1.GetWindowText((char*)cData,Maxsize);
m_ctrlTrack1.SetSel(0, track1len); //设置处理区域
m_ctrlTrack1.SetSelectionCharFormat(cf);
m_ctrlTrack1.SetSel(track1len,track1len); //设置处理区域
}
}
else if(TrackSelect2 == EditSelect)
{
m_ctrlTrack2.Paste();

if(m_brush2 != NULL)
{
m_brush2 = NULL;
track2len = m_ctrlTrack2.GetWindowText((char*)cData,Maxsize);
m_ctrlTrack2.SetSel(0, track2len); //设置处理区域
m_ctrlTrack2.SetSelectionCharFormat(cf);
m_ctrlTrack2.SetSel(track2len,track2len); //设置处理区域
}
}
else if(TrackSelect3 == EditSelect)
{
m_ctrlTrack3.Paste();

if(m_brush3 != NULL)
{
m_brush3 = NULL;
track3len = m_ctrlTrack3.GetWindowText((char*)cData,Maxsize);
m_ctrlTrack3.SetSel(0, track3len); //设置处理区域
m_ctrlTrack3.SetSelectionCharFormat(cf);
m_ctrlTrack3.SetSel(track3len,track3len); //设置处理区域
}
}
}


//选择数据
void CMSR009BTDlg::OnSelectData()
{   
    char cData[255] = {0};
int Maxsize = 255;
int track1len,track2len,track3len;
int i,j;


if(TrackSelect1 == EditSelect)
{
((CEdit *)GetDlgItem(IDC_EDIT_Track1))->GetSel(WrtieTrack1.Start, WrtieTrack1.End);
memset(cData,0,sizeof(cData));
track1len = m_ctrlTrack1.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack1.End > track1len)
{
WrtieTrack1.End = track1len;
}
GetDlgItem(IDC_EDIT_Track1)->GetWindowText(WrtieTrack1.Allstring);
WrtieTrack1.Editdtring = WrtieTrack1.Allstring.Left(WrtieTrack1.End).Right(WrtieTrack1.End - WrtieTrack1.Start);


//----------------------------------//
if(WrtieTrack1.Start !=  WrtieTrack1.End)
{
j = WrtieTrack1.End - WrtieTrack1.Start;
if(j > 9)
{
SetDlgItemText(IDC_MessageTxet,WrtieTrack1.Editdtring);
MessageBox("(Message) The selected  number of track1 can not be more than 9 digits","Notion",MB_OK);
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();


return;
}
for(i=0;i{
if((WrtieTrack1.Editdtring[i] < '0') || (WrtieTrack1.Editdtring[i] > '9'))
{
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();
SetDlgItemText(IDC_MessageTxet,WrtieTrack1.Editdtring);
MessageBox("(Message) The selected data of track1 can only choose digits!","Notion",MB_OK);
return;
}
}
if (m_brush1)
DeleteObject(m_brush1); 
m_brush1 = CreateSolidBrush(RGB(200, 200, 255)); // 根据需要设置


GetDlgItem(IDC_EDIT_Track1)->SetWindowText(WrtieTrack1.Allstring);
((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(WrtieTrack1.Start, WrtieTrack1.End);
m_ctrlTrack1.SetFocus();
Invalidate();

CHARFORMAT cf; 
ZeroMemory(&cf, sizeof(CHARFORMAT)); 
cf.cbSize = sizeof(CHARFORMAT); 
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE; 
cf.dwEffects = 0; 
cf.crTextColor = RGB(255, 0, 0); //文字颜色
m_ctrlTrack1.SetSel(WrtieTrack1.Start, WrtieTrack1.End); //设置处理区域
m_ctrlTrack1.SetSelectionCharFormat(cf);
m_ctrlTrack1.EnableWindow(false);
}
}
else if(TrackSelect2 == EditSelect)
{
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->GetSel(WrtieTrack2.Start, WrtieTrack2.End);
memset(cData,0,sizeof(cData));
track2len = m_ctrlTrack2.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack2.End > track2len)
{
WrtieTrack2.End = track2len;
}
GetDlgItem(IDC_EDIT_Track2)->GetWindowText(WrtieTrack2.Allstring);
WrtieTrack2.Editdtring = WrtieTrack2.Allstring.Left(WrtieTrack2.End).Right(WrtieTrack2.End - WrtieTrack2.Start);


//--------------------------//
if(WrtieTrack2.Start !=  WrtieTrack2.End)
{
j = WrtieTrack2.End - WrtieTrack2.Start;
if(j > 9)
{
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();
SetDlgItemText(IDC_MessageTxet,WrtieTrack2.Editdtring);
MessageBox("(Message) The selected  number of track2 can not be more than 9 digits","Notion",MB_OK);
return;
}
for(i=0;i{
if((WrtieTrack2.Editdtring[i] < '0') || (WrtieTrack2.Editdtring[i] > '9'))
{
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();
SetDlgItemText(IDC_MessageTxet,WrtieTrack2.Editdtring);
MessageBox("(Message) The selected data of track2 can only choose digits!","Notion",MB_OK);
return;
}
}


if (m_brush2)
DeleteObject(m_brush2); 
m_brush2 = CreateSolidBrush(RGB(200, 200, 255)); // 根据需要设置


GetDlgItem(IDC_EDIT_Track2)->SetWindowText(WrtieTrack2.Allstring);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(WrtieTrack2.Start, WrtieTrack2.End);
m_ctrlTrack2.SetFocus();
Invalidate();

CHARFORMAT cf; 
ZeroMemory(&cf, sizeof(CHARFORMAT)); 
cf.cbSize = sizeof(CHARFORMAT); 
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE; 
cf.dwEffects = 0; 
cf.crTextColor = RGB(255, 0, 0); //文字颜色
m_ctrlTrack2.SetSel(WrtieTrack2.Start, WrtieTrack2.End); //设置处理区域
m_ctrlTrack2.SetSelectionCharFormat(cf);
m_ctrlTrack2.EnableWindow(false);
}
}
else if(TrackSelect3 == EditSelect)
{
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->GetSel(WrtieTrack3.Start, WrtieTrack3.End);
memset(cData,0,sizeof(cData));
track3len = m_ctrlTrack3.GetWindowText((char*)cData,Maxsize);

if(WrtieTrack3.End > track3len)
{
WrtieTrack3.End = track3len;
}
GetDlgItem(IDC_EDIT_Track3)->GetWindowText(WrtieTrack3.Allstring);
WrtieTrack3.Editdtring = WrtieTrack3.Allstring.Left(WrtieTrack3.End).Right(WrtieTrack3.End - WrtieTrack3.Start);


//----------------------//
if(WrtieTrack3.Start !=  WrtieTrack3.End)
{
j = WrtieTrack3.End - WrtieTrack3.Start;
if(j > 9)
{
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();
SetDlgItemText(IDC_MessageTxet,WrtieTrack3.Editdtring);
MessageBox("(Message) The selected  number of track3 can not be more than 9 digits","Notion",MB_OK);
return;
}
for(i=0;i{
if((WrtieTrack3.Editdtring[i] < '0') || (WrtieTrack3.Editdtring[i] > '9'))
{
m_brush1 = NULL;
m_brush2 = NULL;
m_brush3 = NULL;


((CEdit *)GetDlgItem(IDC_EDIT_Track1))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track2))->SetSel(-1, 0);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(-1, 0);
Invalidate();
SetDlgItemText(IDC_MessageTxet,WrtieTrack3.Editdtring);
MessageBox("(Message) The selected data of track3 can only choose digits!","Notion",MB_OK);
return;
}
}


if (m_brush3)
DeleteObject(m_brush3); 
m_brush3 = CreateSolidBrush(RGB(200, 200, 255)); // 根据需要设置


GetDlgItem(IDC_EDIT_Track3)->SetWindowText(WrtieTrack3.Allstring);
((CEdit *)GetDlgItem(IDC_EDIT_Track3))->SetSel(WrtieTrack3.Start, WrtieTrack3.End);
m_ctrlTrack3.SetFocus();
Invalidate();

CHARFORMAT cf; 
ZeroMemory(&cf, sizeof(CHARFORMAT)); 
cf.cbSize = sizeof(CHARFORMAT); 
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE; 
cf.dwEffects = 0; 
cf.crTextColor = RGB(255, 0, 0); //文字颜色
m_ctrlTrack3.SetSel(WrtieTrack3.Start, WrtieTrack3.End); //设置处理区域
m_ctrlTrack3.SetSelectionCharFormat(cf);
m_ctrlTrack3.EnableWindow(false);
}
}


if((WrtieTrack1.Start ==  WrtieTrack1.End) && (WrtieTrack2.Start ==  WrtieTrack2.End) && (WrtieTrack3.Start ==  WrtieTrack3.End))
{
MessageBox("Did not select any data!","Notion",MB_OK);
}
}


void CMSR009BTDlg::OnDeselectData()
{  
CHARFORMAT cf; 
ZeroMemory(&cf, sizeof(CHARFORMAT)); 
cf.cbSize = sizeof(CHARFORMAT); 
cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE; 
cf.dwEffects = 0; 


if(TrackSelect1 == EditSelect)
{
m_ctrlTrack1.EnableWindow(true);
WrtieTrack1.Start = 0;
WrtieTrack1.End = 0;
WrtieTrack1.Editdtring = "";
WrtieTrack1.Allstring = "";


m_brush1 = NULL;
m_ctrlTrack1.SetSel(0, 200); //设置处理区域
m_ctrlTrack1.SetSelectionCharFormat(cf);
m_ctrlTrack1.SetSel(0,0); //设置处理区域
}
else if(TrackSelect2 == EditSelect)
{
m_ctrlTrack2.EnableWindow(true);
WrtieTrack2.Start = 0;
WrtieTrack2.End = 0;
WrtieTrack2.Editdtring = "";
WrtieTrack2.Allstring = "";


m_brush2 = NULL;
m_ctrlTrack2.SetSel(0, 200); //设置处理区域
m_ctrlTrack2.SetSelectionCharFormat(cf);
m_ctrlTrack2.SetSel(0,0); //设置处理区域
}
else if(TrackSelect3 == EditSelect)
{
m_ctrlTrack3.EnableWindow(true);
WrtieTrack3.Start = 0;
WrtieTrack3.End = 0;
WrtieTrack3.Editdtring = "";
WrtieTrack3.Allstring = "";


m_brush3 = NULL;
m_ctrlTrack3.SetSel(0, 200); //设置处理区域
m_ctrlTrack3.SetSelectionCharFormat(cf);
m_ctrlTrack3.SetSel(0,0); //设置处理区域
}
}

你可能感兴趣的:(C++,MFC)