CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 27

Threaded View

  1. #16
    Join Date
    Mar 2011
    Posts
    27

    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&#37; flicker free Good job
    Last edited by smallbit; March 24th, 2011 at 04:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured