Edit Control with Icon and Background Text

来源:互联网 发布:破解单位网络限制 编辑:程序博客网 时间:2024/04/29 04:05
Edit Control with Icon and Background Text - 蓝色火焰 - 水木博客

The same controls look like the following when text is entered.

Edit Control with Icon and Background Text - 蓝色火焰 - 水木博客

 

代码:


 

#pragma once


// CSymbolEdit

class CSymbolEdit : public CEdit
{
 DECLARE_DYNAMIC(CSymbolEdit)

   CFont m_fontPrompt;
   HICON m_hSymbolIcon;
   bool m_bInternalIcon;
   CString m_strPromptText;
   COLORREF m_colorPromptText;

   void DestroyIcon();

public:
 CSymbolEdit();
 virtual ~CSymbolEdit();

   void SetSymbolIcon(HICON hIcon, BOOL redraw = TRUE);
   void SetSymbolIcon(UINT id, BOOL redraw = TRUE);

   void SetPromptText(CString text, BOOL redraw = TRUE);
   void SetPromptText(LPCTSTR szText, BOOL redraw = TRUE);

   void SetPromptTextColor(COLORREF color, BOOL redraw = TRUE);

   void SetPromptFont(CFont& font, BOOL redraw = TRUE);
   void SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw = TRUE);

protected:
   void RecalcLayout();
   virtual void PreSubclassWindow();

   afx_msg void OnSize(UINT nType, int cx, int cy);
   afx_msg LRESULT OnSetFont(WPARAM wParam, LPARAM lParam);

 DECLARE_MESSAGE_MAP()

public:
   afx_msg void OnPaint();
};

 

// AeroEdit.cpp : implementation file
//

#include "stdafx.h"
#include "SymbolEdit.h"

// CSymbolEdit

IMPLEMENT_DYNAMIC(CSymbolEdit, CEdit)

CSymbolEdit::CSymbolEdit():
m_hSymbolIcon(NULL),
m_bInternalIcon(false),
m_colorPromptText(RGB(127,127,127))
{
 m_fontPrompt.CreateFont(
  16,                        // nHeight
  0,                         // nWidth
  0,                         // nEscapement
  0,                         // nOrientation
  FW_NORMAL,                 // nWeight
  TRUE,                      // bItalic
  FALSE,                     // bUnderline
  0,                         // cStrikeOut
  DEFAULT_CHARSET,           // nCharSet
  OUT_DEFAULT_PRECIS,        // nOutPrecision
  CLIP_DEFAULT_PRECIS,       // nClipPrecision
  DEFAULT_QUALITY,           // nQuality
  DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
  _T("Calibri"));
}

CSymbolEdit::~CSymbolEdit()
{
 DestroyIcon();
}


BEGIN_MESSAGE_MAP(CSymbolEdit, CEdit)
 ON_WM_PAINT()
 ON_MESSAGE(WM_SETFONT, OnSetFont)
END_MESSAGE_MAP()

void CSymbolEdit::DestroyIcon()
{
 // if icon was loaded internally, destroy it
 if(m_bInternalIcon || m_hSymbolIcon != NULL)
  ::DestroyIcon(m_hSymbolIcon);
}

void CSymbolEdit::PreSubclassWindow()
{
 RecalcLayout();
}

