Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Label/StaticText
Label/StaticText [message #3877] Wed, 05 July 2006 16:43 Go to next message
arni is currently offline  arni
Messages: 2
Registered: July 2006
Junior Member
Hello,

I need to create a label, with blinking background to get user's attention. What is the easiest way to change label's background colour? I found none. Sad


arni.
Re: Label/StaticText [message #3878 is a reply to message #3877] Wed, 05 July 2006 17:22 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
Hi Arni,

I am not sure, wether i am right or not nor wether it is the best way to do it.

I would derive a class from Label and reimplement the Paint Method of StaticText
void StaticText::Paint(Draw& w)
{
 Size sz = GetSize();
 if(!IsTransparent())
  w.DrawRect(0,0,sz.cx,sz.cy,SColorFace);    
 PaintLabel(w,0,0,sz.cx,sz.cy,!IsShowEnabled(),false,false,VisibleAccessKeys()); 
}


to
void StaticText::Paint(Draw& w)
{
 Size sz = GetSize();
 if(!IsTransparent())   
  w.DrawRect(0,0,sz.cx,sz.cy,BackgroundColor);
 PaintLabel(w,0,0,sz.cx,sz.cy,!IsShowEnabled(),false,false,VisibleAccessKeys()); 
}

and add "Color BackgroundColor" and the get/set-Method to your derived Class

to get it blinking create a Callback-Function, that changes your Backgroundcolor and let it be called each n ms by setting up a TimeCallback with Ctrl::SetTimeCallback(int delay_ms, Callback cb, int id)

hope that helps and isnt too bad,
Bas
Re: Label/StaticText [message #3879 is a reply to message #3878] Wed, 05 July 2006 18:10 Go to previous messageGo to next message
arni is currently offline  arni
Messages: 2
Registered: July 2006
Junior Member

Yeah, thanks, it worked OK.
Re: Label/StaticText [message #3880 is a reply to message #3879] Wed, 05 July 2006 18:20 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
your welcome!

for all of you who need some code:
header:
class BlinkingLabel : public Label {
 private:
  Color BackgroundColor[2];
  int currentColor;
 public:
  BlinkingLabel();
  void blink();
  virtual void Paint(Draw& w);
};

source:
BlinkingLabel::BlinkingLabel() {
 currentColor=0;
 //the two Backgrounds between wich is switched
 BackgroundColor[0] = Color(255,0,0) //red
 BackgroundColor[1] = Color(255,255,255) //white
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //start blinking every second
};

void BlinkingLabel::Paint(Draw& w) {
 Size sz = GetSize();
 if(!IsTransparent())
  w.DrawRect(0,0,sz.cx,sz.cy,BackgroundColor[currentColor]);
  PaintLabel(w,0,0,sz.cx,sz.cy,!IsShowEnabled(),false,false,VisibleAccessKeys()); 
};

void BlinkingLabel::blink() {
 currentColor=1 & ++currentColor; //toggle between 0 and 1
 Refresh();
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //continue blinking every second
};


and the main:
...
BlinkingLabel bl;
bl.Transparent(false); //so we arent transparent and our background will be seen
bl.SetText("my background ist switching every second between red and white!");
...


*EDIT: The Error, wich Luigi posted was removed in the Code
have fun

[Updated on: Thu, 06 July 2006 07:38]

Report message to a moderator

Re: Label/StaticText [message #3881 is a reply to message #3880] Thu, 06 July 2006 00:48 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
fallingdutch wrote on Wed, 05 July 2006 18:20

...
have fun


Hello,

thank you for the nice code. I post here your example ready to run. Please note that one line was not recognised by the compiler. I commented it and it works the same. Please check if it is necessary. Moreover the text on the label doesn't appear.

Luigi

#include <CtrlLib/CtrlLib.h>

class BlinkingLabel : public Label {
 private:
  int currentColor;
  Color BackgroundColor[2];
public:
  BlinkingLabel();
  void blink();
  virtual void Paint(Draw& w);
};

BlinkingLabel::BlinkingLabel() {
 currentColor=0;
 //the two Backgrounds between wich is switched
 BackgroundColor[0] = Color(255,0,0); //red
 BackgroundColor[1] = Color(255,255,255); //white
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //start blinking every second
};

void BlinkingLabel::Paint(Draw& w) {
 Size sz = GetSize();
 if(!IsTransparent())
  w.DrawRect(0,0,sz.cx,sz.cy,BackgroundColor[currentColor]);
// w.DrawRect(0,0,sz.cx,sz.cy,!IsShowEnabled(),false,false,VisibleAccessKeys()); // --- problem---
};

void BlinkingLabel::blink() {
 currentColor=1 & ++currentColor; //toggle between 0 and 1
 Refresh();
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //continue blinking every second
};


GUI_APP_MAIN
{ TopWindow w;
  BlinkingLabel bl;
  w.Add(bl);
  bl.SetText("my background is switching every second between red and white!");
  bl.LeftPosZ(55, 500).TopPosZ(51, 19);
  bl.Transparent(false); //so we arent transparent and our background will be seen
  w.Run();}
Re: Label/StaticText [message #3882 is a reply to message #3881] Thu, 06 July 2006 07:28 Go to previous message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
Thanks Luigi,

You are right, that line was wrong!
If you change that line like seen here, the label will also be drawn.

it should be:
#include <CtrlLib/CtrlLib.h>

class BlinkingLabel : public Label {
 private:
  int currentColor;
  Color BackgroundColor[2];
public:
  BlinkingLabel();
  void blink();
  virtual void Paint(Draw& w);
};

BlinkingLabel::BlinkingLabel() {
 currentColor=0;
 //the two Backgrounds between wich is switched
 BackgroundColor[0] = Color(255,0,0); //red
 BackgroundColor[1] = Color(255,255,255); //white
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //start blinking every second
};

void BlinkingLabel::Paint(Draw& w) {
 Size sz = GetSize();
 if(!IsTransparent())
  w.DrawRect(0,0,sz.cx,sz.cy,BackgroundColor[currentColor]);
 PaintLabel(w,0,0,sz.cx,sz.cy, !IsShowEnabled(), false,false,VisibleAccessKeys());
};

void BlinkingLabel::blink() {
 currentColor=1 & ++currentColor; //toggle between 0 and 1
 Refresh();
 SetTimeCallback(1000,callback(this,&BlinkingLabel::blink)); //continue blinking every second
};


GUI_APP_MAIN
{ TopWindow w;
  BlinkingLabel bl;
  w.Add(bl);
  bl.SetText("my background is switching every second between red and white!");
  bl.LeftPosZ(55, 500).TopPosZ(51, 19);
  bl.Transparent(false); //so we arent transparent and our background will be seen
  w.Run();}

[Updated on: Thu, 06 July 2006 07:32]

Report message to a moderator

Previous Topic: OpenGL, SDL
Next Topic: Mapping button to CALLBACK
Goto Forum:
  


Current Time: Fri May 03 20:02:02 CEST 2024

Total time taken to generate the page: 0.02400 seconds