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

C# Discussion :

[TextBox] Textbox multiligne transparente | si possible justifié


Sujet :

C#

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut [TextBox] Textbox multiligne transparente | si possible justifié
    Bonsoir,

    je cherche avant tout, sans utiliser de lib, avoir une textbox transparente ( multiligne mais bon ca c est natif)

    et si possible qu il justifi le text ( comme on dit dans word ...)


    PS: comment on dit "justifié" en anglais ?

    voila

    merci !

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    euhh comment on fait pour ajouter la reference API:
    Win32.WM_PAINT par exemple ??


  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Citation Envoyé par alavoler Voir le message
    euhh comment on fait pour ajouter la reference API:
    Win32.WM_PAINT par exemple ??
    C'est déclaré dans le code du lien que tu as donné au post précédent a priori...

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    J ai pas vu... en tout cas le code tel quel ne compil pas
    malgres un using Microsoft.Win32; Win32 n existe pas dans le contexte actuel.... en plus c est pas de la vrai transparence il triche il passe par une image le bougre...



    j ai essaye de mettre ca dans une clase herite de textbox ....

    mais ca marche pas


    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
    return cp;
    }
    }

  6. #6
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    ah m***e, je croyais que les sources étaient disponibles en téléchargement...

    Win32 c'est une classe qu'il a déclaré, et WM_PAINT une constante qui correspond au message Windows du même nom. Tu peux trouver les valeurs WM_* (et d'autres constantes) dans le fichier WinUser.h qui est normalement placé ici :
    C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include

    La classe Win32 contient aussi la déclaration de l'API Windows SendMessage :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    ah je te remercie !

    jvais tester ça....
    mais je pense que jvais être déçu vu les commentaires :/

  8. #8
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Drawing.Imaging;
     
    namespace TransparentTextBox
    {
        public class TransparentTextBox : TextBox
        {
            PictureBox pictureBox = new PictureBox();
     
            public TransparentTextBox()
            {
                pictureBox.Dock = DockStyle.Fill;
                this.Controls.Add(pictureBox);
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                switch (m.Msg)
                {
                    case 0x000F:
     
                        Bitmap bmpCaptured =
                          new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
                        Bitmap bmpResult =
                          new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
                        Rectangle r =
                          new Rectangle(0, 0, this.ClientRectangle.Width,
                          this.ClientRectangle.Height);
     
                        CaptureWindow(this, ref bmpCaptured);
                        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                        this.BackColor = Color.Transparent;
     
                        ImageAttributes imgAttrib = new ImageAttributes();
     
                        ColorMap[] colorMap = new ColorMap[1];
     
                        colorMap[0] = new ColorMap();
     
                        colorMap[0].OldColor = Color.White;
     
                        colorMap[0].NewColor = Color.Transparent;
     
                        imgAttrib.SetRemapTable(colorMap);
     
                        Graphics g = Graphics.FromImage(bmpResult);
     
                        g.DrawImage(bmpCaptured, r, 0, 0, this.ClientRectangle.Width,
                            this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib);
     
                        g.Dispose();
     
                        pictureBox.Image = (Image)bmpResult.Clone();
                        break;
     
     
                    case 0x0114:
     
                    case 0x0115:
     
                        this.Invalidate(); // repaint
     
                        // if you use scrolling then add these two case statements
     
     
                        break;
                }
            }
     
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
            static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
     
     
            private static void CaptureWindow(Control control, ref Bitmap bitmap)
            {
                Graphics g = Graphics.FromImage(bitmap);
                int i = (int)(0x00000004L | 0x00000008L);
                IntPtr iPtr = new IntPtr(14);
                IntPtr hdc = g.GetHdc();
     
                SendMessage(control.Handle, 0x0317, hdc, iPtr);
                g.ReleaseHdc(hdc);
                g.Dispose();
            }
        }
    }
    voila du code qui compile , ca marche impec dans mon cas...

    merci tom


    bon maintenant.... il me reste plus qu'a avoir l'AutoSize, et si possible du texte justifié ( mais ca c'est le maxi bonus)

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    j'ai ajouté:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
            [BrowsableAttribute(true)]
            public override bool AutoSize
            {
                get
                {
                    return base.AutoSize;
                }
                set
                {
                    base.AutoSize = value;
                }
            }
    mais ca n'a pas vraiment l'air de fonctionner ...

  10. #10
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Citation Envoyé par alavoler Voir le message
    j'ai ajouté:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
            [BrowsableAttribute(true)]
            public override bool AutoSize
            {
                get
                {
                    return base.AutoSize;
                }
                set
                {
                    base.AutoSize = value;
                }
            }
    mais ca n'a pas vraiment l'air de fonctionner ...
    ben ça ne change rien de faire ça, si tu te contente d'appeler la propriété de la classe de base... tu veux faire quoi en fait avec ça ?

  11. #11
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    tomlev

    j'aimerais que ma public class TransparentTextBox : TextBox, se retaille automatiquement à son contenu

  12. #12
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Citation Envoyé par alavoler Voir le message
    j'aimerais que ma public class TransparentTextBox : TextBox, se retaille automatiquement à son contenu
    ah ok... en fait AutoSize est une propriété déclarée dans Control mais qui n'a de sens que pour certains contrôles, dont TextBox ne fait pas partie. Il faut donc l'overrider pour ajuster la taille en fonction du contenu, selon ta propre logique... Par exemple, quand le texte change tu peux calculer la taille du texte avec la classe TextRenderer, et donner cette taille (plus une marge) à la TextBox.

    Exemple :
    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
        public class AutoSizeTextBox : TextBox
        {
            private bool _autoSize = false;
            [Browsable(true), DefaultValue(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public override bool AutoSize
            {
                get
                {
                    return _autoSize;
                }
                set
                {
                    _autoSize = value;
                    if (_autoSize) this.Size = ComputeSize();
                }
            }
     
            protected override void OnTextChanged(EventArgs e)
            {
                base.OnTextChanged(e);
                if (this.AutoSize)
                {
                    this.Size = ComputeSize();
                }
            }
     
            protected System.Drawing.Size ComputeSize()
            {
                System.Drawing.Size s = TextRenderer.MeasureText(this.Text, this.Font);
                int width = Math.Min(Math.Max(s.Width + 3, this.MinimumSize.Width), this.MaximumSize.Width);
                int height = Math.Min(Math.Max(s.Height + 8, this.MinimumSize.Height), this.MaximumSize.Height);
                return new System.Drawing.Size(width, height);
            }
     
        }

  13. #13
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    Merci du coup de main, (j'aimerai aider autant que tu aides)

    Bon manque plus que de justifier le texte... mais la je crois que jvais laisser tomber pour l'option

    Je posterais le code propre de cette textbox d'ici peu !

    Merci

  14. #14
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Citation Envoyé par alavoler Voir le message
    Bon manque plus que de justifier le texte... mais la je crois que jvais laisser tomber pour l'option
    Dommage que tu ne codes pas en WPF, ça aurait été plus simple... l'énumération TextAlignment de WPF contient un membre Justify, et l'AutoSize est aussi géré nativement

  15. #15
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    eheh...

    j'ai vu en effet que pas mal de choses etaient gérée nativement avec le 3.5...

    mais ce n'est pas moi qui choisit la techno, helas...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. TEXTBOX : controle sur deux formats possible
    Par Ercohuma59 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 29/08/2007, 17h10
  2. TextBox et Textarea Font transparent, Possible?
    Par kahya dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 16/02/2007, 15h47
  3. [VBA-E] Défilement auto textbox multiligne ?
    Par Ender dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 28/07/2006, 16h11
  4. [VB6] Problème de textbox multiligne qui génére une erreur
    Par bb62 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 20/02/2006, 16h21
  5. [VB6] Découper un mot dans une textBox multiligne?
    Par bb62 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 09/02/2006, 11h26

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