Smiley animé

benoit57320 Messages postés 16 Date d'inscription mardi 10 mars 2015 Statut Membre Dernière intervention 23 janvier 2016 - 2 sept. 2015 à 08:50
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 - 6 sept. 2015 à 12:59
Bonjour, à vous

Est-il possible de mettre enfin insérer un smiley animé dans une richtextbox ?
Pour l'insertion d'un gif pas de problème j'ai le code il fonctionne, le problème se trouve juste sur la visualisation des gif animé dans la richtextbox.

Après j'utilise pas la richtextbox de base j'utilise une autre basé sur la richtextbox avec les fonction d'impression formaté (RichtextboxCtrlPrint donné sur le forum de microsoft)

Je suppose que pour les gif animé il faut passé par une nouvelle librairie de class ? Y a-t-il une autre possibilité, des piste ou un controler fini (dll) disponible

Je code en vb.net, framework 3.5, c'est pour un logiciel de chat, les autre fonction ne pose pas de problème

Merci d'avance
Benoit

1 réponse

benoit57320 Messages postés 16 Date d'inscription mardi 10 mars 2015 Statut Membre Dernière intervention 23 janvier 2016
4 sept. 2015 à 12:04
Personne ...... dommage qu'on voit pas le nombres de vues .....
Enfin bref j'ai trouver ma solution :

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace RichTextBoxPrint
{
    public class RichTextBoxPrint : RichTextBox
    {
 
        const int MaximumTextLength = 10000;
        const int TextLengthToBeRemoved = 3000;
        const int MaximumNoOfControl = 50;
        const int NoOfControlToBeRemoved = 20;
        bool KeepShort = true;
 
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, ref Point pt);
        const int EM_GETSCROLLPOS = 0x0400 + 221;
 
        List<MetaInfo> ControlList = new List<MetaInfo>();
 
        public RichTextBoxPrint()
            : base()
        {
            this.VScroll += new EventHandler(RichTextBoxPrint_VScroll);
            this.SizeChanged += new EventHandler(RichTextBoxPrint_SizeChanged);
            this.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.RichTextBoxPrint_LinkClicked);
        }
 
        //Contains the initial position data of a control relative to the text content.
        internal class MetaInfo
        {
            int charIndex;
            public int CharIndex
            {
                get { return charIndex; }
                set { charIndex = value; }
            }
 
            int deltaY;
            public int DeltaY
            {
                get { return deltaY; }
                set { deltaY = value; }
            }
 
            Control theControl;
            public Control TheControl
            {
                get { return theControl; }
                set { theControl = value; }
            }
 
            public MetaInfo(Control theControl)
            {
                this.theControl = theControl;
            }
 
        }
 
        public void AddControl(Control oneControl)
        {
            // Obtain the initial metadata.
            MetaInfo one = new MetaInfo(oneControl);
            base.Controls.Add(oneControl);
            one.TheControl.Location = this.GetPositionFromCharIndex(this.SelectionStart);
            one.DeltaY = this.GetPositionFromCharIndex(0).Y - one.TheControl.Location.Y;
            ControlList.Add(one);
 
            RemoveSome();
            AutoScroll();
        }
 
        public void AutoScroll()
        {
            this.SelectionStart = this.TextLength - 1;
            this.ScrollToCaret();
        }
 
        private void RemoveSome()
        {
            //Optional. 
            //Remove some text and control if too many, to release system resources and improve performance.
            if (!KeepShort)
            {
                return;
            }
 
            int texttoRemove = 0;
            int imgtoRemove = 0;
            try
            {
                if (this.TextLength > MaximumTextLength)
                {
                    texttoRemove = TextLengthToBeRemoved;
                    this.Text = this.Text.Substring(texttoRemove);
                    texttoRemove += this.Text.IndexOf("\n");
                    if (texttoRemove > TextLengthToBeRemoved)
                    {
                        this.Text = this.Text.Substring(texttoRemove - TextLengthToBeRemoved);
                    }
 
                    foreach (MetaInfo oldone in ControlList)
                    {
                        if (oldone.CharIndex < texttoRemove)
                        {
                            imgtoRemove++;
                        }
                        else
                        {
                            oldone.CharIndex -= texttoRemove;
                        }
                    }
 
                    for (int i = 0; i < imgtoRemove; i++)
                    {
                        this.Controls[0].Dispose();
                        ControlList.RemoveAt(0);
                    }
                    //need to calculate the metadata again.
                    CalculateDelta();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            try
            {
                if (ControlList.Count > MaximumNoOfControl)
                {
                    imgtoRemove = NoOfControlToBeRemoved;
                    for (int i = 0; i < imgtoRemove; i++)
                    {
                        texttoRemove = ControlList[0].CharIndex;
                        ControlList.RemoveAt(0);
                        this.Controls[0].Dispose();
                    }
                    this.Text = this.Text.Substring(texttoRemove);
                    foreach (MetaInfo oldone in ControlList)
                    {
                        oldone.CharIndex -= texttoRemove;
                    }
                    //need to calculate the metadata again.
                    CalculateDelta();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        private void RichTextBoxPrint_VScroll(object sender, EventArgs e)
        {
            Point pt = new Point();
            SendMessage(this.Handle, EM_GETSCROLLPOS, 0, ref pt);
 
            foreach (MetaInfo one in ControlList)
            {
                one.TheControl.Location = new Point(one.TheControl.Location.X, -pt.Y - one.DeltaY);
            }
        }
 
        private void RichTextBoxPrint_SizeChanged(object sender, EventArgs e)
        {
            CalculateDelta();
        }
 
        private void CalculateDelta()
        {
            foreach (MetaInfo one in ControlList)
            {
                one.TheControl.Location = this.GetPositionFromCharIndex(one.CharIndex);
                one.DeltaY = this.GetPositionFromCharIndex(0).Y - one.TheControl.Location.Y;
            }
        }
 
        private void RichTextBoxPrint_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }
 
 
 
 
 
 
        //Convert the unit used by the .NET framework (1/100 inch) 
        //and the unit used by Win32 API calls (twips 1/1440 inch)
        private const double anInch = 14.4;
 
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        private struct CHARRANGE
        {
            public int cpMin;         //First character of range (0 for start of doc)
            public int cpMax;           //Last character of range (-1 for end of doc)
        }
 
        [StructLayout(LayoutKind.Sequential)]
        private struct FORMATRANGE
        {
            public IntPtr hdc;             //Actual DC to draw on
            public IntPtr hdcTarget;       //Target DC for determining text formatting
            public RECT rc;                //Region of the DC to draw to (in twips)
            public RECT rcPage;            //Region of the whole DC (page size) (in twips)
            public CHARRANGE chrg;         //Range of text to draw (see earlier declaration)
        }
 
        private const int WM_USER = 0x0400;
        private const int EM_FORMATRANGE = WM_USER + 57;
 
        [DllImport("USER32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
 
        // Render the contents of the RichTextBox for printing
        //	Return the last character printed + 1 (printing start from this point for next page)
        public int Print(int charFrom, int charTo, PrintPageEventArgs e)
        {
            //Calculate the area to render and print
            RECT rectToPrint;
            rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
            rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
            rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
            rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
 
            //Calculate the size of the page
            RECT rectPage;
            rectPage.Top = (int)(e.PageBounds.Top * anInch);
            rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
            rectPage.Left = (int)(e.PageBounds.Left * anInch);
            rectPage.Right = (int)(e.PageBounds.Right * anInch);
 
            IntPtr hdc = e.Graphics.GetHdc();
 
            FORMATRANGE fmtRange;
            fmtRange.chrg.cpMax = charTo;				//Indicate character from to character to 
            fmtRange.chrg.cpMin = charFrom;
            fmtRange.hdc = hdc;                    //Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc;              //Point at printer hDC
            fmtRange.rc = rectToPrint;             //Indicate the area on page to print
            fmtRange.rcPage = rectPage;            //Indicate size of page
 
            IntPtr res = IntPtr.Zero;
 
            IntPtr wparam = IntPtr.Zero;
            wparam = new IntPtr(1);
 
            //Get the pointer to the FORMATRANGE structure in memory
            IntPtr lparam = IntPtr.Zero;
            lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            Marshal.StructureToPtr(fmtRange, lparam, false);
 
            //Send the rendered data for printing 
            res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
 
            //Free the block of memory allocated
            Marshal.FreeCoTaskMem(lparam);
 
            //Release the device context handle obtained by a previous call
            e.Graphics.ReleaseHdc(hdc);
 
            //Return last + 1 character printer
            return res.ToInt32();
        }
 
    }
class Utility
    {
 
        //Bonus. A simple check on the GIF files because some may contain "bad" data and crash the program.
        public static bool isImageCorrupted(Image img)
        {
            bool itis = false;
 
            try
            {
                if (!ImageAnimator.CanAnimate(img))
                {
                    return itis;
                }
                int frames = img.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time);
                if (frames <= 1)
                {
                    return itis;
                }
                byte[] times = img.GetPropertyItem(0x5100).Value;
                int frame = 0;
                for (; ; )
                {
                    int dur = BitConverter.ToInt32(times, 4 * frame);
                    if (++frame >= frames) break;
                    img.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Time, frame);
                }
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            return itis;
        }
    }
}


voila le code ça fonctionne à 100% tous les gif animés, prise en charge de l'impression ("imprimer ce que je voit a l'écran")

Benoit
0
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
6 sept. 2015 à 12:25
Bonjour !
tu as trouvé la solution bravo !
Tu as dit précédemment : "Je code en vb.net, framework 3.5"
Ne serait-ce pas du C# plutôt ?
Tu as posté en VB aussi !
0
benoit57320 Messages postés 16 Date d'inscription mardi 10 mars 2015 Statut Membre Dernière intervention 23 janvier 2016
6 sept. 2015 à 12:38
Bonjour
Je code bien en VB.net et pas en C# (sauf pour ce controller)
Je voulais transposer ce code en VB.net pour ça ne fonctionner pas pour une partie du code
 public RichTextBoxPrint()
: base()
{
this.VScroll += new EventHandler(RichTextBoxPrint_VScroll);
this.SizeChanged += new EventHandler(RichTextBoxPrint_SizeChanged);
this.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.RichTextBoxPrint_LinkClicked);
}


Donc pour résoudre ce problème j'ai codé la richtextbox en C# ce qui a permit de crée le controller sans erreurs.
Du coup je peux utiliser ce controller dans mon programme codé en vb.net
0
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169 > benoit57320 Messages postés 16 Date d'inscription mardi 10 mars 2015 Statut Membre Dernière intervention 23 janvier 2016
6 sept. 2015 à 12:59
ah ok !
Pas de souci alors !
Bonne prog et excuses-moi !
0
Rejoignez-nous