PDA

View Full Version : سوال: خطا در نمونه سازی از یک کلاس



رامین مرادی
چهارشنبه 02 خرداد 1397, 11:07 صبح
سلام دوستان وقت بخیر.
من یه کد وی بی پیدا کردم برای نوتیفیکیشن اونو تبدیل کردم به سی شارپ حالا یه مشکلی دارم که کد خطاشو میزارم اینجا.
این کدهای کلاس:


public class ExtendedControl :Control
{


ExtendedControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColo r, true);
}


bool m_isTransparent = false;
[Description("Gets or sets the 'real' transparency of the control.")]
public bool IsTransparent
{
get { return m_isTransparent; }
set
{
m_isTransparent = value;
if ((value == true))
{
this.BackColor = Color.Transparent;
}
Invalidate();
}
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventA rgs e)
{
base.OnPaintBackground(e);


if ((IsTransparent))
{
if (!(Parent == null))
{
int myIndex = Parent.Controls.GetChildIndex(this);
for (int i = Parent.Controls.Count - 1; i >= myIndex + 1; i += -1)
{
Control ctrl = Parent.Controls[i];
if ((ctrl.Bounds.IntersectsWith(Bounds)))
{
if ((ctrl.Visible))
{
Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height);
ctrl.DrawToBitmap(bmp, ctrl.ClientRectangle);
e.Graphics.TranslateTransform(ctrl.Left - Left, ctrl.Top - Top);
e.Graphics.DrawImage(bmp, Point.Empty);
e.Graphics.TranslateTransform(Left - ctrl.Left, Top - ctrl.Top);
bmp.Dispose();
}
}
}
}
}
}
}



اینم کد کلاس نوتیفیکیشن منیجر:



