PDA

View Full Version : سوال: تبدیل Color به Brush



sajjadrad
سه شنبه 16 شهریور 1389, 13:56 عصر
// print variables
private string strFileName;
private StreamReader objStreamToPrint;
private Font objPrintFont;
private Brush Font_color;
private Color Force_color;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument Print_Document = new PrintDocument();
Print_Document.DocumentName = "print";
printDialog1.Document = Print_Document;
Force_color = richTextBox1.ForeColor;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
objStreamToPrint = new StreamReader(strFileName);
objPrintFont = richTextBox1.Font;
Print_Document.PrinterSettings = printDialog1.PrinterSettings;
Print_Document.PrintPage +=new PrintPageEventHandler(prtPage);
Print_Document.Print();
objStreamToPrint.Close();
objStreamToPrint = null;
}
}
private void prtPage(object sender, PrintPageEventArgs e)
{
float sngLinesPerpage = 0;
float sngVerticalPosition = 0;
int intLineCount = 0;
float sngLeftMargin = e.MarginBounds.Left;
float sngTopMargin = e.MarginBounds.Top;
string strLine;
sngLinesPerpage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics);
strLine = objStreamToPrint.ReadLine();
while ((intLineCount < sngLinesPerpage) && (strLine != null))
{
sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics));
e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, sngLeftMargin, sngVerticalPosition, new StringFormat());
intLineCount = intLineCount + 1;
if (intLineCount < sngLinesPerpage)
{
strLine = objStreamToPrint.ReadLine();
}
}
if (strLine != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}


سلام خسته نباشید.تو کد بالا برای اینکه بتونم رنگ رشته رو برای چاپ به عنوان پارامتر (Brushes.Black) بفرستم باید Force_color رو که از نوع Color هستش به نوع Brush تبدیل کنم.
لطفا کسی کمک کنه:خجالت:

hamid.shekasteh
سه شنبه 16 شهریور 1389, 14:47 عصر
سلام دقت کنید که کلاس Brush از نوع abstract class هست پس نمی تونید از اون به طور مستقیم instance بگیرید پس از یکی از کلاس هایی که از اون مشتق شده به صورت زیر استفاده کنید.


Color color = Color.Blue;
Brush brush = new SolidBrush(color);