Form Opacity Changed -> Component not updating  
Author Message
Dragos Jeleriu





PostPosted: Windows Forms General, Form Opacity Changed -> Component not updating Top

First of all I apollogise for reposting this, but apparently I haven't posted it in the right place the first time. I hope that by posting here I could get an answer. So here goes:

I have created a form, and set the region of the form from a bitmap. On this form I have an PictureBox Component wich is updated by a timer. On the OnTimer Event I have some code that does the following things :

  • Gets a region of the forms background and paints it on the control
  • Gets 2 bitmaps from two different picture boxes and draws them on control

Everything worked fine until I decided to add the possibility to change the forms Opacity at runtime. The form is shown with 100% opacity at runtime and everything works as it should. When I change the opacity :

this->Opacity = 0.7;

the form is made semi-transparent but the PictureBox stops updating. The odd thing is that when I hold the left mouse button pressed on the form, the control updates as it should.

I should mention that I have the following code, which is used to move the form, in the OnMouseDown event of the form :

int WM_NCLBUTTONDOWN = 0xA1;

int HT_CAPTION = 0x2;

Win32Calls::ReleaseCapture();

Win32Calls::SendMessage(this->Handle,WM_NCLBUTTONDOWN,HT_CAPTION,0);

I've tried forcing a form redraw at each update, but aside the flicker that it creates on each timer update, when the form is first shown, it has no effect whatsoever when the opacity is changed.
One last thing I should mention is that when I change at runtime the Opacity from anything to 100% things work once again.

Thank you for your answers,
Dragos



Windows Forms12  
 
 
nobugz





PostPosted: Windows Forms General, Form Opacity Changed -> Component not updating Top

We'd need to see some code to help you trouble-shoot this, especially for the timer event handler. However, you clearly have a refresh/paint problem. Your OnMouseDown event would cause the form to be repainted. Painting directly to a PB at runtime is not a good idea, you should let the Paint event do the painting. This is especially important when you use Opacity, Windows needs to ensure that the background image is accurate to properly blend the background with the foreground...


 
 
Dragos Jeleriu





PostPosted: Windows Forms General, Form Opacity Changed -> Component not updating Top

Thank you for your answer.

Here is the code for the OnTick Event :

private: System::Void updateTimer_Tick(System::Object^ sender, System::EventArgs^ e) {

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Digital
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

String ^Sec = gcnew String(Acum.Now.Second.ToString());
String ^Min =
gcnew String(Acum.Now.Minute.ToString());
String ^Ora =
gcnew String(Acum.Now.Hour.ToString());

if (Sec->Length < 2) Sec = Sec->Concat("0",Sec);
if (Min->Length < 2) Min = Min->Concat("0",Min);
if (Ora->Length < 2) Ora = Ora->Concat("0",Ora);
Timp = Timp->Concat(Ora,
":",Min,":",Sec);
CanvasDigital->DrawImage(
this->BackgroundImage,0,0,RectangleF((float)this->Digital->Left,(float)this->Digital->Top,(float)this->Digital->Width,(float)this->Digital->Height),GraphicsUnit::Pixel);
CanvasDigital->DrawString(Timp,MyFont,
gcnew SolidBrush(Color::GreenYellow),0,0);

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Analog
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Desen->DrawImage(this->BackgroundImage,0,0,RectangleF((float)this->Analog->Left,(float)this->Analog->Top,(float)this->Analog->Width,(float)this->Analog->Height),GraphicsUnit::Pixel);

// -=-=--=-=-=-=-=-=-=-=-=-=-=
// Hour
// -=-=-=-=-=-=-=-=-=-=-=-=-=-

Desen->TranslateTransform(60.0f,60.0f);
Desen->RotateTransform((
float)180+((30*Acum.Now.Hour)+(0.5*Acum.Now.Minute)));
Desen->DrawImage(Orar->Image,Point(-9,-9));
Desen->ResetTransform();

// -=-=--=-=-=-=-=-=-=-=-=-=-=
// Minute
// -=-=-=-=-=-=-=-=-=-=-=-=-=-

Desen->TranslateTransform(60.0f,60.0f);
Desen->RotateTransform((
float)180+(6*Acum.Now.Minute));
Desen->DrawImage(Minutar->Image,Point(-9,-13));
Desen->ResetTransform();

// -=-=--=-=-=-=-=-=-=-=-=-=-=
// Second
// -=-=-=-=-=-=-=-=-=-=-=-=-=-

Desen->TranslateTransform(60.0f,60.0f);
Desen->RotateTransform((
float)180+(6*Acum.Now.Second));
Desen->DrawImage(Minutar->Image,Point(-9,-13));
Desen->ResetTransform();

}

Also, the declarations and initialization for CanvasDigital and Desen are the following :

private: Graphics ^CanvasDigital;
private: Graphics ^Desen;

In the OnLoad event of the form :

Desen = Analog->CreateGraphics();
CanvasDigital = Digital->CreateGraphics();

Analog and Digital are Picture Boxes, ony one is visible at a time, but both react the same to the opacity change. Also Orar and Minutar are two picture boxes which hold two images that remain unchanged during the execution of the program.

This is all the drawing that is being done. If you require additional code please let me know and I will post it

Hope anyone has an ideea :).

Thanks in advance,
Dragos.


 
 
Dragos Jeleriu





PostPosted: Windows Forms General, Form Opacity Changed -> Component not updating Top

Well, problem solved. I moved the code to the OnPaint event of each Picture Box and in the Timer I invalidated the Picture Boxes at each tick so that they would update.

Thanks for the help,
Dragos.