 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike King Guest
|
Posted: Thu May 04, 2006 7:14 pm Post subject: Painting on TEdit |
|
|
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
|
Posted: Sun May 07, 2006 1:14 pm Post subject: Re: Painting on TEdit |
|
|
"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
|
Posted: Wed May 17, 2006 6:14 pm Post subject: Re: Painting on TEdit |
|
|
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 |
|
 |
|
|
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
|
|