Re: [RESOLVED] flickering problem Button with PNG image.
Hi Guys,
I have tried to implement a mix of both of your solution in a custom control, but I am still getting the flicker however very tiny like from the Cthulu's solution, probably Eri's idea doesn't work correctly here or I have done something wrong.
Here is the code of the user control that i am dragging then to the form.
Code:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace flicker {
/// <summary>
/// Summary for PanREAL
/// </summary>
public ref class PanREAL : public System::Windows::Forms::UserControl
{
public:
PanREAL(void)
{
InitializeComponent();
SetStyle(ControlStyles::Opaque | ControlStyles::OptimizedDoubleBuffer | ControlStyles::UserPaint , true);
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~PanREAL()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Timer^ timer1;
public:
int loc;
private: System::ComponentModel::IContainer^ components;
protected:
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(PanREAL::typeid));
this->button2 = (gcnew System::Windows::Forms::Button());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// button2
//
this->button2->BackColor = System::Drawing::Color::Transparent;
this->button2->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"button2.BackgroundImage")));
this->button2->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
this->button2->FlatAppearance->BorderSize = 0;
this->button2->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
this->button2->Location = System::Drawing::Point(3, 3);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(63, 60);
this->button2->TabIndex = 1;
this->button2->Text = L"button1";
this->button2->UseVisualStyleBackColor = false;
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &PanREAL::timer1_Tick);
//
// PanREAL
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::ActiveCaption;
this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"$this.BackgroundImage")));
this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
this->Controls->Add(this->button2);
this->Name = L"PanREAL";
this->Size = System::Drawing::Size(436, 412);
this->Load += gcnew System::EventHandler(this, &PanREAL::PanREAL_Load);
this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &PanREAL::PanREAL_Paint);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void PanREAL_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
System::Drawing::Rectangle temp(0,0,ClientSize.Width,ClientSize.Height );
temp.Width+=3;
temp.Height+=3;
// currentImage3 = splitContainerBajoVisual->Panel2->BackgroundImage;
e->Graphics->DrawImage(this->BackgroundImage,temp);
e->Graphics->DrawImage(this->button2->BackgroundImage, button2->Bounds);
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
loc+=5;
if(loc>600)
loc=0;
Drawing::Region ^rgToInvalidate = gcnew Drawing::Region(button2->Bounds);
button2->Location = System::Drawing::Point(loc,loc);
rgToInvalidate->Union(button2->Bounds); // Add new button area
// Invalidate the union of the old and new button areas
Invalidate(rgToInvalidate);
}
private: System::Void PanREAL_Load(System::Object^ sender, System::EventArgs^ e) {
loc=0;
timer1->Start();
}
};
}
Anyway The Eris solution applied directly on the form is 100% flicker free Good job
Re: [RESOLVED] flickering problem Button with PNG image.
Note that the trick I suggested only works if the button is invisible and the code of your user control pretty much looks like it's not. Keep in mind that in this case you need to handle the click yourself because an invisible button is not clickable. The user control class doesn't contain a click handler for the button but the button object is public, so there may be something outside that class.
BTW, for that trick to actually work, a button (or any other control for that matter) isn't even needed: If you store the button bitmap separately like I did in my version of the flicker sample project, a plain Rectangle will suffice. My code handles every aspect of mimicing a button's behaviour on its own.
Quote:
Originally Posted by
smallbit
Anyway The Eris solution applied directly on the form is 100% flicker free Good job
Thanks. :blush:
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
Eri523
Note that the trick I suggested only works if the button is invisible and the code of your user control pretty much looks like it's not. Keep in mind that in this case you need to handle the click yourself because an invisible button is not clickable. The user control class doesn't contain a click handler for the button but the button object is public, so there may be something outside that class.
Well I could bet money that it works also on button that isn't invisible (I mean has a half PNG file with some transparent parts, assigned as a background from the beginning through the preferences window), when button parent is form itself.
However I tried to reproduce it in the custom control code (source 2 posts above) and it's not working any more as i wrote above.
Anyway I got it implemented on my project already with all the button functionality and it works just fine (except for the 5% flicker :D )
Thanks again for your time!
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
smallbit
Well I could bet money that it works also on button that isn't invisible (I mean has a half PNG file with some transparent parts, assigned as a background from the beginning through the preferences window), when button parent is form itself.
You would win that bet but this is not the kind of invisibility I mean. What I mean is setting the button's Visible property to false, and only this ensures no flicker. In that state then buton isn't clickable anymore.
Re: [RESOLVED] flickering problem Button with PNG image.
smallbit, you must be doing something wrong here...
This is what I had in mind:
Code:
+----------------------------+
| Form1 (normal painting) |
+----------------------------+
| |
| +-------------+ |
| | |........... SpititLevel Control
| | [] | | (not moving, custom painting: background > bubbles > frame)
| | . [] | |
| +--.--------.-+ |
| . . |
| ..................... Bubbles (can be moved/animated)
| |
+----------------------------+
// Intended usage (for example, from within a containing form):
// (Note: I'm not sure how your multiple bubbles are used; I'll just call them X, and Y.)
aSpititLvl->SetXBubble(0.4); // (for example: -1 is left, 0 is level, 1 is right)
aSpiritLvl->SetYBubble(-0.12); // (same logic...)
// (u can also use properties)
So, based on what you've said before: why would the bubbles need button-like functionality anyway?
:confused:
If such functionality is not required, you can just draw the PNG image of the bubble on your user control. Keep it simple.
If it is required, Eri523's approach should work... Can you post a sample app that reproduces the problem again? I need to see what kind of flicker it is. Unless you manage to fix it after this, of course.
@Eri523:
Quote:
But let me suggest an alternative approach, [...] which is completely flicker-free.
Nice! Good job. :thumb:
1 Attachment(s)
Re: [RESOLVED] flickering problem Button with PNG image.
Well I have never written that this is still bubble case :D
This is different part already therefore I created a new thread here.
This is gonna be some sort of joystick that does two things,
- reflects the state of the game pad joystick.
- can be used as a virtual joystick using mouse.
However in the bubble case while i'm moving them there is no flicker at all although I haven't written any custom methods for that.
I adjust the sample code of the button in the panel creating flicker.
1 Attachment(s)
Re: [RESOLVED] flickering problem Button with PNG image.
Completing the changes analoguos to my suggestions in post #14 (that you already applied partially), just applying them to PanREAL instead of the form class eliminates flicker here as well.
I have attached a ZIP file containing the two modified project files (PanREAL.h and PanREAL.cpp) to this post.
This time I decided to implement the two new member functions in the .cpp file instead of the .h file to avoid the common circular inclusion problem in case you want to make the actual button click handler a member of the form class. Also, I implemented the panel's click handler as a base class method override instead of an event handler (mainly to avoid having to manually deal with delegates):
Code:
// Mouse click handler of the panel - this time implemented as base class member override
void PanREAL::OnMouseClick(MouseEventArgs ^e)
{
// If the click hits the invisible button explicitly invoke its Click handler
if (e->Button == Windows::Forms::MouseButtons::Left && button2->Bounds.Contains(e->Location))
TheButtonHasBeenClicked(this, e);
// Alternatively static_cast<Form1 ^>(Parent)->TheButtonHasBeenClicked(this, e);
// to call an equivalent (public) handler in the form class
}
Calling the actual button click handler could also be implemented using delegates here (like the .NET framework handles this) but that would be a bit more complicated and would only gain something in case you want to reuse the panel class, because then it wouldn't need to know anything about its parent class.
Unfortunately, my IDE refused to open both the form and the panel of the converted project in design view this time. It complained about an exception of type System::Exception (huh, the most informative exception type of them all... :rolleyes:) having been thrown and the call stack showed lots of stuff from the Microsoft::VisualC namespace I never heard of... :o
The only change I needed to apply to the design was to set button2->Visible to false though, and I manually edited that into PanREAL::InitializeComponent(). For this reason the caveats I mentioned in post #14 about replacing/modifying .h files used by the Forms Designer apply even more here.
All new stuff is marked with comments beginning with *****.
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
smallbit
Well I have never written that this is still bubble case :D
:D(:rolleyes:)->:mad:
(translation: I made a big grin (but a sarcastic one, cause I'm actually angry.))
Why didn't you say so in the first place?! How can one provide you with meaningful help if you haven't clearly stated both what is the problem and what the requirements are?
Well, at least not all of the discussion was off-track.
:lol:
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
TheGreatCthulhu
:D(:rolleyes:)->:mad:
(translation: I made a big grin (but a sarcastic one, cause I'm actually angry.))
Why didn't you say so in the first place?! How can one provide you with meaningful help if you haven't clearly stated both what is the problem and what the requirements are?
Well, at least not all of the discussion was off-track.
:lol:
Well forgive me, but
Quote:
Originally Posted by
smallbit
Hi Again,
So next riddle for you guys,
The only thing that two examples had in common is a PNG file used. I'am sorry I thought it was clear. Anyway I described the problem precisely and upload an example recreating it, therefore i don't really understand why did you wrote that :
Quote:
Originally Posted by
TheGreatCthulhu
... if you haven't clearly stated both what is the problem and what the requirements are?
//////////////////////////////////////
Quote:
Originally Posted by
Eri523
The only change I needed to apply to the design was to set button2->Visible to false though, and I manually edited that into PanREAL::InitializeComponent(). For this reason the caveats I mentioned in post #14 about replacing/modifying .h files used by the Forms Designer apply even more here.
Big thanks, Now the solution is complete !!!
Thanks again guys !!!
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
smallbit
Well forgive me, but
The only thing that two examples had in common is a PNG file used. I'am sorry I thought it was clear.
Don't sweat it: if I meant it seriously, I wouldn't have used smilies to express it. :)
I somehow connected this thread to the previous one cause IIRC you mentioned that you had some flicker problems with the bubble. And a sample app did contain a bubble-like sphere; furthermore, since it was just a sample app meant to reconstruct the problem, I thought that it just had some random background.
And, yes, you did say it's another puzzle, but that doesn't automatically mean that it's not the part of the same overall problem.
Anyway, people asking questions on the forums often presume that people reading will have their mind set in a similar state, so that it is not necessary to explicitly state some things; however, this is generally not the case - people will interpret someone's words differently for various reasons. Because of that, it's best to be as explicit as possible - and not just on forums, but when discussing/documenting software in general.
Re: [RESOLVED] flickering problem Button with PNG image.
Guys I could use some tips to tune my sample app to get closer to the problem. but that's conditional. because I have noticed that is not a problem of PNG itself, because it only happens when there is another picture below as a background.
Re: [RESOLVED] flickering problem Button with PNG image.
Quote:
Originally Posted by
KieBenJ54
Guys I could use some tips to tune my sample app to get closer to the problem. but that's conditional. because I have noticed that is not a problem of PNG itself, because it only happens when there is another picture below as a background.
So please give us more details to get started with. What you already posted is practically nothing.
Also, chances are your concrete problem isn't really so closely related to what has been discussed in this more than one year old thread. Unless it's really quite similar, please start a new thread.