Catching EM_SETCHARFORMAT and modifying the CHARFORMAT data in EN_PROTECTED
I am receiving a EM_CHARFORMAT message in my message handler when SetFont() is called on my custom control. But when I change the data in the CHARFORMAT structure pointed to in the LPARAM, it isn't being used.
void CMyRichEdit::OnProtected(NMHDR* pNMHDR, LRESULT* pResult)
{
ENPROTECTED* pEP = (ENP开发者_高级运维ROTECTED*)pNMHDR;
*pResult = 1;
switch(pEP->msg)
{
case EM_SETCHARFORMAT:
{
CHARFORMAT *cf = reinterpret_cast<CHARFORMAT *>(pEP->lParam);
cf->dwEffects |= (CFE_PROTECTED | CFE_ITALIC);
cf->dwMask |= (CFM_PROTECTED | CFM_ITALIC);
*pResult = 0;
}
break;
The MSDN docs say only if the lParam value changes is it used over the original... but here lParam is a pointer to an object. How can I allocate a new object without the memory leaking?
How can I allocate a new object without the memory leaking?
I'm not sure that reallocating lParam is going to work, but since a window procedure doesn't have to worry about being called on multiple threads, you can point lParam to a static variable safely. That way you don't have to worry about freeing it.
case EM_SETCHARFORMAT:
{
static CHARFORMAT mycf;
CHARFORMAT *cf = reinterpret_cast<CHARFORMAT *>(pEP->lParam);
mycf = *cf;
pEP->lParam = (LPARAM)(LONG_PTR)&mycf;
cf->dwEffects |= (CFE_PROTECTED | CFE_ITALIC);
cf->dwMask |= (CFM_PROTECTED | CFM_ITALIC);
*pResult = 0;
}
break;
However, I really don't think that this is going to solve your problem.
精彩评论