Fade TabPage between TabControl's tab selection
-
After searching on the internet, the possible solution that come out is by using TabPage.DrawToBitmap, put the bitmap in a PictureBox, set the position of PictureBox to the same location of TabPage, and fade it. The code is below :
System::Void tabControl1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
alphaBlend = 250;
repaint = true;// set TabPicture visibility, location, and size TabPictureBox->Visible = true; int posX = tabControl1->Location.X + tabControl1->TabPages\[tabControl1->SelectedIndex\]->Margin.Left + 1; int posY = tabControl1->Location.Y + tabControl1->ItemSize.Height + 1 + tabControl1->TabPages\[tabControl1->SelectedIndex\]->Margin.Top; TabPictureBox->Location = System::Drawing::Point(posX, posY); TabPictureBox->Size = tabControl1->TabPages\[tabControl1->SelectedIndex\]->Size; // set Bitmap TabBitmap = gcnew Bitmap(tabControl1->TabPages\[tabControl1->SelectedIndex\]->Width, tabControl1->TabPages\[tabControl1->SelectedIndex\]->Height); tabControl1->TabPages\[tabControl1->SelectedIndex\]->DrawToBitmap(TabBitmap, Rectangle(0, 0, tabControl1->TabPages\[tabControl1->SelectedIndex\]->Width, tabControl1->TabPages\[tabControl1->SelectedIndex\]->Height)); while (alphaBlend > 0) { alphaBlend = alphaBlend - 25; TabPictureBox->Refresh(); } TabPictureBox->Visible = false; repaint = false;
}
System::Void TabPictureBox_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
if (repaint)
{
if (TabBitmap != nullptr)
{
Bitmap^ temp = gcnew Bitmap(TabBitmap);
Graphics^ bitmapGraphics = Graphics::FromImage(temp);
if (alphaBlend < 0) alphaBlend = 0;
SolidBrush^ greyBrush = gcnew SolidBrush(Color::FromArgb(alphaBlend, Color::White));
bitmapGraphics->CompositingMode = CompositingMode::SourceOver;
bitmapGraphics->FillRectangle(greyBrush, Rectangle(0, 0, TabBitmap->Width, TabBitmap->Height));
e->Graphics->CompositingQuality = CompositingQuality::GammaCorrected;
e->Graphics->DrawImage(temp, 0, 0);
}
}
}The problem is that each time I select the tabPage, my CPU usage spike to 70 - 80% and the fading effect is kind of slow. Is there any solution to fix it so that I can draw the bitmap faster and with minimum CPU usage (less than 50%)? Thanks.