Avatar billede danm Nybegynder
27. august 2007 - 17:12 Der er 3 kommentarer og
1 løsning

Oversætte C# kode (Der fortæller hvordan der printes RTF)

Jeg vil gerne udskrive teksten i en Richtextbox med rigtig fonte og farver, og har så endelig fundet et eksempel på hvordan dette kan gøres. Det er så desværre i C# så jeg har lidt svært ved at læse det. Hvis der er en der kan oversætte det, eller har et andet eksempel på hvordan jeg kan printe RTF, vil dette være skønt.. (o=
Her er C# koden

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// Get device context for the printer
Graphics g = e.Graphics;
// Pass the device context to the function that
// draws the RichTextBox to the printer
lastChar = DrawEditControl(g, lastChar);
if (lastChar != -1)
e.HasMorePages = true;
else
{
e.HasMorePages = false;
lastChar = 0;
}
}

private int lastChar = 0;
private int DrawEditControl(Graphics g, int lastChar)
{
// draw the control by selecting each character and deterimining its
// color
int xPos = 10;
int yPos = 40;
int kMargin = 50;
// start from the last position of the previous page
for (int c = lastChar; c < richTextBox1.Text.Length; c++)
{
// Select a single character and retrieve the color and font
richTextBox1.Select(c,1);
char nextChar = richTextBox1.Text[c];
Color theColor = richTextBox1.SelectionColor;
Font theFont = richTextBox1.SelectionFont;
// Determine the character height from the font
int height = theFont.Height;
// if the next character is a return character, increment the Y
// Position
if (nextChar == '\n')
{
// add to height on return characters
yPos += (height + 3);
xPos = 10; // Get the height of the default print page
int paperHeight = printDocument1.PrinterSettings.
DefaultPageSettings.PaperSize.Height;
// Test to see if we went past the bottom margin of the page
if (yPos > paperHeight - kMargin)
return c;
}
// if the next character is a space or tab, increment the horizontal
// position by half the height
else if ((nextChar == ' ') || (nextChar == '\t'))
{
xPos += theFont.Height/2;
}
else
{
Regex r = new Regex(@"\w",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match m;
string nextWord = "";
bool reduceAtEnd = false;
m = r.Match(nextChar.ToString());
// Determine if next character is alpha numeric
if (m.Success)
reduceAtEnd = true;
else
nextWord = nextChar.ToString();
// use a regular expression matching alphanumerics
// until a whole word is formed
// by printing the whole word, rather than individual
// characters, this way the characters will be spaced
// better in the printout
while (m.Success)
{
nextWord += nextChar;
c++;
nextChar = richTextBox1.Text[c];
m = r.Match(nextChar.ToString());
}
if (reduceAtEnd)
{
c--;
}
// Draw the string at the current x position with the current font
// and current selection color
g.DrawString(nextWord, theFont, new SolidBrush(theColor),
xPos, yPos);
// Measure the length of the string to see where to advance the next
// horizontal position
SizeF thesize = g.MeasureString(nextWord, theFont);
// Increment the x position by the size of the word
xPos += (int)thesize.Width - 4;
}
}
// All characters in the RichTextBox have been visited, return -1
return -1;
}

Og link til siden hvis dette ikke er nok..

http://www.c-sharpcorner.com/UploadFile/mgold/SimpleColorSyntaxCodeEditorPartI11212005052757AM/SimpleColorSyntaxCodeEditorPartI.aspx
Avatar billede arne_v Ekspert
27. august 2007 - 17:23 #1
Avatar billede danm Nybegynder
31. august 2007 - 14:42 #2
Jeg havde prøvet en, men den kunne oversætte det hele.. Den du linker til var noget bedre, og den kunne oversætte det hele.. Takker herfra!
Smid endelig et svar, så jeg lige kan lukke tråden..

Forresten arne_V, kender du en anden/bedre metode til at printe RTF tekstboksen?
Avatar billede arne_v Ekspert
31. august 2007 - 18:10 #3
svar
Avatar billede arne_v Ekspert
31. august 2007 - 18:14 #4
Nej. Jeg har slet ikke erfaringer med at printe.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester