PDA

View Full Version : سوال: فعال کردن DOCK برای هر یک از Panel های UserControl



md3848
جمعه 16 اسفند 1398, 15:54 عصر
سلام - من یه UserControl ایجاد کردم، که به صورت زیر عمل میکنه ( رو دکمه کلیک کنی، پنل قرمز رنگ عرضش 0 میشه یا همون غیب میشه، دوباره کلیک کنی، نمایش داده میشه، چیز ساده ای هستش )
151421

مشکلی که الان دارم اینه که، وقتی این کنترل فوق رو به پروژه اضافه میکنم، مثلا یه Button میخوام تو قسمت قرمز رنگ Dock کنم، تو کل کامپوننت فوق، Dock میشه!


اینم سورس کد، اگه نیاز دارید :

http://s7.picofile.com/file/8390250134/User_Controls.rar.html
(http://s7.picofile.com/file/8390250134/User_Controls.rar.html)
using System;using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;


namespace WindowsFormsApplication1.User_Controls
{
public partial class NewTableLayoutPanel : UserControl
{
public NewTableLayoutPanel()
{
InitializeComponent();


// set opacity [ 50% ]
Set_btnShowHide_Opacity(0.5f);
}


private void Btn_ShowHide_Click(object sender, EventArgs e)
{
if (tableLayoutPanel1.ColumnStyles[0].Width == 0)
{
tableLayoutPanel1.ColumnStyles[0].Width = 200;


btn_ShowHide.BackgroundImage = Properties.Resources.Hide;


Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}
else
{
tableLayoutPanel1.ColumnStyles[0].Width = 0;


btn_ShowHide.BackgroundImage = Properties.Resources.Show;


Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}
}
private void Btn_ShowHide_MouseEnter(object sender, EventArgs e)
{
Set_btnShowHide_Opacity(1.0f); // set opacity [ 100% ]
}
private void Btn_ShowHide_MouseLeave(object sender, EventArgs e)
{
Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}


public void Set_btnShowHide_Opacity(float opacity)
{
btn_ShowHide.BackColor = Color.Transparent;


if (tableLayoutPanel1.ColumnStyles[0].Width == 0)
{
btn_ShowHide.BackgroundImage = SetImageOpacity(Properties.Resources.Show, opacity);
}
else
{
btn_ShowHide.BackgroundImage = SetImageOpacity(Properties.Resources.Hide, opacity);
}
}
public Image SetImageOpacity(Image image, float opacity)
{
Bitmap bmp = new Bitmap(image.Width, image.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
ColorMatrix matrix = new ColorMatrix();
matrix.Matrix33 = opacity;
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
g.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, attributes);
}
return bmp;
}
}
}

md3848
جمعه 16 اسفند 1398, 18:04 عصر
خب یافتم! از SplitContainer استفاده کردم - مشکل حل شد :لبخند:
دکمه btn_ShowHide رو هم به صورت public تعریف کردم تا اگه زیرش عکسی / مپی چیزی رفت، بتونید parent شو تعیین کنید تا تو backClolor.Transparent به مشکلی بر نخورید، امیدوارم بکارتون بیاد. :لبخند::قلب:


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;


namespace WindowsFormsApplication1.User_Controls
{
public partial class NewSplitContainer : SplitContainer
{
public Button btn_ShowHide;


public NewSplitContainer()
{
InitializeComponent();






// init component
this.Name = "newSplitContainer1";
//---
this.IsSplitterFixed = true;
//---
this.Panel1.Margin = new Padding(0);
this.Panel2.Margin = new Padding(0);
//---
this.SplitterWidth = 5;
//---
this.BorderStyle = BorderStyle.FixedSingle;
//---
this.BackColor = Color.Yellow;
this.Panel1.BackColor = Color.Red;
this.Panel2.BackColor = Color.Green;
//---
this.FixedPanel = FixedPanel.Panel1;
//---
this.SplitterMoved += This_SplitterMoved; // for bug?!




// add button to component
btn_ShowHide = new Button();
this.Panel2.Controls.Add(btn_ShowHide);
btn_ShowHide.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
btn_ShowHide.BackColor = Color.Transparent;
btn_ShowHide.BackgroundImage = Properties.Resources.Hide;
btn_ShowHide.BackgroundImageLayout = ImageLayout.Stretch;
btn_ShowHide.FlatStyle = FlatStyle.Flat;
btn_ShowHide.FlatAppearance.BorderSize = 0;
btn_ShowHide.FlatAppearance.MouseDownBackColor = Color.Transparent;
btn_ShowHide.FlatAppearance.MouseOverBackColor = Color.Transparent;
btn_ShowHide.Size = new Size(15, 25);
btn_ShowHide.Location = new Point(5, this.Panel2.Height - btn_ShowHide.Height - 5);
btn_ShowHide.BringToFront();
//---
btn_ShowHide.Click += Btn_ShowHide_Click;
btn_ShowHide.MouseEnter += Btn_ShowHide_MouseEnter;
btn_ShowHide.MouseLeave += Btn_ShowHide_MouseLeave;
//---
Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}






private void This_SplitterMoved(object sender, SplitterEventArgs e)
{
btn_ShowHide.Location = new Point(5, this.Panel2.Height - btn_ShowHide.Height - 5);
}






private void Btn_ShowHide_Click(object sender, EventArgs e)
{

if (this.Panel1.Width == 0)
{
this.Panel1Collapsed = false;
this.Panel1.Show();


btn_ShowHide.BackgroundImage = Properties.Resources.Hide;


btn_ShowHide.Location = new Point(5, this.Panel2.Height - btn_ShowHide.Height - 5);


Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}
else
{
this.Panel1Collapsed = true;
this.Panel1.Hide();


btn_ShowHide.BackgroundImage = Properties.Resources.Show;


btn_ShowHide.Location = new Point(5, this.Panel2.Height - btn_ShowHide.Height - 5);


Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}
}
private void Btn_ShowHide_MouseEnter(object sender, EventArgs e)
{
Set_btnShowHide_Opacity(1.0f); // set opacity [ 100% ]
}
private void Btn_ShowHide_MouseLeave(object sender, EventArgs e)
{
Set_btnShowHide_Opacity(0.5f); // set opacity [ 50% ]
}


public void Set_btnShowHide_Opacity(float opacity)
{
btn_ShowHide.BackColor = Color.Transparent;


if (this.Panel1.Width == 0)
{
btn_ShowHide.BackgroundImage = SetImageOpacity(Properties.Resources.Show, opacity);
}
else
{
btn_ShowHide.BackgroundImage = SetImageOpacity(Properties.Resources.Hide, opacity);
}
}
public Image SetImageOpacity(Image image, float opacity)
{
Bitmap bmp = new Bitmap(image.Width, image.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
ColorMatrix matrix = new ColorMatrix();
matrix.Matrix33 = opacity;
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
g.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, attributes);
}
return bmp;
}
}
}