PDA

View Full Version : سوال: TransparentPictureBox



ahmadi.edu
پنج شنبه 22 اردیبهشت 1390, 18:32 عصر
سلام به همه دوستان.لطفا کد زیر را بخونید یا اجرا کنید،البته دوتا عکس هم داره که مهم نیست چی باشه.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using WindowsApplication1;
using System.Drawing.Drawing2D;

namespace WindowsApplication1
{
class TransparentImage : System.Windows.Forms.Panel
{
public TransparentImage()
{

TabStop = false;
MaximumSize = new Size(34, 34);
}

/// <summary>
/// Gets the creation parameters.
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// do nothing
}
static PaintEventArgs PEA = null;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
PEA = e;
DrawImage(false);
}

private Image _TempImage=null;
[
Category("Drawing"),
Description("The Image Src")
]
public Image Image
{
get
{
return _TempImage;
}
set
{
_TempImage = value;
}
}

protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
//Size = new Size(34, 34);
}
private void DrawImage(bool a)
{
using (Graphics graphics = CreateGraphics())
{
graphics.Clear(Color.Transparent);
Bitmap Temp = null;
if (_TempImage == null)
Temp = new Bitmap(10, 10);
else
Temp = new Bitmap(_TempImage);
Bitmap Temp1 = null;
if (BackgroundImage == null)
{
Temp1 = new Bitmap(34, 34);
graphics.DrawImage(Temp1, new Point(0, 0));
}
else
{
Temp1 = new Bitmap(BackgroundImage);
graphics.DrawImage(Temp1, new Point(0, 0));
}
graphics.DrawImage(Temp, new Point(10, 10));


}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.BackgroundImage = global::EtahaTree.Properties.Resources.downs;
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this.BackgroundImage = global::EtahaTree.Properties.Resources.overs;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackgroundImage = null;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.BackgroundImage = null;
}
}
}


ببینید دوستان عزیز این یه شیء(یا هر چیزی که دوست دارید بگید)picturebox هست که روی هر کنترلی بیفته به صورت transparent کنترل زیرش رو نشون میده.مشکلی که من دارم اینه که وقتی background image براش میذارم و با استفاده از bitmap رسمش میکنم نمیدونم چکار کنم که هر وقت خواستم دوباره بتونم اون background image رو پاک کنم،یعنی همونطور که تو کد میبینید میخوام با mouse_leave روی شیء background image پاک بشه، حالا شما بگید چه کنم؟واین که ایا میشه این کد و کاری که من کردم، رو بهتر نوشت؟

exlord
پنج شنبه 22 اردیبهشت 1390, 20:46 عصر
اول اینو تصحیح کن ...
private void DrawImage(bool a ,Graphics g)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
PEA = e;
DrawImage(false,e.Graphics);
}

protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackgroundImage = null;
this.Invalidate();
}

Invalidate باعث Repaint شدن کنترل میشه ...

exlord
پنج شنبه 22 اردیبهشت 1390, 21:43 عصر
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackgroundImage = null;
Parent.Invalidate(this.Bounds, true);
}

ahmadi.edu
شنبه 24 اردیبهشت 1390, 09:57 صبح
اول اینو تصحیح کن ...
private void DrawImage(bool a ,Graphics g)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
PEA = e;
DrawImage(false,e.Graphics);
}

protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackgroundImage = null;
this.Invalidate();
}

Invalidate باعث Repaint شدن کنترل میشه ...
این پارامتر graphic رو برای چی اضافه کردی؟به نظر بود و نبودش فرقی نمیکنه.

exlord
شنبه 24 اردیبهشت 1390, 12:55 عصر
به جای اینکه هر بار Graphics رو بسازی بهتره که از Graphics خود کنترل استفاده بکنین ...