md3848
جمعه 16 اسفند 1398, 16: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;
}
}
}
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;
}
}
}