Link to home
Create AccountLog in
Avatar of tantormedia
tantormediaFlag for United States of America

asked on

MFC: Two listboxes with one scrollbar

Dear experts,

Could you please tell me how I could make two listboxes use one common scrollbar to move content of both vertically for my MFC application?
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of tantormedia

ASKER

Thank you for your answer. Could you please tell me how I can get m_hwndList2?
You could create an object of your list control

Have a look at this:
http://www.functionx.com/visualc/controls/listbox.htm
This I understand. My question was about the window handle that is passed to PostMessage().
Also, my ListBox class should be generic, so I guess, I would have to pass the handle to the second Listbox to its constructor...
HWND m_hwndList2 = NULL;
m_hwndList2 = m_MyList2.m_hWnd;
tantormedia, deepuabrahamk speeks of a list control while you were talking of list boxes.

nevertheless, you can do the parallel scrolling with listboxes as well:

  - derive a own listbox class CMyListBox from CListBox
  - exchange CListBox in dialog header by CMyListBox
  - add a function CMyListBox::PreTranslateMessage  to CMyListBox like

BOOL CMyListBox::WindowProc(MSG* pMsg)
{
    if(pMsg->message==WM_VSCROLL) {
         CMyDialog* pDlg = (CMyDialog*)GetParent();

         pDlg->m_listbox2.PostMessage(pMsg->message, pMsg->wParam, pMsg->lParam);
    }
    return FALSE;   // not handled cause the scrolling must be done by CListBox
}

using a list control in report style would allow you to have two text columns (and line number column if you want). so you automatically have one scrollbar for both text files.

Sara

Thank you for your answers.
Sara, your code for WindowProc() is very similar to DeepuAbrahamK's. Is there any real difference?

Thanks.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
the PreTranslateMessage and WindowProc both will be called from message loop so that you can hook with them into message processing.

for your purpose the one is as good as the other. but you should use the CWnd member functions rather than pure WINAPI as deepuabrahamk posted. The CWnd is baseclass of CListBox and has the window handle as a member.

Sara
Strange, but it doesn't work. WM_VSCROLL is not caught.

Sara, could you please answer also my question there:
https://www.experts-exchange.com/questions/26820685/mfc-CDHtmlDialog-question.html?anchorAnswerId=34897334#a34897334

Thanks.
then try the WindowProc.

i mean to remember that PreTranslateMessage only was called for dialog windows.

unfortunately there are also two different techniques for scrollbar. one is the built-in scroll bar of a normal control window. another is a separate scroll bar window attached to a control. i mean listbox has the first and list control the second.

Sara
Thanks.