Limitations of cricheditctrl and evolution Methods

Source: Internet
Author: User
Tags ole

Today, a colleague is leaving the company to go to the computer newspaper to do things. At noon, everyone held a farewell meeting for him. Everyone drank a lot.
After I sent my colleague away, I went back to the office and felt dizzy. I didn't want to do anything, so I concentrated on my mind. These days I was dissatisfied with cricheditctrl In the MFC framework, in this blog, I hope that our predecessors will be able to give more advice and help them eliminate some obstacles in the development process of MFC.

  1. Cannot be automatically initialized
  2. The tab key cannot be accepted.
  3. Automatic line feed cannot be set through Properties
  4. Other OLE objects such as images cannot be displayed
  5. You cannot use Ctrl + C to copy data.

Cannot be automatically initialized

When I put the rich edit control on the resource form for the first time, I found that the program could not run at all. Later, I found the cause. The rich edit control is an ole-type control. Initialization is required when the rich edit control is loaded. The Code is as follows:
Boolctestapp: initinstance (){

.....
Afxinitrichedit ();

}

The tab key cannot be accepted.

When the RichEdit control is placed on the resource form, it is found that its attribute page is not set to accept the tab key settings. As a result, when the focus is placed on the rich Edit Control, press the tab key to move the focus to the next control.
The specific solution is to overload the ongetdlgcode of the rich edit control:
Sample Code:
. H file:
Class cmyrichedit: Public cricheditctrl {

........
Afx_msg uint ongetdlgcode ();
........

}
. Cpp file:
Begin_message_map (cmyrichedit, cricheditctrl)
On_wm_getdlgcode ()
End_message_map ()

Uint colericheditctrl: ongetdlgcode (){

Return dlgc_wanttab;

}

Automatic line feed cannot be set through Properties

When the rich edit control is placed on the resource form, it is found that the attribute settings for the rich Edit Control to automatically wrap are not set in its attribute form. The example code is as follows:
Bool ctestdlg: initdialog (){

.............
// M_richedit is a member variable of the Form class.
This-> m_richedit.settargetdevice (null, 0 );
............

}

Other OLE objects such as images cannot be displayed

Cricheditctrl provided by MFC does not provide properties or method settings for OLE objects such as direct display of images, but provides an interfaceSetolecallback (iricheditolecallback *Pcallback);
To display the image with cricheditctrl, you have to work hard on iricheditolecallback.
Iricheditolecallback is an interface in windows. Its definition is as follows:
Contextsensitivehelp:
By using this method, the application is notified that it will schedule the help in the following associated methods.
Deleteobject:
This method sends a notification that an object will be deleted from the RichEdit control.
Getclipboarddata:
This method allows the RichEdit client (calling program) to provide its own paste object
Getcontextmenu:
This method is used to send a right-click event request to the application to obtain the context menu.
Getdragdropeffect:
This method allows the RichEdit client (calling program) to set the effect of the drag operation.
Getinplacecontext:
This method provides application-level and file-level interfaces, as well as necessary information that supports in-place activation.
Getnewstrorage:
This method stores new objects read from clipboard or hypertext stream (RTF ).
Queryacceptdata:
This method determines whether the data introduced in the paste or drag-and-drop operation is acceptable.
Queryinsertobject:
This method is used to ask the application if an object can be inserted.
Showcontainerui:
This method is used to tell the application whether to display its own operation interface.

After learning about the iricheditolecallback interface, you should be clear that to display OLE objects such as images, you should at least implement the getnewstorage method, because this method is the interface method for storing OLE objects.

The following Code declares an interface:
Interface iexricheditolecallback: iricheditolecallback
{

Public:
Iexricheditolecallback ();
Virtual ~ Iexricheditolecallback ();
Int m_inumstorages;
Istorage * pstorage;
DWORD m_dwref;

Virtual hresult stdmethodcalltype getnewstorage (lpstorage * lplpstg );
Virtual hresult stdmethodcalltype QueryInterface (refiid IID, void ** ppvobject );
Virtual ulong stdmethodcalltype addref ();
Virtual ulong stdmethodcalltype release ();
Virtual hresult stdmethodcalltype getinplacecontext (lpoleinplaceframe far * lplpframe,
Lpoleinplaceuiwindow far * lplpdoc, lpoleinplaceframeinfo lpframeinfo );
Virtual hresult stdmethodcalltype showcontainerui (bool fshow );
Virtual hresult stdmethodcalltype queryinsertobject (maid, lpstorage lpstg, long CP );
Virtual hresult stdmethodcalltype deleteobject (lpoleobject lpoleobj );
Virtual hresult stdmethodcalltype queryacceptdata (lpdataobject lpdataobj, clipformat far * lpcfformat, DWORD reco, bool freally, hglobal hmetapict );
Virtual hresult stdmethodcalltype contextsensitivehelp (bool fentermode );
Virtual hresult stdmethodcalltype getclipboarddata (charrange far * lpchrg, DWORD reco, lpdataobject far * lplpdataobj );
Virtual hresult stdmethodcalltype getdragdropeffect (bool fdrag, DWORD grfkeystate, lpdword pdweffect );
Virtual hresult stdmethodcalltype getcontextmenu (word seltyp, lpoleobject lpoleobj, charrange far * lpchrg, hmenu far * lphmenu );

}
The interface implementation will be included in the last appendix.

