GetScrollInfo sur VerticalScrollBar d'un RichTextBox

Résolu
NeuroCypher Messages postés 29 Date d'inscription vendredi 20 janvier 2006 Statut Membre Dernière intervention 24 octobre 2007 - 30 janv. 2007 à 15:30
NeuroCypher Messages postés 29 Date d'inscription vendredi 20 janvier 2006 Statut Membre Dernière intervention 24 octobre 2007 - 31 janv. 2007 à 12:08
Bonjour,

Je veux recuperer la position de la VerticalScrollBar d'un RichTextBox
lorsque cette meme scrollbar a fini de bouger...

J'ai donc commence par creer une classe ScrollingRichtTextBox qui herite de RichTextBox
qui contient un override de WndProc qui me permet de recevoir les events sur la scrollbar:
        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == WM_VSCROLL)
            {
                if (Scrolled != null)
                {                   
                    ScrollInfoStruct si = new ScrollInfoStruct();
                    si.fMask = SIF_ALL;
                    si.cbSize = (uint)Marshal.SizeOf(si);
                    GetScrollInfo(msg.HWnd, SB_VERT, ref si);
                    if (msg.WParam.ToInt32() == SB_ENDSCROLL)
                    {
                        ScrollEventArgs sargs = new ScrollEventArgs(
                            ScrollEventType.EndScroll, si.nPos);
                        Scrolled(this, sargs);
                    }
                }
            }
            base.WndProc(ref msg);
        }

Pour etre le plus clair et proche de la realite possible,
j'ai declare les macros en const dans la classe (cf: msdn, winuser.h, google....):
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;

        private const int SB_HORZ = 0;
        private const int SB_VERT = 1;
       
        private const int SB_LINELEFT = 0;
        private const int SB_LINERIGHT = 1;
        private const int SB_PAGELEFT = 2;
        private const int SB_PAGERIGHT = 3;
        private const int SB_THUMBPOSITION = 4;
        private const int SB_THUMBTRACK = 5;
        private const int SB_LEFT = 6;
        private const int SB_RIGHT = 7;
        private const int SB_ENDSCROLL = 8;

        private const int SIF_TRACKPOS = 0x10;
        private const int SIF_RANGE = 0x1;
        private const int SIF_POS = 0x4;
        private const int SIF_PAGE = 0x2;
        private const int SIF_ALL = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS;

J'ai aussi declare un event "Scrolled" :
        [Category("Action")]
        public event ScrollEventHandler Scrolled = null;

Cette event est alors manage dans mon designer de Form qui appele une methode de la FormClass, comme suit:
       this._viewBox.Scrolled += new System.Windows.Forms.ScrollEventHandler(this._viewBoxScrolled);

Et voici la methode:
        private void _viewBoxScrolled(object sender, System.Windows.Forms.ScrollEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("okk\n" +
                                                 e.OldValue.ToString() + "\n" +
                                                 e.Type.ToString() + "\n" +
                                                 e.ToString() + "\n" +
                                                 e.ScrollOrientation.ToString() + "\n"
                                                );
        }
<hr size="2" width="100%" />
Tout ce code "fonctionne"...
Seulement le probleme est le suivant:
La methode est bien appelee lorsqu'un ENDSCROLL sur la VerticalScrollBar est emis...
Mais en argument je recois les informations de la HorizontalScrollBar.
Pourtant dans le GetScrollInfo je precise bien en 2e argument un SB_VERT (0x1 = 1)
qui est la flag correspondant a la VerticalBar....

Cela fait un moment que je cherche...
J'ai essaye tout ce qui me passait par la tete je suis vraiment depite la.
J'ai peut-etre fait une erreur toute bete, en tout cas je ne la vois pas.

Je sollicite donc votre aide.

Merci par avance.

---
NeuroCypher
Etudiant en 3e annee d'un cursus de 5ans
Programmation et Reseaux {EPITECH}

2 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
31 janv. 2007 à 07:20
Salut, ça donne quoi avec cette classe..

public class MyRichTextBox : RichTextBox
{
    private enum SIF
    {
        RANGE           = 0x0001,
        PAGE            = 0x0002,
        POS             = 0x0004,
        DISABLENOSCROLL = 0x0008,
        TRACKPOS        = 0x0010,
        ALL             = RANGE | PAGE | POS | TRACKPOS
    }


    [ StructLayout( LayoutKind.Sequential ) ]
    private struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int  nMin;
        public int  nMax;
        public uint nPage;
        public int  nPos;
        public int  nTrackPos;
    }


    [ DllImport( "user32.dll", SetLastError = true ) ]
    private static extern bool GetScrollInfo
    (
        IntPtr hWnd,
        int fnBar,
        ref SCROLLINFO lpsi
    );


    private const int WM_VSCROLL = 0x0115;
    private const int SB_ENDSCROLL = 8;
    private const int SB_VERT = 1;


    public readonly uint SIZEOF_SCROLLINFO = ( uint )Marshal.SizeOf( typeof( SCROLLINFO ) );


    public event ScrollEventHandler VerticalScrollEnd = null;


    protected override void WndProc( ref Message m )
    {
        switch ( m.Msg )
        {
            case WM_VSCROLL :
                   
                    if ( ( int )m.WParam == SB_ENDSCROLL )
                    {
                        SCROLLINFO info = new SCROLLINFO( );
                        info.cbSize = SIZEOF_SCROLLINFO;
                        info.fMask = ( uint )SIF.POS; // ou ALL


                        if ( GetScrollInfo( this.Handle, SB_VERT, ref info ) )
                        {
                            ScrollEventArgs args = new ScrollEventArgs
                            (
                                ScrollEventType.EndScroll,
                                -1, // à implementer
                                info.nPos,
                                ScrollOrientation.VerticalScroll
                            );


                            OnVerticalScrollEnd( args );
                        }
                    }


                break; // WM_VSCROLL


            // ..


        }


        base.WndProc( ref m );
    }


    protected virtual void OnVerticalScrollEnd( ScrollEventArgs args )
    {
        if ( VerticalScrollEnd != null )
            VerticalScrollEnd( this, args );
    }
}
3
NeuroCypher Messages postés 29 Date d'inscription vendredi 20 janvier 2006 Statut Membre Dernière intervention 24 octobre 2007 1
31 janv. 2007 à 12:08
Bonjour Lutinore,

Deja merci de m'avoir repondu...
En lisant ton code je me suis rendu compte qu'en effet je n'avais pas regarder
les differents constructeurs de ScrollEventArgs, car j'etais focaliser sur le GetScrollInfo.

Mon erreur etait en fait toute bete...
J'affichais OldValue au lieu de NewValue et comme tu l'as precise je n'avais pas defini le OldValue...
Et comme je n'avais pas preciser le VerticalBar non plus l 'affichage de Horizontal me perturbait.

Merci beaucoup.

---
NeuroCypher
Etudiant en 3e annee d'un cursus de 5ans
Programmation et Reseaux {EPITECH}
0
Rejoignez-nous