Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have an edit control which takes input as text. But how to draw user defined shapes onto it.

Please help.
Posted

Derive a custom control from a CEdit and then implement OnPaint

You'll get something like the following where you will use your paint dc for drawing shapes...

C#
class CShapeEdit : public CEdit
{
    DECLARE_DYNAMIC(CShapeEdit)

public:
    CShapeEdit();
    virtual ~CShapeEdit();

protected:
    DECLARE_MESSAGE_MAP()
    afx_msg void OnPaint();
};

BEGIN_MESSAGE_MAP(CShapeEdit, CEdit)
    ON_WM_PAINT()
END_MESSAGE_MAP()

IMPLEMENT_DYNAMIC(CShapeEdit, CEdit)

CShapeEdit::CShapeEdit()
{
}

CShapeEdit::~CShapeEdit()
{
}

void CShapeEdit::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CEdit::OnPaint() for painting messages
}


Now in your dialog class, which hosts the custom edit control, you add a member variable

CShapeEdit m_ShapeEdit;


In the implementation of your dialog class bind it to the UI using DDX

DDX_Control(pDX, IDC_SHAPE_EDIT, m_ShapeEdit);


Then later on cause it to redraw itself with

m_ShapeEdit.RedrawWindow();


Now you have a canvas with which to explore the Win32 GDI!!!!
 
Share this answer
 
If it is enough to draw only on the some left and/or right parts of the control,
you could use CYourEdit::SetMargins(uiLeftUntouchedPixels, uiRightUntouchedPixels)
to prevent the CEdit-content painting there, even in the edit-caret mode.
Then you could also add(at or after the creating) the WS_CLIPCHILDREN style to CYourEdit control
and place some owner-drawn CYourShapeStatic child-control(s) onto the "freed" area(s) :)

Zerr0cool wrote:
I want to draw rectangles, circles in the Edit control where the user types in.

Try to set your control in the transparent mode as shown here[^]
and then to draw the shapes on the parent's surface, "behind" the control :)
 
Share this answer
 
v3
Comments
[no name] 1-Nov-12 9:25am    
I want to draw rectangles, circles in the Edit control where the user types in.
JackDingler 1-Nov-12 13:52pm    
Perhaps you want to create a custom font and apply that?
[no name] 1-Nov-12 13:57pm    
Its not about custom font. When a user enters a text say circle then I want to display circle and so on inside the edit control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900