void CSymbolEdit::SetSymbolIcon(HICON hIcon, BOOL redraw)
{
 DestroyIcon();

 m_hSymbolIcon = hIcon;

 // icon was not loaded internally
 m_bInternalIcon = false;

 RecalcLayout();

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetSymbolIcon(UINT id, BOOL redraw)
{
 DestroyIcon();

 m_hSymbolIcon = (HICON)::LoadImage(
  AfxGetResourceHandle(),
  MAKEINTRESOURCE(id),
  IMAGE_ICON,
  16,
  16,
  LR_DEFAULTCOLOR|LR_LOADTRANSPARENT);

 ASSERT(m_hSymbolIcon != NULL);

 // icon was loaded internally
 m_bInternalIcon = true;

 RecalcLayout();

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetPromptText(CString text, BOOL redraw)
{
 m_strPromptText = text;

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetPromptText(LPCTSTR szText, BOOL redraw)
{
 m_strPromptText = szText;

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetPromptTextColor(COLORREF color, BOOL redraw)
{
 m_colorPromptText = color;

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetPromptFont(CFont& font, BOOL redraw)
{
 LOGFONT lf;
 memset(&lf, 0, sizeof(LOGFONT));

 font.GetLogFont(&lf);
 SetPromptFont(&lf);

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw)
{
 m_fontPrompt.DeleteObject(); 
 m_fontPrompt.CreateFontIndirect(lpLogFont);

 if(redraw)
  Invalidate(TRUE);
}

void CSymbolEdit::RecalcLayout()
{
 int width = GetSystemMetrics( SM_CXSMICON );

 if(m_hSymbolIcon)
 {
  DWORD dwMargins = GetMargins();
  SetMargins(LOWORD(dwMargins), width + 6);
 }
}

// CSymbolEdit message handlers

void CSymbolEdit::OnPaint()
{
 CPaintDC dc(this);

 CRect rect;
 GetClientRect( &rect );

 // Clearing the background
 dc.FillSolidRect( rect, GetSysColor(COLOR_WINDOW) );

 DWORD dwMargins = GetMargins();

 if( m_hSymbolIcon )
 {
  // Drawing the icon
  int width = GetSystemMetrics( SM_CXSMICON );
  int height = GetSystemMetrics( SM_CYSMICON );

  ::DrawIconEx(
   dc.m_hDC,
   rect.right - width - 1,
   1,
   m_hSymbolIcon,
   width,
   height,
   0,
   NULL,
   DI_NORMAL);

  rect.left += LOWORD(dwMargins) + 1;
  rect.right -= (width + 7);
 }
 else
 {
  rect.left += (LOWORD(dwMargins) + 1);
  rect.right -= (HIWORD(dwMargins) + 1);
 }

 CString text;
 GetWindowText(text);
 CFont* oldFont = NULL;

   rect.top += 1;

 if(text.GetLength() == 0)
  
  if(this != GetFocus() && m_strPromptText.GetLength() > 0)
  {
   oldFont = dc.SelectObject(&m_fontPrompt);
   COLORREF color = dc.GetTextColor();
   dc.SetTextColor(m_colorPromptText);
   dc.DrawText(m_strPromptText, rect, DT_LEFT|DT_SINGLELINE|DT_EDITCONTROL);
   dc.SetTextColor(color);
   dc.SelectObject(oldFont);
  }
 }
 else
 {
  oldFont = dc.SelectObject(GetFont());
  dc.DrawText(text, rect, DT_SINGLELINE|DT_INTERNAL|DT_EDITCONTROL);
  dc.SelectObject(oldFont);
 }
}

void CSymbolEdit::OnSize(UINT nType, int cx, int cy)
{
 CEdit::OnSize(nType, cx, cy);

 RecalcLayout();
}

LRESULT CSymbolEdit::OnSetFont(WPARAM wParam, LPARAM lParam)
{
 DefWindowProc(WM_SETFONT, wParam, lParam);

 RecalcLayout();

 return 0;
}

 

How to Use It in Your Code

 

  1. In your dialog class header, include "SymbolEdit.h".

  2. Declare a variable of type CSymbolEdit.

    CSymbolEdit m_edit;

  3. Map it to the control in DoDataExchange:

    void CEditDemoDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT, m_edit);}

  4. Set the icon, text, color, or font (in OnInitDialog, for example).

    m_edit.SetSymbolIcon(IDI_SEARCH, FALSE);m_edit.SetPromptText(_T("Find "), FALSE);m_edit.SetPromptTextColor(RGB(192, 192, 192));

转自http://jianhai1229.blog.163.com/blog/static/3488700020086814315279/

0 0
原创粉丝点击