class NotificationManager
{
Timer tmrAnimation;
Timer tmrDelay;
//Control where the message will be displayed.
ExtendedControl displaycontrol = new ExtendedControl();
//Some property variables.
Color GlowColor = Color.Blue;
float alphaval = 0;
float incr = 0.1f;
bool isVisible = false;
SizeF textSize;
string msg = "";
Control prnt;
private void Control_Paint(object sender, PaintEventArgs pe)
{
//This BITMAP object will hold the appearance of the notification dialog.
//Why paint in bitmap? because we will set its opacity and paint it on the control later with a specified alpha.
Bitmap img = new Bitmap(displaycontrol.Width, displaycontrol.Height);
Graphics e = Graphics.FromImage(img);


//Set smoothing.
e.SmoothingMode = SmoothingMode.AntiAlias;


//Prepare drawing tools.
Brush bru = new SolidBrush(Color.FromArgb(50, GlowColor));
Pen pn = new Pen(bru, 6);
GraphicsPath gp = new GraphicsPath();


//Make connecting edges rounded.
pn.LineJoin = LineJoin.Round;


//Draw borders
//Outmost, 50 alpha
gp.AddRectangle(new Rectangle(3, 3, displaycontrol.Width - 10, displaycontrol.Height - 10));
e.DrawPath(pn, gp);


//level 3, A bit solid
gp.Reset();
gp.AddRectangle(new Rectangle(5, 5, displaycontrol.Width - 14, displaycontrol.Height - 14));
e.DrawPath(pn, gp);


//level 2, a bit more solid
gp.Reset();
gp.AddRectangle(new Rectangle(7, 7, displaycontrol.Width - 18, displaycontrol.Height - 18));
e.DrawPath(pn, gp);


//level 1, more solidness
gp.Reset();
gp.AddRectangle(new Rectangle(9, 9, displaycontrol.Width - 22, displaycontrol.Height - 22));
e.DrawPath(pn, gp);


//Draw Content Rectangle.
gp.Reset();
bru = new SolidBrush(Color.FromArgb(7, 7, 7));
pn = new Pen(bru, 5);
pn.LineJoin = LineJoin.Round;
gp.AddRectangle(new Rectangle(8, 8, displaycontrol.Width - 20, displaycontrol.Height - 20));
e.DrawPath(pn, gp);
e.FillRectangle(bru, new Rectangle(9, 9, displaycontrol.Width - 21, displaycontrol.Height - 21));


//Set COLORMATRIX (RGBAw).
//Matrix [3,3] will be the Alpha. Alpha is in float, 0(transparent) - 1(opaque).
ColorMatrix cma = new ColorMatrix();
cma.Matrix33 = alphaval;
ImageAttributes imga = new ImageAttributes();
imga.SetColorMatrix(cma);


//Draw the notification message..
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.DrawString(msg, prnt.Font, new SolidBrush(Color.FromArgb(247, 247, 247)), new Rectangle(9, 9, displaycontrol.Width - 21, displaycontrol.Height - 21), sf);


//Now, draw the content on the control.
pe.Graphics.DrawImage(img, new Rectangle(0, 0, displaycontrol.Width, displaycontrol.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imga);


//Free the memory.
cma = null;
sf.Dispose();
imga.Dispose();
e.Dispose();
img.Dispose();
bru.Dispose();
pn.Dispose();
gp.Dispose();


}


//Handles the window animation.
// ERROR: Handles clauses are not supported in C#
private void tmr_tick(object sender, EventArgs e)
{
if ((incr > 0))
{
if ((alphaval < 1))
{
if ((alphaval + incr <= 1))
{
alphaval += incr;
displaycontrol.Refresh();
}
else
{
alphaval = 1;
displaycontrol.Refresh();
tmrAnimation.Enabled = false;
tmrDelay.Enabled = true;
}
}
}
else
{
if ((alphaval > 0))
{
if ((alphaval + incr >= 0))
{
alphaval += incr;
displaycontrol.Refresh();
}
else
{
alphaval = 0;
tmrAnimation.Enabled = false;
tmrAnimation.Dispose();
tmrDelay.Dispose();
displaycontrol.Dispose();
incr = 0.1f;
isVisible = false;
}
}
}
}


//handles the delay.
// ERROR: Handles clauses are not supported in C#
private void tmrDelay_tick(object sender, EventArgs e)
{
incr = -0.1f;
tmrAnimation.Enabled = true;
tmrDelay.Enabled = false;
}
public void Show(ref Control Parent, string Message, Color glw, int delay)
{
if (!(isVisible))
{
isVisible = true;
prnt = Parent;
msg = Message;
//Set up notification window.
displaycontrol = new ExtendedControl();
displaycontrol.IsTransparent = true;


//Measure message
textSize = displaycontrol.CreateGraphics().MeasureString(Mess age, Parent.Font);
displaycontrol.Height = (int)25 + (int)textSize.Height;
displaycontrol.Width = (int)35 + (int)textSize.Width;
if ((textSize.Width > Parent.Width - 100))
{
displaycontrol.Width = Parent.Width - 100;
int hf = (int)textSize.Width / (Parent.Width - 100);
displaycontrol.Height += (int)(textSize.Height * hf);
}


//Position control in parent
displaycontrol.Left = (Parent.Width - displaycontrol.Width) / 2;
displaycontrol.Top = (Parent.Height - displaycontrol.Height) - 50;
Parent.Controls.Add(displaycontrol);
displaycontrol.BringToFront();
GlowColor = glw;


//Set up animation
tmrAnimation = new Timer();
tmrAnimation.Interval = 15;
tmrAnimation.Enabled = true;


tmrDelay = new Timer();
tmrDelay.Interval = delay;
}
else
{
tmrDelay.Stop();
tmrDelay.Start();
}
}


}





که روی خط ExtendedControl displaycontrol = new ExtendedControl(); این خطا رو میده




Error 1 'WindowsFormsApplication2.ExtendedControl.Extended Control()' is inaccessible due to its protection level D:\WindowsFormsApplication2\WindowsFormsApplicatio n2\NotificationManager.cs 16 42 WindowsFormsApplication2






ممنون میشم راهنماییم کنید بدجور لنگ اینم.:افسرده:

رامین مرادی
چهارشنبه 02 خرداد 1397, 11:41 صبح
یا اگه کسی تونست این کد وی بی رو به دی ال ال تبدیل کنه ممنون میشم. برا همه ممکنه کاربرد داشته باشه. موضوش هم پیام Toast به سبک اندرویده

بدلیل حجم زیاد از لینک زیر دانلود کنید

http://s9.picofile.com/file/8327210450/NotificationTest.rar.html

336699
چهارشنبه 02 خرداد 1397, 19:31 عصر
یا اگه کسی تونست این کد وی بی رو به دی ال ال تبدیل کنه ممنون میشم. برا همه ممکنه کاربرد داشته باشه. موضوش هم پیام Toast به سبک اندرویده

بدلیل حجم زیاد از لینک زیر دانلود کنید

http://s9.picofile.com/file/8327210450/NotificationTest.rar.html

سلام

کدهای شما رو تبدیل به DLL کردم

ابتدا DLL رو به رفرنس ها اضافه نمایید (پروژه را بصورت x86 کامپایل کنید - دات نت 4.5)

سپس بصورت زیر می توانید استفاده نمایید.


using NotificationPlus;

private void button1_Click(object sender, EventArgs e)
{
ToastNotifier Notification = new ToastNotifier();
Notification.Show(this, "Test Notification", Color.Red, 2000);
}

Mahmoud.Afrad
چهارشنبه 02 خرداد 1397, 20:39 عصر
سلام دوستان وقت بخیر.
...
که روی خط ExtendedControl displaycontrol = new ExtendedControl(); این خطا رو میده




Error 1 'WindowsFormsApplication2.ExtendedControl.Extended Control()' is inaccessible due to its protection level D:\WindowsFormsApplication2\WindowsFormsApplicatio n2\NotificationManager.cs 16 42 WindowsFormsApplication2






ممنون میشم راهنماییم کنید بدجور لنگ اینم.:افسرده:

سازنده کلاس را public کنید.

رامین مرادی
پنج شنبه 03 خرداد 1397, 08:11 صبح
سازنده کلاس را public کنید.

دیشب خودم به این اشتباهم پی بردم :لبخند:(آبروم رفت)

رامین مرادی
پنج شنبه 03 خرداد 1397, 08:12 صبح
سلام

کدهای شما رو تبدیل به DLL کردم

ابتدا DLL رو به رفرنس ها اضافه نمایید (پروژه را بصورت x86 کامپایل کنید - دات نت 4.5)

سپس بصورت زیر می توانید استفاده نمایید.


using NotificationPlus;

private void button1_Click(object sender, EventArgs e)
{
ToastNotifier Notification = new ToastNotifier();
Notification.Show(this, "Test Notification", Color.Red, 2000);
}





ممنون دوست عزیز بی زحمت سورس کد رو هم قرار بدید. تا یه سری تغییرات ظاهری مثل فونت رو هم انجام بدیم.

رامین مرادی
پنج شنبه 03 خرداد 1397, 08:20 صبح
یادم رفته بود منبع این سورس رو بزارم گفتم اینجا بنویسم.

منبع: https://www.codeproject.com/Articles/442983/Android-Style-Toast-Notification-for-NET

336699
پنج شنبه 03 خرداد 1397, 14:43 عصر
ممنون دوست عزیز بی زحمت سورس کد رو هم قرار بدید. تا یه سری تغییرات ظاهری مثل فونت رو هم انجام بدیم.

کدها به زبان VB هستش
در واقع من کدها رو تغییر ندادم همون سورس رو که شما ارسال کرده بودین رو تبدیل به DLL کردم (فقط یه فرم حذف شده)

در مورد تغییر فونت هم
فونت بستگی به کنترلی داره که Notification روی آن نمایش داده میشه

الان فونت فرم هرچه باشه فونت پیام هم مثل فونت فرم خواهد بود.

رامین مرادی
پنج شنبه 03 خرداد 1397, 15:00 عصر
کدها به زبان VB هستش
در واقع من کدها رو تغییر ندادم همون سورس رو که شما ارسال کرده بودین رو تبدیل به DLL کردم (فقط یه فرم حذف شده)

در مورد تغییر فونت هم
فونت بستگی به کنترلی داره که Notification روی آن نمایش داده میشه

الان فونت فرم هرچه باشه فونت پیام هم مثل فونت فرم خواهد بود.
بعله راجب فونت دقت نکرده بودم. ممنون

یه تغییر کوچیک هم بهتر بود اونم اینه که کاش نوتیفیکیشن وسط صفحه ظاهر میشد نه پایین. همچنین میزان شفافیت اون کادر.
مقدار

displaycontrol.Top = (Parent.Height - displaycontrol.Height)-50
رو به

displaycontrol.Top = (Parent.Height - displaycontrol.Height) / 2

تغییر بدید تو ماژول NotificationManager

Helpco
یک شنبه 13 خرداد 1397, 10:45 صبح
بعله راجب فونت دقت نکرده بودم. ممنون

یه تغییر کوچیک هم بهتر بود اونم اینه که کاش نوتیفیکیشن وسط صفحه ظاهر میشد نه پایین. همچنین میزان شفافیت اون کادر.
مقدار

displaycontrol.Top = (Parent.Height - displaycontrol.Height)-50
رو به

displaycontrol.Top = (Parent.Height - displaycontrol.Height) / 2

تغییر بدید تو ماژول NotificationManager


کد سی شارپ که کار کنه ندارید؟
این تغیرات چکار می کنه

Helpco
یک شنبه 13 خرداد 1397, 11:11 صبح
من تبدیل به سی شارپ کردم ولی اجرا نمیشه علت چیست

رامین مرادی
یک شنبه 13 خرداد 1397, 11:39 صبح
کد سی شارپ که کار کنه ندارید؟
این تغیرات چکار می کنه

کد سی شارپ ندارم. ولی همین کدی که تبدیل به دی ال ال شده رو ویرایش کردم خودم این تغییراتو اعمال کردم.
این تغییرات باعث میشه بجای اینکه پیام تو پایین صفحه ظاهر بشه دقیقا وسط فرم ظاهر بشه.

Helpco
یک شنبه 13 خرداد 1397, 12:00 عصر
کد سی شارپ ندارم. ولی همین کدی که تبدیل به دی ال ال شده رو ویرایش کردم خودم این تغییراتو اعمال کردم.
این تغییرات باعث میشه بجای اینکه پیام تو پایین صفحه ظاهر بشه دقیقا وسط فرم ظاهر بشه.

ممکن سورس بفرسید

رامین مرادی
یک شنبه 13 خرداد 1397, 12:02 عصر
تو پست های 2 و 3 لینک دانلود هست.پست دوم اصل کد هست. پست سوم دی ال ال که دوستمون زحمت تبدیلشو کشیدن

Helpco
یک شنبه 13 خرداد 1397, 12:38 عصر
کد اصلاح شده که وسط صفحه باز میشه مخواستم اگه هست

sg.programmer
یک شنبه 13 خرداد 1397, 14:44 عصر
سلام

کدهای شما رو تبدیل به DLL کردم

ابتدا DLL رو به رفرنس ها اضافه نمایید (پروژه را بصورت x86 کامپایل کنید - دات نت 4.5)

سپس بصورت زیر می توانید استفاده نمایید.


using NotificationPlus;

private void button1_Click(object sender, EventArgs e)
{
ToastNotifier Notification = new ToastNotifier();
Notification.Show(this, "Test Notification", Color.Red, 2000);
}





سلام
اگه با X64 کامپایل بشه کار نمیکنه؟