RichTextBox handling with custom ScrollBar in C# -
recently i'm on form project custom gui. (metroframework) has own textbox component, prefer richtextbox. problem uses windows legacy scrollbar if set visible. don't want use this. luckily framework has own scrollbar component skin want. can set richtextbox no scrollbar (scrollbars property set none) , set independent custom scrollbar docked right richtextbox. didn't find solution how derive or attach custom scrollbar control legacy richtextbox if behaviors rtb's built in scrollbar. tried manually. when populate rtb, can calculate custom scrollbar minimum, maximum, smallchange, largechange properties can set scrollbar thumb size. achieved emulate scrolling method cathich custom scrollbar's scroll event scroll rtb's text content vertically (only need vertical scroll) interop services (user32.dll):
private void metroscrollbarrichtextboxmain_scroll(object sender, scrolleventargs e) { uint npos = (uint)metroscrollbarrichtextboxmain.value * (uint)math.round((decimal)richtextboxmain.font.getheight(), 1, midpointrounding.awayfromzero); uint wparam = (uint)windowmessageconstants.scrollbarcommands.sb_thumbposition | npos << 16; sendmessage(richtextboxmain.handle, windowmessageconstants.wm_vscroll, (intptr)wparam, intptr.zero); }
so scrolling mouse while hold scrollbar's thumb ok. here catch, wanna use richtextbox other abilites such scroll text content arrow keys , mousewheel. mousewheel ok, implemented easily. can't find solution catch event if user scroll content keyboard arrows. needed because have sync position custom scrollbar. didn't find solution what's happening when rtb move text content 1 line down or when use keyboard arrows. nor when make selection mouse , slide towards upper or lower edge of rtb (it scroll text too) event fires in case? scrolltocaret function, doesn't fire event. other approach if set vertical scrollbar visible, , "hide" behind custom scrollbar. in case scroll position sync scrollbar pos. ugly, there must better way. tried capture if scroll effect happening via wndproc:
protected override void wndproc(ref message m) { base.wndproc(ref m); if (m.msg == windowmessageconstants.wm_vscroll || m.msg == windowmessageconstants.wm_hscroll || m.msg == windowmessageconstants.wm_mousewheel) { ...do here } }
but when scroll or mousewheel scroll happening. not when scroll keyboard arrows.
can guys show me way how achieve this? or maybe approach wrong, maybe there way, derive/inherit legacy rtb controls to/from custom scrollbar's controls can attach custom scrollbar richtextbox while rtb's scrollbars property set none. (so built in scrollbars not visible)
sorry long explanation, searched solution everywhere no success days...
Comments
Post a Comment