#pragma once
class CMyEdit : public CWindowImpl<CMyEdit>
{
public:
BEGIN_MSG_MAP_EX(CMyEdit)
MSG_WM_PAINT(OnPaint)
MSG_WM_INITDIALOG(OnInitDialog)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
END_MSG_MAP()
//OnPaint传进来的参数始终为空?
void OnPaint(CDCHandle dc)
{
CPaintDC dcdraw(m_hWnd);
GetWindowText(m_strText);
dcdraw.TextOut(5, 5, m_strText);
}
BOOL OnInitDialog ( HWND hwndFocus, LPARAM lParam )
{
return TRUE;
}
//如果用户选中的字体的话,字体的蓝色背景会出现在本来字体的位置上,所以禁用选中字体的功能
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
public:
CMyEdit()
{
m_strText = _T("");
}
public:
CString m_strText;
CFont m_TxtFont;
};
问题:
1、OnPaint传进来的参数dc为空?
2、如何给OnPaint里的dcdraw设置字体?
3、想让用户能选中字体,但蓝色背景和字体位置不一致,如何解决?
12 个解决方案
#1
CWindowImpl<CMyEdit> 是啥东西???
应该是
class CMyEdit : public CEdit
应该是
class CMyEdit : public CEdit
#2
WTL
#3
这个不知道,到.Net板块问问吧
#4
1、OnPaint传进来的参数dc为空?
OnPaint的参数没用的,获取DC用CPaintDC类
2、如何给OnPaint里的dcdraw设置字体?
3、想让用户能选中字体,但蓝色背景和字体位置不一致,如何解决?
用EDIT解决你这两个问题很难,你可以把EDIT替换成CRichEdit
OnPaint的参数没用的,获取DC用CPaintDC类
2、如何给OnPaint里的dcdraw设置字体?
3、想让用户能选中字体,但蓝色背景和字体位置不一致,如何解决?
用EDIT解决你这两个问题很难,你可以把EDIT替换成CRichEdit
#5
现在字体是粗体的,我想设置普通字体如何设?
#6
SetFont
#7
看看
#8
#pragma once
#include <atlframe.h>
class CMyEdit : public CWindowImpl< CMyEdit/*, CEdit*/ >,
public CDoubleBufferImpl< CMyEdit >
{
public:
BEGIN_MSG_MAP_EX(CMyEdit)
// MSG_WM_PAINT(DoPaint)
MSG_WM_INITDIALOG(OnInitDialog)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
CHAIN_MSG_MAP(CDoubleBufferImpl< CMyEdit >)
REFLECT_NOTIFICATIONS()
END_MSG_MAP()
void DoPaint(CDCHandle dc)
{
int nContextState = dc.SaveDC();
DrawBkgnd(dc);
DrawItem(dc);
dc.RestoreDC( nContextState );
}
void DrawBkgnd(CDCHandle dc)
{
CRect rect;
dc.GetClipBox(&rect);
dc.SetBkColor(RGB(255, 255, 255));
dc.SetBkMode(TRANSPARENT);
dc.ExtTextOut( rect.left, rect.top, ETO_OPAQUE, rect, _T( "" ), 0, NULL );
}
void DrawItem(CDCHandle dc)
{
CRect rect;
dc.GetClipBox(&rect);
rect.OffsetRect(5, 0);
dc.SelectFont(m_TxtFont);
GetWindowText(m_strText);
// dc.TextOut(5, 5, m_strText);
dc.DrawText(m_strText, m_strText.GetLength(), &rect, DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
BOOL OnInitDialog ( HWND hwndFocus, LPARAM lParam )
{
CLogFont logFont;
logFont.SetMessageBoxFont();
// logFont.lfWeight = FW_THIN;
// logFont.lfHeight = 12;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
// m_TxtFont.CreateFont(12,
// 0,
// 0,
// 0,
// FW_REGULAR, //见MFC类参数
// 0,
// 0,
// 0,
// GB2312_CHARSET,
// OUT_DEFAULT_PRECIS,
// CLIP_DEFAULT_PRECIS,
// DEFAULT_QUALITY,
// DEFAULT_PITCH,
// /*SYSTEM_DEFAULT_FONT*/
// _T("宋体"));
return TRUE;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
public:
CMyEdit()
{
m_strText = _T("");
}
public:
CString m_strText;
CFont m_TxtFont;
};
dc没有SetFont成员,只有SelectFont,但不起作用。
#9
// HMXEdit.cpp : implementation file
//
#include "stdafx.h"
#include "novaprogram.h"
#include "HMXEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit
CHMXEdit::CHMXEdit()
{
m_clrBkGnd = GetSysColor(COLOR_WINDOW);
m_clrText = GetSysColor(COLOR_WINDOWTEXT);
m_clrBkGndFocus = m_clrBkGnd;
m_clrTextFocus = m_clrText;
m_brsBkGnd.CreateSolidBrush(m_clrBkGnd);
m_brsBkGndFocus.CreateSolidBrush(m_clrBkGndFocus);
m_bEnableEditing = true;
m_bHasFocus = false;
m_nOffsetX = 0;
m_nOffsetY = 0;
}
CHMXEdit::~CHMXEdit()
{
m_brsBkGnd.DeleteObject();
m_brsBkGndFocus.DeleteObject();
m_fntText.DeleteObject();
}
BEGIN_MESSAGE_MAP(CHMXEdit, CEdit)
//{{AFX_MSG_MAP(CHMXEdit)
ON_WM_CTLCOLOR()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit message handlers
LRESULT CHMXEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if( !m_bEnableEditing )
if( message == WM_PASTE || message == WM_KEYDOWN || message == WM_CHAR || message == WM_RBUTTONDOWN)
return FALSE;
return CEdit::WindowProc(message, wParam, lParam);
}
BOOL CHMXEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CEdit::PreTranslateMessage(pMsg);
}
void CHMXEdit::SetIMOffset(int nOffsetX, int nOffsetY)
{
m_nOffsetX = nOffsetX;
m_nOffsetY = nOffsetY;
}
void CHMXEdit::OnShowIM(BOOL bShow)
{
m_hmCore = (HMODULE) LoadLibrary(_T("coredll.dll"));
if(m_hmCore != NULL) {
pSipShowIM = (SIPSHOWIM) GetProcAddress(m_hmCore, _T("SipShowIM"));
pSipGetInfo = (SIPGETINFOR) GetProcAddress(m_hmCore, _T("SipGetInfo"));
pSipSetInfo = (SIPSETINFOR) GetProcAddress(m_hmCore, _T("SipSetInfo"));
if(pSipShowIM == NULL) {
FreeLibrary(m_hmCore);
m_hmCore=NULL;
}
}
//Show or Hide
if(bShow == TRUE)
{
SIPINFO si;
memset( &si, 0, sizeof( si ) );
si.cbSize = sizeof( si );
if( pSipGetInfo(&si) )
{ //Under CEdit
CRect rc;
GetWindowRect(&rc);
si.rcSipRect.top = rc.bottom;
//Center Sip
HDC hdc = ::GetDC(NULL);
int nX = ::GetDeviceCaps(hdc,HORZRES);
int nSipWidth = (si.rcSipRect.right - si.rcSipRect.left);
si.rcSipRect.left = ( nX - nSipWidth ) / 2 ;
//End of Center Sip
//Offset
si.rcSipRect.left += m_nOffsetX;
si.rcSipRect.right += m_nOffsetX;
si.rcSipRect.top += m_nOffsetY;
si.rcSipRect.bottom += m_nOffsetY;
//End of Offset
pSipSetInfo( &si );
}
pSipShowIM(1);
}
else
pSipShowIM(0);
}
BOOL CHMXEdit::SetFontBold(bool bBold)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontItalic(bool bItalic)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfItalic = bItalic ? TRUE : FALSE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontHeight(int nHeight)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontFaceName(const CString &sFaceName)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
_tcscpy( lgfnt.lfFaceName, sFaceName );
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::GetTextFont(LOGFONT *plgfnt)
{
GetFont()->GetObject( sizeof(LOGFONT), (void*)plgfnt);
return true;
}
BOOL CHMXEdit::SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString &sFaceName)
{
LOGFONT lgfnt;
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
lgfnt.lfEscapement = 0;
lgfnt.lfOrientation = 0;
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
lgfnt.lfItalic = bItalic?TRUE:FALSE;
lgfnt.lfUnderline = FALSE;
lgfnt.lfStrikeOut = FALSE;
lgfnt.lfCharSet = DEFAULT_CHARSET;
lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS;
lgfnt.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lgfnt.lfQuality = DEFAULT_QUALITY;
lgfnt.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
_tcscpy( lgfnt.lfFaceName, sFaceName );
return SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetTextFont(const LOGFONT &lgfnt)
{
m_fntText.DeleteObject();
m_fntText.CreateFontIndirect( &lgfnt );
SetFont( &m_fntText, TRUE );
return true;
}
BOOL CHMXEdit::EnableEditing(bool bEditing)
{
m_bEnableEditing = bEditing;
return true;
}
BOOL CHMXEdit::GetTextClrFocus(COLORREF &clr)
{
clr = m_clrTextFocus;
return true;
}
BOOL CHMXEdit::SetTextClrFocus(COLORREF clr)
{
m_clrTextFocus = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClrFocus(COLORREF &clr)
{
clr = m_clrBkGndFocus;
return true;
}
BOOL CHMXEdit::SetBkClrFocus(COLORREF clr)
{
m_clrBkGndFocus = clr;
m_brsBkGndFocus.DeleteObject();
m_brsBkGndFocus.CreateSolidBrush(clr);
Invalidate();
return true;
}
BOOL CHMXEdit::GetTextClr(COLORREF &clr)
{
clr = m_clrText;
return true;
}
BOOL CHMXEdit::SetTextClr(COLORREF clr)
{
m_clrText = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClr(COLORREF &clr)
{
clr = m_clrBkGnd;
return true;
}
BOOL CHMXEdit::SetBkClr(COLORREF clr)
{
m_clrBkGnd = clr;
m_brsBkGnd.DeleteObject();
m_brsBkGnd.CreateSolidBrush(clr);
Invalidate();
return true;
}
HBRUSH CHMXEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetBkColor(m_bHasFocus?m_clrBkGndFocus:m_clrBkGnd);
pDC->SetTextColor(m_bHasFocus?m_clrTextFocus:m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return m_bHasFocus?(HBRUSH)m_brsBkGndFocus:(HBRUSH)m_brsBkGnd;
}
void CHMXEdit::OnKillFocus(CWnd* pNewWnd)
{
m_bHasFocus = false;
Invalidate();
OnShowIM(FALSE);
CEdit::OnKillFocus(NULL);
}
void CHMXEdit::OnSetFocus(CWnd* pOldWnd)
{
m_bHasFocus = true;
Invalidate();
OnShowIM(TRUE);
CEdit::OnSetFocus(NULL);
}
//
#include "stdafx.h"
#include "novaprogram.h"
#include "HMXEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit
CHMXEdit::CHMXEdit()
{
m_clrBkGnd = GetSysColor(COLOR_WINDOW);
m_clrText = GetSysColor(COLOR_WINDOWTEXT);
m_clrBkGndFocus = m_clrBkGnd;
m_clrTextFocus = m_clrText;
m_brsBkGnd.CreateSolidBrush(m_clrBkGnd);
m_brsBkGndFocus.CreateSolidBrush(m_clrBkGndFocus);
m_bEnableEditing = true;
m_bHasFocus = false;
m_nOffsetX = 0;
m_nOffsetY = 0;
}
CHMXEdit::~CHMXEdit()
{
m_brsBkGnd.DeleteObject();
m_brsBkGndFocus.DeleteObject();
m_fntText.DeleteObject();
}
BEGIN_MESSAGE_MAP(CHMXEdit, CEdit)
//{{AFX_MSG_MAP(CHMXEdit)
ON_WM_CTLCOLOR()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit message handlers
LRESULT CHMXEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if( !m_bEnableEditing )
if( message == WM_PASTE || message == WM_KEYDOWN || message == WM_CHAR || message == WM_RBUTTONDOWN)
return FALSE;
return CEdit::WindowProc(message, wParam, lParam);
}
BOOL CHMXEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CEdit::PreTranslateMessage(pMsg);
}
void CHMXEdit::SetIMOffset(int nOffsetX, int nOffsetY)
{
m_nOffsetX = nOffsetX;
m_nOffsetY = nOffsetY;
}
void CHMXEdit::OnShowIM(BOOL bShow)
{
m_hmCore = (HMODULE) LoadLibrary(_T("coredll.dll"));
if(m_hmCore != NULL) {
pSipShowIM = (SIPSHOWIM) GetProcAddress(m_hmCore, _T("SipShowIM"));
pSipGetInfo = (SIPGETINFOR) GetProcAddress(m_hmCore, _T("SipGetInfo"));
pSipSetInfo = (SIPSETINFOR) GetProcAddress(m_hmCore, _T("SipSetInfo"));
if(pSipShowIM == NULL) {
FreeLibrary(m_hmCore);
m_hmCore=NULL;
}
}
//Show or Hide
if(bShow == TRUE)
{
SIPINFO si;
memset( &si, 0, sizeof( si ) );
si.cbSize = sizeof( si );
if( pSipGetInfo(&si) )
{ //Under CEdit
CRect rc;
GetWindowRect(&rc);
si.rcSipRect.top = rc.bottom;
//Center Sip
HDC hdc = ::GetDC(NULL);
int nX = ::GetDeviceCaps(hdc,HORZRES);
int nSipWidth = (si.rcSipRect.right - si.rcSipRect.left);
si.rcSipRect.left = ( nX - nSipWidth ) / 2 ;
//End of Center Sip
//Offset
si.rcSipRect.left += m_nOffsetX;
si.rcSipRect.right += m_nOffsetX;
si.rcSipRect.top += m_nOffsetY;
si.rcSipRect.bottom += m_nOffsetY;
//End of Offset
pSipSetInfo( &si );
}
pSipShowIM(1);
}
else
pSipShowIM(0);
}
BOOL CHMXEdit::SetFontBold(bool bBold)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontItalic(bool bItalic)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfItalic = bItalic ? TRUE : FALSE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontHeight(int nHeight)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontFaceName(const CString &sFaceName)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
_tcscpy( lgfnt.lfFaceName, sFaceName );
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::GetTextFont(LOGFONT *plgfnt)
{
GetFont()->GetObject( sizeof(LOGFONT), (void*)plgfnt);
return true;
}
BOOL CHMXEdit::SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString &sFaceName)
{
LOGFONT lgfnt;
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
lgfnt.lfEscapement = 0;
lgfnt.lfOrientation = 0;
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
lgfnt.lfItalic = bItalic?TRUE:FALSE;
lgfnt.lfUnderline = FALSE;
lgfnt.lfStrikeOut = FALSE;
lgfnt.lfCharSet = DEFAULT_CHARSET;
lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS;
lgfnt.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lgfnt.lfQuality = DEFAULT_QUALITY;
lgfnt.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
_tcscpy( lgfnt.lfFaceName, sFaceName );
return SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetTextFont(const LOGFONT &lgfnt)
{
m_fntText.DeleteObject();
m_fntText.CreateFontIndirect( &lgfnt );
SetFont( &m_fntText, TRUE );
return true;
}
BOOL CHMXEdit::EnableEditing(bool bEditing)
{
m_bEnableEditing = bEditing;
return true;
}
BOOL CHMXEdit::GetTextClrFocus(COLORREF &clr)
{
clr = m_clrTextFocus;
return true;
}
BOOL CHMXEdit::SetTextClrFocus(COLORREF clr)
{
m_clrTextFocus = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClrFocus(COLORREF &clr)
{
clr = m_clrBkGndFocus;
return true;
}
BOOL CHMXEdit::SetBkClrFocus(COLORREF clr)
{
m_clrBkGndFocus = clr;
m_brsBkGndFocus.DeleteObject();
m_brsBkGndFocus.CreateSolidBrush(clr);
Invalidate();
return true;
}
BOOL CHMXEdit::GetTextClr(COLORREF &clr)
{
clr = m_clrText;
return true;
}
BOOL CHMXEdit::SetTextClr(COLORREF clr)
{
m_clrText = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClr(COLORREF &clr)
{
clr = m_clrBkGnd;
return true;
}
BOOL CHMXEdit::SetBkClr(COLORREF clr)
{
m_clrBkGnd = clr;
m_brsBkGnd.DeleteObject();
m_brsBkGnd.CreateSolidBrush(clr);
Invalidate();
return true;
}
HBRUSH CHMXEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetBkColor(m_bHasFocus?m_clrBkGndFocus:m_clrBkGnd);
pDC->SetTextColor(m_bHasFocus?m_clrTextFocus:m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return m_bHasFocus?(HBRUSH)m_brsBkGndFocus:(HBRUSH)m_brsBkGnd;
}
void CHMXEdit::OnKillFocus(CWnd* pNewWnd)
{
m_bHasFocus = false;
Invalidate();
OnShowIM(FALSE);
CEdit::OnKillFocus(NULL);
}
void CHMXEdit::OnSetFocus(CWnd* pOldWnd)
{
m_bHasFocus = true;
Invalidate();
OnShowIM(TRUE);
CEdit::OnSetFocus(NULL);
}
#10
是看不到效果吧?改成下面试试:
CLogFont logFont;
logFont.SetMessageBoxFont();
logFont.lfItalic = TRUE;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
CLogFont logFont;
logFont.SetMessageBoxFont();
logFont.lfItalic = TRUE;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
#11
也还是看不到效果。
#12
To 9
因为是组件里,所以这个方法不行哈。
本来这个方法都是可以的,不知道为什么后来就不行了
http://topic.csdn.net/u/20090929/17/f486a1bf-c181-4380-bc0c-7c60822bee9e.html
因为是组件里,所以这个方法不行哈。
本来这个方法都是可以的,不知道为什么后来就不行了
http://topic.csdn.net/u/20090929/17/f486a1bf-c181-4380-bc0c-7c60822bee9e.html
#1
CWindowImpl<CMyEdit> 是啥东西???
应该是
class CMyEdit : public CEdit
应该是
class CMyEdit : public CEdit
#2
WTL
#3
这个不知道,到.Net板块问问吧
#4
1、OnPaint传进来的参数dc为空?
OnPaint的参数没用的,获取DC用CPaintDC类
2、如何给OnPaint里的dcdraw设置字体?
3、想让用户能选中字体,但蓝色背景和字体位置不一致,如何解决?
用EDIT解决你这两个问题很难,你可以把EDIT替换成CRichEdit
OnPaint的参数没用的,获取DC用CPaintDC类
2、如何给OnPaint里的dcdraw设置字体?
3、想让用户能选中字体,但蓝色背景和字体位置不一致,如何解决?
用EDIT解决你这两个问题很难,你可以把EDIT替换成CRichEdit
#5
现在字体是粗体的,我想设置普通字体如何设?
#6
SetFont
#7
看看
#8
#pragma once
#include <atlframe.h>
class CMyEdit : public CWindowImpl< CMyEdit/*, CEdit*/ >,
public CDoubleBufferImpl< CMyEdit >
{
public:
BEGIN_MSG_MAP_EX(CMyEdit)
// MSG_WM_PAINT(DoPaint)
MSG_WM_INITDIALOG(OnInitDialog)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
CHAIN_MSG_MAP(CDoubleBufferImpl< CMyEdit >)
REFLECT_NOTIFICATIONS()
END_MSG_MAP()
void DoPaint(CDCHandle dc)
{
int nContextState = dc.SaveDC();
DrawBkgnd(dc);
DrawItem(dc);
dc.RestoreDC( nContextState );
}
void DrawBkgnd(CDCHandle dc)
{
CRect rect;
dc.GetClipBox(&rect);
dc.SetBkColor(RGB(255, 255, 255));
dc.SetBkMode(TRANSPARENT);
dc.ExtTextOut( rect.left, rect.top, ETO_OPAQUE, rect, _T( "" ), 0, NULL );
}
void DrawItem(CDCHandle dc)
{
CRect rect;
dc.GetClipBox(&rect);
rect.OffsetRect(5, 0);
dc.SelectFont(m_TxtFont);
GetWindowText(m_strText);
// dc.TextOut(5, 5, m_strText);
dc.DrawText(m_strText, m_strText.GetLength(), &rect, DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
BOOL OnInitDialog ( HWND hwndFocus, LPARAM lParam )
{
CLogFont logFont;
logFont.SetMessageBoxFont();
// logFont.lfWeight = FW_THIN;
// logFont.lfHeight = 12;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
// m_TxtFont.CreateFont(12,
// 0,
// 0,
// 0,
// FW_REGULAR, //见MFC类参数
// 0,
// 0,
// 0,
// GB2312_CHARSET,
// OUT_DEFAULT_PRECIS,
// CLIP_DEFAULT_PRECIS,
// DEFAULT_QUALITY,
// DEFAULT_PITCH,
// /*SYSTEM_DEFAULT_FONT*/
// _T("宋体"));
return TRUE;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
public:
CMyEdit()
{
m_strText = _T("");
}
public:
CString m_strText;
CFont m_TxtFont;
};
dc没有SetFont成员,只有SelectFont,但不起作用。
#9
// HMXEdit.cpp : implementation file
//
#include "stdafx.h"
#include "novaprogram.h"
#include "HMXEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit
CHMXEdit::CHMXEdit()
{
m_clrBkGnd = GetSysColor(COLOR_WINDOW);
m_clrText = GetSysColor(COLOR_WINDOWTEXT);
m_clrBkGndFocus = m_clrBkGnd;
m_clrTextFocus = m_clrText;
m_brsBkGnd.CreateSolidBrush(m_clrBkGnd);
m_brsBkGndFocus.CreateSolidBrush(m_clrBkGndFocus);
m_bEnableEditing = true;
m_bHasFocus = false;
m_nOffsetX = 0;
m_nOffsetY = 0;
}
CHMXEdit::~CHMXEdit()
{
m_brsBkGnd.DeleteObject();
m_brsBkGndFocus.DeleteObject();
m_fntText.DeleteObject();
}
BEGIN_MESSAGE_MAP(CHMXEdit, CEdit)
//{{AFX_MSG_MAP(CHMXEdit)
ON_WM_CTLCOLOR()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit message handlers
LRESULT CHMXEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if( !m_bEnableEditing )
if( message == WM_PASTE || message == WM_KEYDOWN || message == WM_CHAR || message == WM_RBUTTONDOWN)
return FALSE;
return CEdit::WindowProc(message, wParam, lParam);
}
BOOL CHMXEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CEdit::PreTranslateMessage(pMsg);
}
void CHMXEdit::SetIMOffset(int nOffsetX, int nOffsetY)
{
m_nOffsetX = nOffsetX;
m_nOffsetY = nOffsetY;
}
void CHMXEdit::OnShowIM(BOOL bShow)
{
m_hmCore = (HMODULE) LoadLibrary(_T("coredll.dll"));
if(m_hmCore != NULL) {
pSipShowIM = (SIPSHOWIM) GetProcAddress(m_hmCore, _T("SipShowIM"));
pSipGetInfo = (SIPGETINFOR) GetProcAddress(m_hmCore, _T("SipGetInfo"));
pSipSetInfo = (SIPSETINFOR) GetProcAddress(m_hmCore, _T("SipSetInfo"));
if(pSipShowIM == NULL) {
FreeLibrary(m_hmCore);
m_hmCore=NULL;
}
}
//Show or Hide
if(bShow == TRUE)
{
SIPINFO si;
memset( &si, 0, sizeof( si ) );
si.cbSize = sizeof( si );
if( pSipGetInfo(&si) )
{ //Under CEdit
CRect rc;
GetWindowRect(&rc);
si.rcSipRect.top = rc.bottom;
//Center Sip
HDC hdc = ::GetDC(NULL);
int nX = ::GetDeviceCaps(hdc,HORZRES);
int nSipWidth = (si.rcSipRect.right - si.rcSipRect.left);
si.rcSipRect.left = ( nX - nSipWidth ) / 2 ;
//End of Center Sip
//Offset
si.rcSipRect.left += m_nOffsetX;
si.rcSipRect.right += m_nOffsetX;
si.rcSipRect.top += m_nOffsetY;
si.rcSipRect.bottom += m_nOffsetY;
//End of Offset
pSipSetInfo( &si );
}
pSipShowIM(1);
}
else
pSipShowIM(0);
}
BOOL CHMXEdit::SetFontBold(bool bBold)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontItalic(bool bItalic)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfItalic = bItalic ? TRUE : FALSE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontHeight(int nHeight)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontFaceName(const CString &sFaceName)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
_tcscpy( lgfnt.lfFaceName, sFaceName );
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::GetTextFont(LOGFONT *plgfnt)
{
GetFont()->GetObject( sizeof(LOGFONT), (void*)plgfnt);
return true;
}
BOOL CHMXEdit::SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString &sFaceName)
{
LOGFONT lgfnt;
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
lgfnt.lfEscapement = 0;
lgfnt.lfOrientation = 0;
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
lgfnt.lfItalic = bItalic?TRUE:FALSE;
lgfnt.lfUnderline = FALSE;
lgfnt.lfStrikeOut = FALSE;
lgfnt.lfCharSet = DEFAULT_CHARSET;
lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS;
lgfnt.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lgfnt.lfQuality = DEFAULT_QUALITY;
lgfnt.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
_tcscpy( lgfnt.lfFaceName, sFaceName );
return SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetTextFont(const LOGFONT &lgfnt)
{
m_fntText.DeleteObject();
m_fntText.CreateFontIndirect( &lgfnt );
SetFont( &m_fntText, TRUE );
return true;
}
BOOL CHMXEdit::EnableEditing(bool bEditing)
{
m_bEnableEditing = bEditing;
return true;
}
BOOL CHMXEdit::GetTextClrFocus(COLORREF &clr)
{
clr = m_clrTextFocus;
return true;
}
BOOL CHMXEdit::SetTextClrFocus(COLORREF clr)
{
m_clrTextFocus = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClrFocus(COLORREF &clr)
{
clr = m_clrBkGndFocus;
return true;
}
BOOL CHMXEdit::SetBkClrFocus(COLORREF clr)
{
m_clrBkGndFocus = clr;
m_brsBkGndFocus.DeleteObject();
m_brsBkGndFocus.CreateSolidBrush(clr);
Invalidate();
return true;
}
BOOL CHMXEdit::GetTextClr(COLORREF &clr)
{
clr = m_clrText;
return true;
}
BOOL CHMXEdit::SetTextClr(COLORREF clr)
{
m_clrText = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClr(COLORREF &clr)
{
clr = m_clrBkGnd;
return true;
}
BOOL CHMXEdit::SetBkClr(COLORREF clr)
{
m_clrBkGnd = clr;
m_brsBkGnd.DeleteObject();
m_brsBkGnd.CreateSolidBrush(clr);
Invalidate();
return true;
}
HBRUSH CHMXEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetBkColor(m_bHasFocus?m_clrBkGndFocus:m_clrBkGnd);
pDC->SetTextColor(m_bHasFocus?m_clrTextFocus:m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return m_bHasFocus?(HBRUSH)m_brsBkGndFocus:(HBRUSH)m_brsBkGnd;
}
void CHMXEdit::OnKillFocus(CWnd* pNewWnd)
{
m_bHasFocus = false;
Invalidate();
OnShowIM(FALSE);
CEdit::OnKillFocus(NULL);
}
void CHMXEdit::OnSetFocus(CWnd* pOldWnd)
{
m_bHasFocus = true;
Invalidate();
OnShowIM(TRUE);
CEdit::OnSetFocus(NULL);
}
//
#include "stdafx.h"
#include "novaprogram.h"
#include "HMXEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit
CHMXEdit::CHMXEdit()
{
m_clrBkGnd = GetSysColor(COLOR_WINDOW);
m_clrText = GetSysColor(COLOR_WINDOWTEXT);
m_clrBkGndFocus = m_clrBkGnd;
m_clrTextFocus = m_clrText;
m_brsBkGnd.CreateSolidBrush(m_clrBkGnd);
m_brsBkGndFocus.CreateSolidBrush(m_clrBkGndFocus);
m_bEnableEditing = true;
m_bHasFocus = false;
m_nOffsetX = 0;
m_nOffsetY = 0;
}
CHMXEdit::~CHMXEdit()
{
m_brsBkGnd.DeleteObject();
m_brsBkGndFocus.DeleteObject();
m_fntText.DeleteObject();
}
BEGIN_MESSAGE_MAP(CHMXEdit, CEdit)
//{{AFX_MSG_MAP(CHMXEdit)
ON_WM_CTLCOLOR()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHMXEdit message handlers
LRESULT CHMXEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if( !m_bEnableEditing )
if( message == WM_PASTE || message == WM_KEYDOWN || message == WM_CHAR || message == WM_RBUTTONDOWN)
return FALSE;
return CEdit::WindowProc(message, wParam, lParam);
}
BOOL CHMXEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CEdit::PreTranslateMessage(pMsg);
}
void CHMXEdit::SetIMOffset(int nOffsetX, int nOffsetY)
{
m_nOffsetX = nOffsetX;
m_nOffsetY = nOffsetY;
}
void CHMXEdit::OnShowIM(BOOL bShow)
{
m_hmCore = (HMODULE) LoadLibrary(_T("coredll.dll"));
if(m_hmCore != NULL) {
pSipShowIM = (SIPSHOWIM) GetProcAddress(m_hmCore, _T("SipShowIM"));
pSipGetInfo = (SIPGETINFOR) GetProcAddress(m_hmCore, _T("SipGetInfo"));
pSipSetInfo = (SIPSETINFOR) GetProcAddress(m_hmCore, _T("SipSetInfo"));
if(pSipShowIM == NULL) {
FreeLibrary(m_hmCore);
m_hmCore=NULL;
}
}
//Show or Hide
if(bShow == TRUE)
{
SIPINFO si;
memset( &si, 0, sizeof( si ) );
si.cbSize = sizeof( si );
if( pSipGetInfo(&si) )
{ //Under CEdit
CRect rc;
GetWindowRect(&rc);
si.rcSipRect.top = rc.bottom;
//Center Sip
HDC hdc = ::GetDC(NULL);
int nX = ::GetDeviceCaps(hdc,HORZRES);
int nSipWidth = (si.rcSipRect.right - si.rcSipRect.left);
si.rcSipRect.left = ( nX - nSipWidth ) / 2 ;
//End of Center Sip
//Offset
si.rcSipRect.left += m_nOffsetX;
si.rcSipRect.right += m_nOffsetX;
si.rcSipRect.top += m_nOffsetY;
si.rcSipRect.bottom += m_nOffsetY;
//End of Offset
pSipSetInfo( &si );
}
pSipShowIM(1);
}
else
pSipShowIM(0);
}
BOOL CHMXEdit::SetFontBold(bool bBold)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontItalic(bool bItalic)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfItalic = bItalic ? TRUE : FALSE;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontHeight(int nHeight)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetFontFaceName(const CString &sFaceName)
{
LOGFONT lgfnt;
GetTextFont( &lgfnt );
_tcscpy( lgfnt.lfFaceName, sFaceName );
SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::GetTextFont(LOGFONT *plgfnt)
{
GetFont()->GetObject( sizeof(LOGFONT), (void*)plgfnt);
return true;
}
BOOL CHMXEdit::SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString &sFaceName)
{
LOGFONT lgfnt;
lgfnt.lfHeight = -MulDivEx(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
lgfnt.lfEscapement = 0;
lgfnt.lfOrientation = 0;
lgfnt.lfWeight = bBold?FW_BOLD:FW_DONTCARE;
lgfnt.lfItalic = bItalic?TRUE:FALSE;
lgfnt.lfUnderline = FALSE;
lgfnt.lfStrikeOut = FALSE;
lgfnt.lfCharSet = DEFAULT_CHARSET;
lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS;
lgfnt.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lgfnt.lfQuality = DEFAULT_QUALITY;
lgfnt.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
_tcscpy( lgfnt.lfFaceName, sFaceName );
return SetTextFont( lgfnt );
return true;
}
BOOL CHMXEdit::SetTextFont(const LOGFONT &lgfnt)
{
m_fntText.DeleteObject();
m_fntText.CreateFontIndirect( &lgfnt );
SetFont( &m_fntText, TRUE );
return true;
}
BOOL CHMXEdit::EnableEditing(bool bEditing)
{
m_bEnableEditing = bEditing;
return true;
}
BOOL CHMXEdit::GetTextClrFocus(COLORREF &clr)
{
clr = m_clrTextFocus;
return true;
}
BOOL CHMXEdit::SetTextClrFocus(COLORREF clr)
{
m_clrTextFocus = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClrFocus(COLORREF &clr)
{
clr = m_clrBkGndFocus;
return true;
}
BOOL CHMXEdit::SetBkClrFocus(COLORREF clr)
{
m_clrBkGndFocus = clr;
m_brsBkGndFocus.DeleteObject();
m_brsBkGndFocus.CreateSolidBrush(clr);
Invalidate();
return true;
}
BOOL CHMXEdit::GetTextClr(COLORREF &clr)
{
clr = m_clrText;
return true;
}
BOOL CHMXEdit::SetTextClr(COLORREF clr)
{
m_clrText = clr;
Invalidate();
return true;
}
BOOL CHMXEdit::GetBkClr(COLORREF &clr)
{
clr = m_clrBkGnd;
return true;
}
BOOL CHMXEdit::SetBkClr(COLORREF clr)
{
m_clrBkGnd = clr;
m_brsBkGnd.DeleteObject();
m_brsBkGnd.CreateSolidBrush(clr);
Invalidate();
return true;
}
HBRUSH CHMXEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetBkColor(m_bHasFocus?m_clrBkGndFocus:m_clrBkGnd);
pDC->SetTextColor(m_bHasFocus?m_clrTextFocus:m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
return m_bHasFocus?(HBRUSH)m_brsBkGndFocus:(HBRUSH)m_brsBkGnd;
}
void CHMXEdit::OnKillFocus(CWnd* pNewWnd)
{
m_bHasFocus = false;
Invalidate();
OnShowIM(FALSE);
CEdit::OnKillFocus(NULL);
}
void CHMXEdit::OnSetFocus(CWnd* pOldWnd)
{
m_bHasFocus = true;
Invalidate();
OnShowIM(TRUE);
CEdit::OnSetFocus(NULL);
}
#10
是看不到效果吧?改成下面试试:
CLogFont logFont;
logFont.SetMessageBoxFont();
logFont.lfItalic = TRUE;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
CLogFont logFont;
logFont.SetMessageBoxFont();
logFont.lfItalic = TRUE;
if(m_TxtFont.CreateFontIndirect(&logFont) == NULL)
{
return TRUE;
}
#11
也还是看不到效果。
#12
To 9
因为是组件里,所以这个方法不行哈。
本来这个方法都是可以的,不知道为什么后来就不行了
http://topic.csdn.net/u/20090929/17/f486a1bf-c181-4380-bc0c-7c60822bee9e.html
因为是组件里,所以这个方法不行哈。
本来这个方法都是可以的,不知道为什么后来就不行了
http://topic.csdn.net/u/20090929/17/f486a1bf-c181-4380-bc0c-7c60822bee9e.html