2017-03-08 1 views
0

Je travaille dans le projet WinForms et j'ai les requêtes dans Impression du texte. J'ai donc créé l'exemple simple pour tester le problème. Dans le formulaire, j'ai dessiné la chaîne entre deux lignes manuellement en utilisant Form.Graphics et fait la même chose en utilisant PrintPageEventArgs.Graphics dans l'événement PrintDocument.PrintPage. La position de dessin est modifiée dans l'aperçu avant impression. Veuillez voir l'image ci-dessous, qui montre le problème (c'est-à-dire que le dessin des lignes est différent entre Form.Graphics et PrintPageEventArgs.Graphics). S'il vous plaît laissez-moi savoir, pourquoi la position de dessin est changé?La position du dessin est modifiée dans PrintPageEventArgs.Graphis

public Form1() 
{ 
    InitializeComponent(); 
    this.Paint += Form1_Paint; 
} 

void Document_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25); 
    e.Graphics.DrawString("Some Chars are getting Cut in Print Preview", this.Font, new SolidBrush(Color.Red), 10, 10); 
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 228, 10, 228, 25); 
} 

void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25); 
    e.Graphics.DrawString("Some Chars are getting Cut in Print Preview", this.Font, new SolidBrush(Color.Red), 10, 10); 
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 228, 10, 228, 25); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    PrintPreviewDialog ppd = new PrintPreviewDialog(); 
    PrintDocument doc = new PrintDocument(); 
    ppd.Document = doc; 
    ppd.Document.PrintPage += Document_PrintPage; 
    ppd.ShowDialog(); 
} 

enter image description here

Exemple: TestSample

Merci à l'avance.

+0

Est-ce que ma réponse a aidé? – TheLethalCoder

+0

Bonjour, je ne peux pas utiliser Graphics.MeasureStrings() dans mon niveau de production. car chaque texte d'une colonne aura une longueur différente. – Prithiv

+0

Puis mesurez toutes les cordes et prenez le maximum ... – TheLethalCoder

Répondre

1

Les positions ne seront pas toujours les mêmes, n'utilisez pas de nombres magiques. Pensez à ce qui se passerait si vous changiez la taille de la police?

Au lieu de chercher à l'aide Graphics.MeasureString:

SizeF stringSize = e.Graphics.MeasureString(theText, this.Font); 
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), stringSize.Width + 1, 10, stringSize.Width + 1, 25); 

également avec cette approche, vous pouvez utiliser le stringSize.Height pour obtenir la hauteur de la chaîne au lieu de coder en dur 25:

int lineEndY = 10 + stringSize.Height /* + anyBufferConstant*/; 

Side Note: Pen et SolidBrush mettre en œuvre IDisposable probablement préférable de les envelopper dans une instruction using, mais vous pouvez également utiliser Pens.Red pour ce cas particulier. Et vous dupliquez également le code que vous pourriez utiliser une méthode d'assistance. Mettre tout ceci:

private void DoPrintingLogic(Graphics g, string text)  
{ 
    const Point startPos = new Point(10, 10); 

    SizeF stringSize = g.MeasureString(text, this.Font); 

    using (SolidBrush redBrush = new SolidBrush(Colors.Red)) 
    { 
     g.DrawLine(Pens.Red, startPos.X, startPos.Y, startPos.X, startPos.Y + stringSize.Height); 
     g.DrawString(text, this.Font, redBrush, startPos.X, startPos.Y); 
     g.DrawLine(Pens.Red, startPos.X + stringSize.Width, startPos.Y, startPos.X + stringSize.Width, startPos.Y + stringSize.Height); 
    } 
} 

Responsabilité: Tout le code est non testé.