You cannot use Ctrl + C to copy data.

In fact, cricheditctrl supports Ctrl + C to copy data. However, when I use the irichieditcallback INTERFACE IN THE crichieditctrl inheritance class, it does not support Ctrl + C for copying. I think the problem lies in the iricheditcallback interface.
After carefully reading its help documentation, I found the problem lies in getclipboarddata. I did not write code in its implementation method, but returned s_ OK, to process Ctrl + C, e_notimpl must be returned.

The above is my development experience over the past few days. I would like to share it with you and hope you can give me more advice.
In the following example of RichEdit code, I have referenced Mike o'neill's code. Thanks again for his contribution.

Appendix
. H file

# If! Defined (afx_olericheditctrl_h1_3dff15ee_7336_4297_9620_7f00b611daa1included _)
# Define afx_olericheditctrl_h1_3dff15ee_7336_4297_9620_7f00b611daa1included _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
// Olericheditctrl. h: header file
//

# Include <richole. h>

//////////////////////////////////////// /////////////////////////////////////
// Colericheditctrl window

Class colericheditctrl: Public cricheditctrl
{
// Construction
Public:
Colericheditctrl ();
Virtual ~ Colericheditctrl ();

Long streaminfromresource (int ires, lpctstr stype );

 

Protected:
 
Static DWORD callback readfunction (DWORD dwcookie,
Lpbyte lpbuf, // the buffer to fill
Long ncount, // number of bytes to read
Long * nread); // number of bytes actually read

Interface iexricheditolecallback; // Forward Declaration (see below in this header file)

Iexricheditolecallback * m_piricheditolecallback;
Bool m_bcallbackset;
 
 
Interface iexricheditolecallback: Public iricheditolecallback
{
Public:
Iexricheditolecallback ();
Virtual ~ Iexricheditolecallback ();
Int m_inumstorages;
Istorage * pstorage;
DWORD m_dwref;

Virtual hresult stdmethodcalltype getnewstorage (lpstorage * lplpstg );
Virtual hresult stdmethodcalltype QueryInterface (refiid IID, void ** ppvobject );
Virtual ulong stdmethodcalltype addref ();
Virtual ulong stdmethodcalltype release ();
Virtual hresult stdmethodcalltype getinplacecontext (lpoleinplaceframe far * lplpframe,
Lpoleinplaceuiwindow far * lplpdoc, lpoleinplaceframeinfo lpframeinfo );
Virtual hresult stdmethodcalltype showcontainerui (bool fshow );
Virtual hresult stdmethodcalltype queryinsertobject (maid, lpstorage lpstg, long CP );
Virtual hresult stdmethodcalltype deleteobject (lpoleobject lpoleobj );
Virtual hresult stdmethodcalltype queryacceptdata (lpdataobject lpdataobj, clipformat far * lpcfformat,
DWORD reco, bool freally, hglobal hmetapict );
Virtual hresult stdmethodcalltype contextsensitivehelp (bool fentermode );
Virtual hresult stdmethodcalltype getclipboarddata (charrange far * lpchrg, DWORD reco, lpdataobject far * lplpdataobj );
Virtual hresult stdmethodcalltype getdragdropeffect (bool fdrag, DWORD grfkeystate, lpdword pdweffect );
Virtual hresult stdmethodcalltype getcontextmenu (word seltyp, lpoleobject lpoleobj, charrange far * lpchrg,
Hmenu far * lphmenu );
};
 

Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (colericheditctrl)
Protected:
Virtual void presubclasswindow ();
//} Afx_virtual

// Implementation
Public:

// Generated message map Functions
Protected:
// {Afx_msg (colericheditctrl)
Afx_msg int oncreate (maid );
 
//} Afx_msg
Afx_msg uint ongetdlgcode ();
Declare_message_map ()
};

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

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

# Endif //! Defined (afx_olericheditctrl_h1_3dff15ee_7336_4297_9620_7f00b611daa1included _)

 

. Cpp File

// Olericheditctrl. cpp: implementation file
//

# Include "stdafx. H"
# Include "olericheditctrl. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// /////////////////////////////////////
// Colericheditctrl

Colericheditctrl: colericheditctrl ()
{
M_bcallbackset = false;
}

Colericheditctrl ::~ Colericheditctrl ()
{
// Iexricheditolecallback class is a reference-counted class
// Which deletes itself and for which delete shocould not be called

// Delete m_piricheditolecallback;
}

Begin_message_map (colericheditctrl, cricheditctrl)
// {Afx_msg_map (colericheditctrl)
On_wm_create ()
//} Afx_msg_map
On_wm_getdlgcode ()
End_message_map ()
//

