BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Painting on TEdit

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development)
View previous topic :: View next topic  
Author Message
Mike King
Guest





PostPosted: Thu May 04, 2006 7:14 pm    Post subject: Painting on TEdit Reply with quote



I'm trying to show two values in a TEdit control. I wanted the second value
to be painted on the background. When the user hits a specific key, I want
the background to go white/clWindow and the Text property to be toggled
between the first and second values. In the following code, I can get the
other value to show up but when I select the control the background
disappears. Does anyone know how to prevent this from happening? I don't
need help on the toggling part just the painting.

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

class TMyEdit : public TEdit
{
public:
__fastcall TMyEdit(TComponent* Owner);
__fastcall ~TMyEdit();

private:
PAINTSTRUCT ps;
TCanvas *FCanvas;

MESSAGE void __fastcall WMPaint(TMessage &Msg);

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_PAINT, TMessage, WMPaint)
END_MESSAGE_MAP(TEdit)
};

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
private: // User declarations
TMyEdit *Edit2;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif




//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit2 = new TMyEdit(this);
Edit2->Parent = this;
Edit2->Left = 30;
Edit2->Top = 0;
}
//---------------------------------------------------------------------------

__fastcall TMyEdit::TMyEdit(TComponent *Owner) : TEdit(Owner) {
FCanvas = new TCanvas();
}
//---------------------------------------------------------------------------

__fastcall TMyEdit::~TMyEdit() {
delete FCanvas;
}
//---------------------------------------------------------------------------

void __fastcall TMyEdit::WMPaint(TMessage &Msg) {
RECT UpdateR;
if (!GetUpdateRect(Handle, &UpdateR, false))
{
Msg.Result = 0;
return;
}

FCanvas->Handle = BeginPaint(Handle, &ps);
FCanvas->Brush->Color = clWhite;
::SetBkMode(FCanvas->Handle, TRANSPARENT);
FCanvas->FillRect(ClientRect);
FCanvas->TextRect(ClientRect, ClientRect.Left + 10, ClientRect.Top + 1,
Text);

TColor tclr = FCanvas->Font->Color;
FCanvas->Font->Color = clGreen;
FCanvas->TextRect(ClientRect, ClientRect.Width() / 2, ClientRect.Top + 1,
"23.23");
FCanvas->Font->Color = tclr;

EndPaint(Handle, &ps);
Msg.Result = 0;
}
//---------------------------------------------------------------------------
Back to top
JD
Guest





PostPosted: Sun May 07, 2006 1:14 pm    Post subject: Re: Painting on TEdit Reply with quote



"Mike King" <emailMK (AT) excite (DOT) com> wrote:
Quote:

[...] I don't need help on the toggling part just the
painting.

Well doesn't the painting depend on what state the component
is in and which colors and text to use to paint the background?
That information is needed to produce a working sample.

Try this:
//-------------------------------------------------------------
class TMyEdit : public TEdit
{
typedef TEdit inherited;
private:
bool FState;
void __fastcall SetState( bool );
MESSAGE void __fastcall WMPaint(TMessage &Msg);
public:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_PAINT, TMessage, WMPaint)
END_MESSAGE_MAP( inherited )
__fastcall TMyEdit(TComponent* Owner);
__fastcall ~TMyEdit();
__property bool State = { read = FState, write = SetState };
};
//-------------------------------------------------------------


#include <memory>
//-------------------------------------------------------------
__fastcall TMyEdit::TMyEdit(TComponent *Owner) : TEdit(Owner)
{
FState = false;
}
//-------------------------------------------------------------
__fastcall TMyEdit::~TMyEdit()
{
}
//-------------------------------------------------------------
void __fastcall TMyEdit::SetState( bool AState )
{
if( FState != AState )
{
FState = AState;
Invalidate();
}
}
//-------------------------------------------------------------
MESSAGE void __fastcall TMyEdit::WMPaint(TMessage &Msg)
{
if( ::GetUpdateRect(Handle, NULL, false) )
{
PAINTSTRUCT ps = { 0 };
::BeginPaint( Handle, &ps );
::ValidateRect( Handle, &ps.rcPaint );

std::auto_ptr<TCanvas> pCanvas ( new TCanvas );
pCanvas->Handle = ::GetWindowDC( Handle );
//***********************************
//
// Start drawing
//
//***********************************
TRect R;
::GetClientRect( Handle, &R );
// for normal text
pCanvas->Brush->Color = clWhite;
pCanvas->Font->Color = clBlack;
pCanvas->FillRect( R );
::DrawText( ... );
// for transparent drawing
pCanvas->Font->Color = clGreen;
int PreviouseMode = ::SetBkMode( pCanvas->Handle, TRANSPARENT );
::DrawText( ... );
::SetBkMode( pCanvas->Handle, PreviouseMode );
//***********************************
//
// end of drawing
//
//***********************************

::ReleaseDC( Handle, pCanvas->Handle );
::EndPaint( Handle, &ps );
}
else inherited::Dispatch(&Message);
}
//-------------------------------------------------------------

~ JD
Back to top
Guest






PostPosted: Wed May 17, 2006 6:14 pm    Post subject: Re: Painting on TEdit Reply with quote



would it have anything to do with the line

::SetBkMode(FCanvas->Handle, TRANSPARENT);

??
SteveS


Mike King wrote:
Quote:
I'm trying to show two values in a TEdit control. I wanted the second value
to be painted on the background. When the user hits a specific key, I want
the background to go white/clWindow and the Text property to be toggled
between the first and second values. In the following code, I can get the
other value to show up but when I select the control the background
disappears. Does anyone know how to prevent this from happening? I don't
need help on the toggling part just the painting.

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp
#include <Controls.hpp
#include <StdCtrls.hpp
#include <Forms.hpp

class TMyEdit : public TEdit
{
public:
__fastcall TMyEdit(TComponent* Owner);
__fastcall ~TMyEdit();

private:
PAINTSTRUCT ps;
TCanvas *FCanvas;

MESSAGE void __fastcall WMPaint(TMessage &Msg);

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_PAINT, TMessage, WMPaint)
END_MESSAGE_MAP(TEdit)
};

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
private: // User declarations
TMyEdit *Edit2;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif




//---------------------------------------------------------------------------

#include <vcl.h
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit2 = new TMyEdit(this);
Edit2->Parent = this;
Edit2->Left = 30;
Edit2->Top = 0;
}
//---------------------------------------------------------------------------

__fastcall TMyEdit::TMyEdit(TComponent *Owner) : TEdit(Owner) {
FCanvas = new TCanvas();
}
//---------------------------------------------------------------------------

__fastcall TMyEdit::~TMyEdit() {
delete FCanvas;
}
//---------------------------------------------------------------------------

void __fastcall TMyEdit::WMPaint(TMessage &Msg) {
RECT UpdateR;
if (!GetUpdateRect(Handle, &UpdateR, false))
{
Msg.Result = 0;
return;
}

FCanvas->Handle = BeginPaint(Handle, &ps);
FCanvas->Brush->Color = clWhite;
::SetBkMode(FCanvas->Handle, TRANSPARENT);
FCanvas->FillRect(ClientRect);
FCanvas->TextRect(ClientRect, ClientRect.Left + 10, ClientRect.Top + 1,
Text);

TColor tclr = FCanvas->Font->Color;
FCanvas->Font->Color = clGreen;
FCanvas->TextRect(ClientRect, ClientRect.Width() / 2, ClientRect.Top + 1,
"23.23");
FCanvas->Font->Color = tclr;

EndPaint(Handle, &ps);
Msg.Result = 0;
}
//---------------------------------------------------------------------------
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.