[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MFC: Two listboxes with one scrollbar

Posted on 2011-02-15
12
Medium Priority
?
845 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:tantormedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 1000 total points
ID: 34896946
You may try overriding the WindowProc for the ListCtrl and capture VSCROLL and post the message to the other list box.
LRESULT CMyListCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==WM_VSCROLL) {
::PostMessage(m_hwndList2, message, wParam, lParam);
}
return CListCtrl:::WindowProc(message, wParam, lParam);
}

Open in new window

0
 

Author Comment

by:tantormedia
ID: 34896999
Thank you for your answer. Could you please tell me how I can get m_hwndList2?
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34897080
You could create an object of your list control

Have a look at this:
http://www.functionx.com/visualc/controls/listbox.htm
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tantormedia
ID: 34897113
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...
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34897198
HWND m_hwndList2 = NULL;
m_hwndList2 = m_MyList2.m_hWnd;
0
 
LVL 35

Expert Comment

by:sarabande
ID: 34897259
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

0
 

Author Comment

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

Thanks.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 1000 total points
ID: 34897446
i made a mistake. the function is

BOOL CMyListBox::PreTranslateMessage(MSG* pMsg)

not WindowProc.

you also must find out whether the 'this' is m_listbox1 or m_listbox2 in order to post the message to the other control. also you need to add code to prevent from infinite messages for example by saving the last message.

BOOL CMyListBox::PreTranslateMessage(MSG* pMsg)
{
    static UINT lastListbox = 0;
    static MSG lastMsg = { 0 };

   
    if(pMsg->message==WM_VSCROLL) {
         CMyDialog* pDlg = (CMyDialog*)GetParent();
         if (pDlg->GetDlgItem(IDC_LIST1) == this)
         {
             if (memcmp(pMsg, &lastMsg) != 0)
                 pDlg->m_listbox2.PostMessage(pMsg->message, pMsg->wParam, pMsg->lParam);
          }
         else /* if (pDlg->GetDlgItem(IDC_LIST2) == this) */
         {
             if (memcmp(pMsg, &lastMsg) != 0)
                 pDlg->m_listbox1.PostMessage(pMsg->message, pMsg->wParam, pMsg->lParam);
          }
          memcpy(&lastMsg, pMsg, sizeof(MSG));
    }
    return FALSE;   // not handled cause the scrolling must be done by CListBox
}


Sara
0
 
LVL 35

Expert Comment

by:sarabande
ID: 34897537
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
0
 

Author Comment

by:tantormedia
ID: 34897642
Strange, but it doesn't work. WM_VSCROLL is not caught.

Sara, could you please answer also my question there:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_26820685.html#a34897334

Thanks.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 34898046
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
0
 

Author Closing Comment

by:tantormedia
ID: 34898233
Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question