Int colericheditctrl: oncreate (maid)
{
If (cricheditctrl: oncreate (lpcreatestruct) =-1)
Return-1;

// M_piricheditolecallback shold have been created in presubclasswindow

Assert (m_piricheditolecallback! = NULL );

// Set the iexricheditolecallback pointer if it wasn' t set
// Successfully in presubclasswindow

If (! M_bcallbackset)
{
Setolecallback (m_piricheditolecallback );
}

Return 0;
}

Void colericheditctrl: presubclasswindow ()
{
// Base class first
Cricheditctrl: presubclasswindow ();

M_piricheditolecallback = NULL;
M_piricheditolecallback = new iexricheditolecallback;
Assert (m_piricheditolecallback! = NULL );

M_bcallbackset = setolecallback (m_piricheditolecallback );
}

Long colericheditctrl: streaminfromresource (int ires, lpctstr stype)
{
Hinstance hinst = AfxGetInstanceHandle ();
Hrsrc =: findresource (hinst,
Makeintresource (IRES), stype );
 
DWORD Len = sizeofresource (hinst, hrsrc );
Byte * lprsrc = (byte *) loadresource (hinst, hrsrc );
Assert (lprsrc );
 
Cmemfile mfile;
Mfile. Attach (lprsrc, Len );

Editstream es;
Es. pfncallback = readfunction;
Es. dwerror = 0;
Es. dwcookie = (DWORD) & mfile;

Return streamin (sf_rtf, ES );
}

/* Static */
DWORD callback colericheditctrl: readfunction (DWORD dwcookie,
Lpbyte lpbuf, // the buffer to fill
Long ncount, // number of bytes to read
Long * nread) // number of bytes actually read
{
Cfile * fp = (cfile *) dwcookie;
* Nread = FP-> Read (lpbuf, ncount );
Return 0;
}

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

Colericheditctrl: iexricheditolecallback ()
{
Pstorage = NULL;
M_inumstorages = 0;
M_dwref = 0;

// Set up Ole Storage

Hresult =: stgcreatedocfile (null,
Stgm_transacted | stgm_readwrite | stgm_clu_exclusive/* | stgm_deleteonrelease */| stgm_create,
0, & pstorage );

If (pstorage = NULL |
Hresult! = S_ OK)
{
Afxthrowoleexception (hresult );
}
}

Colericheditctrl: iexricheditolecallback ::~ Iexricheditolecallback ()
{
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: getnewstorage (lpstorage * lplpstg)
{
M_inumstorages ++;
Wchar tname [50];
Swprintf (tname, l "reolestorage % d", m_inumstorages );

Hresult = pstorage-> createstorage (tname,
Stgm_transacted | stgm_readwrite | stgm_clu_exclusive | stgm_create,
0, 0, lplpstg );

If (hresult! = S_ OK)
{
: Afxthrowoleexception (hresult );
}

Return hresult;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: QueryInterface (refiid IID, void ** ppvobject)
{

Hresult hR = s_ OK;
* Ppvobject = NULL;
 
If (IID = iid_iunknown |
IID = iid_iricheditolecallback)
{
* Ppvobject = this;
Addref ();
HR = noerror;
}
Else
{
HR = e_nointerface;
}

Return hr;
}

 

Ulong stdmethodcalltype
Colericheditctrl: iexricheditolecallback: addref ()
{
Return ++ m_dwref;
}

 

Ulong stdmethodcalltype
Colericheditctrl: iexricheditolecallback: release ()
{
If (-- m_dwref = 0)
{
Delete this;
Return 0;
}

Return m_dwref;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: getinplacecontext (lpoleinplaceframe far * lplpframe,
Lpoleinplaceuiwindow far * lplpdoc, lpoleinplaceframeinfo lpframeinfo)
{
Return s_ OK;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: showcontainerui (bool fshow)
{
Return s_ OK;
}

 

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: queryinsertobject (maid, lpstorage lpstg, long CP)
{
Return s_ OK;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: deleteobject (lpoleobject lpoleobj)
{
Return s_ OK;
}

 

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: queryacceptdata (lpdataobject lpdataobj, clipformat far * lpcfformat,
DWORD reco, bool freally, hglobal hmetapict)
{
Return s_ OK;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: contextsensitivehelp (bool fentermode)
{
Return s_ OK;
}

 

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: getclipboarddata (charrange far * lpchrg, DWORD reco, lpdataobject far * lplpdataobj)
{
Return e_notimpl;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: getdragdropeffect (bool fdrag, DWORD grfkeystate, lpdword pdweffect)
{
Return s_ OK;
}

Hresult stdmethodcalltype
Colericheditctrl: iexricheditolecallback: getcontextmenu (word seltyp, lpoleobject lpoleobj, charrange far * lpchrg,
Hmenu far * lphmenu)
{
Return s_ OK;
}

// Tabricheditctrl Message Processing Program
Uint colericheditctrl: ongetdlgcode (){

Return dlgc_wanttab;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.