IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Connaitre la hauteur d'un texte RTF imprimé via le RichTextBox


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Création de jeux video (en amateur)
    Inscrit en
    Mars 2003
    Messages
    408
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Cantal (Auvergne)

    Informations professionnelles :
    Activité : Création de jeux video (en amateur)
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2003
    Messages : 408
    Par défaut Connaitre la hauteur d'un texte RTF imprimé via le RichTextBox
    Bonjour,

    J'utilise cette méthode

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    // C#
    /// <summary>
    /// Calculate or render the contents of our RichTextBox for printing
    /// </summary>
    /// <param name="measureOnly">If true, only the calculation is performed,
    /// otherwise the text is rendered as well</param>
    /// <param name="e">The PrintPageEventArgs object from the
    /// PrintPage event</param>
    /// <param name="charFrom">Index of first character to be printed</param>
    /// <param name="charTo">Index of last character to be printed</param>
    /// <returns>(Index of last character that fitted on the
    /// page) + 1</returns>
    public int FormatRange(bool measureOnly, PrintPageEventArgs e,
                           int charFrom, int charTo)
    {
        // Specify which characters to print
        STRUCT_CHARRANGE cr;
        cr.cpMin = charFrom;
        cr.cpMax = charTo;
     
        // Specify the area inside page margins
        STRUCT_RECT rc;
        rc.top        = HundredthInchToTwips(e.MarginBounds.Top);
        rc.bottom    = HundredthInchToTwips(e.MarginBounds.Bottom);
        rc.left        = HundredthInchToTwips(e.MarginBounds.Left);
        rc.right    = HundredthInchToTwips(e.MarginBounds.Right);
     
        // Specify the page area
        STRUCT_RECT rcPage;
        rcPage.top    = HundredthInchToTwips(e.PageBounds.Top);
        rcPage.bottom = HundredthInchToTwips(e.PageBounds.Bottom);
        rcPage.left   = HundredthInchToTwips(e.PageBounds.Left);
        rcPage.right  = HundredthInchToTwips(e.PageBounds.Right);
     
        // Get device context of output device
        IntPtr hdc = e.Graphics.GetHdc();
     
        // Fill in the FORMATRANGE struct
        STRUCT_FORMATRANGE fr;
        fr.chrg      = cr;
        fr.hdc       = hdc;
        fr.hdcTarget = hdc;
        fr.rc        = rc;
        fr.rcPage    = rcPage;
     
        // Non-Zero wParam means render, Zero means measure
        Int32 wParam = (measureOnly ? 0 : 1);
     
        // Allocate memory for the FORMATRANGE struct and
        // copy the contents of our struct to this memory
        IntPtr lParam = Marshal.AllocCoTaskMem( Marshal.SizeOf( fr ) ); 
        Marshal.StructureToPtr(fr, lParam, false);
     
        // Send the actual Win32 message
        int res = SendMessage(Handle, EM_FORMATRANGE, wParam, lParam);
     
        // Free allocated memory
        Marshal.FreeCoTaskMem(lParam);
     
        // and release the device context
        e.Graphics.ReleaseHdc(hdc);
     
        return res;
    }
    trouvée ici : http://msdn.microsoft.com/en-us/library/ms996492.aspx

    mais si mon texte c'est "a", ce que je récupère dans fr.rc.Bottom, c'est toujours la taille que je lui ai donné au début : (HundredthInchToTwips(e.MarginBounds.Bottom))

    Sous Delphi ils récupèrent pourtant bien la hauteur réelle du texte imprimé.

    http://stackoverflow.com/questions/3...edit-in-delphi

    Et dans la doc microsoft, ils disent qu'on devrait pouvoir récupérer cette valeur.

    http://msdn.microsoft.com/fr-fr/libr...(v=vs.85).aspx

    Pourquoiça ne fonctionne pas en C# ???


    D'avance merci,

  2. #2
    Membre éclairé
    Homme Profil pro
    Création de jeux video (en amateur)
    Inscrit en
    Mars 2003
    Messages
    408
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Cantal (Auvergne)

    Informations professionnelles :
    Activité : Création de jeux video (en amateur)
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2003
    Messages : 408
    Par défaut
    Début de réponse.

    http://objectmix.com/dotnet/104079-s...rectangle.html

    StrucureToPtr fait une copie, il faut faire PtrToStructure après.


    Problème : il s'arrête de calculer la hauteur, dès qu'il trouve un retour chariot. (et y'en a pas mal dans un texte...)

    Là je ne sais plus quoi faire.

Discussions similaires

  1. [WD17] Lancer Thunderbird avec un texte RTF rempli via WinDev
    Par Jeje9 dans le forum WinDev
    Réponses: 9
    Dernier message: 15/10/2012, 09h29
  2. [Access] Enregistrer des données de type Texte RTF
    Par portu dans le forum Bases de données
    Réponses: 22
    Dernier message: 27/10/2005, 22h54
  3. [Composant] Editeur de textes RTF
    Par Bideul dans le forum Composants
    Réponses: 8
    Dernier message: 25/07/2005, 13h57
  4. Connaître la hauteur d'un texte multilignes
    Par Crisanar dans le forum Windows
    Réponses: 2
    Dernier message: 22/11/2004, 23h01
  5. Connaitre la largeur d'un texte en pixels sur un DC
    Par Harry_polin dans le forum Windows
    Réponses: 3
    Dernier message: 11/05/2004, 13h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo