Re: How to resize an Custom class derived from CEdit when it reaches its max text limit?
Try this class, it's pretty much what Joe suggested with a few minor fixes.
#pragma once
// CDSEdit
class CDSEdit : public CEdit
{
DECLARE_DYNAMIC(CDSEdit)
public:
CDSEdit();
virtual ~CDSEdit();
protected:
afx_msg void OnEnUpdate();
DECLARE_MESSAGE_MAP()
};
//////////////////////////////////////////////////////////////////////
// DSEdit.cpp : implementation file
//
#include "stdafx.h"
#include "DSEdit.h"
// CDSEdit
IMPLEMENT_DYNAMIC(CDSEdit, CEdit)
CDSEdit::CDSEdit()
{
}
CDSEdit::~CDSEdit()
{
}
BEGIN_MESSAGE_MAP(CDSEdit, CEdit)
ON_CONTROL_REFLECT(EN_UPDATE, OnEnUpdate)
END_MESSAGE_MAP()
// CDSEdit message handlers
void CDSEdit::OnEnUpdate()
{
CClientDC dc(this);
//Get the margins
DWORD Margin = GetMargins();
WORD LMargin = LOWORD(Margin);
WORD RMargin = HIWORD(Margin);
//get the size of the text
CFont *pOldFont = dc.SelectObject(GetFont());
CString Str;
GetWindowText(Str);
CSize sz = dc.GetTextExtent(Str);
dc.SelectObject(pOldFont);
//get the inside size of the edit
CRect Rect;
GetRect(&Rect);
//get the external size of the edit
CRect WRect;
GetWindowRect(&WRect);
//if the size of the edit is different than the size of the
//text then resize.
//NOTE: you might want to set a limit to how small it gets
if ((sz.cx + LMargin + RMargin) != Rect.Width())
{
WRect.right += (sz.cx + LMargin + RMargin) - Rect.Width();
SetWindowPos(NULL, 0, 0, WRect.Width(), WRect.Height(), SWP_NOMOVE |
SWP_NOZORDER);
}
}
AliR.
<sujeet27kulk@gmail.com> wrote in message
news:3fcd0b15-831f-438f-8bff-d1ac8697b6df@w4g2000prd.googlegroups.com...
Hi, I have a class derrived from CEdit. I need to resize it when it
reaches its max text limit in order to accomodate the new character.
But if I incremant it by a fixed size there is a problem. As each
character is taking different space. like 'l' , '.' and '2' wiil
require different space. So a constant number doesen't fit the bill.
Also when I press backspace the Edit control should decreament by
exact size required by the chartacter (I fill this is somewhat more
difficult as you need to know the size of a character) back space is
going to delete.Please help if anybody has the solution.