Solved

changing styles of dialog controls

Posted on 1998-09-21
1
320 Views
Last Modified: 2013-11-19
I want to change the style of a dialog control on
run-time,e.g. I want to switch the style of a
list box from single column to multicolumn by
button-click. I'm using old-fashionend Visual c++ 1.5 for
win 3.11. Is there any MFC-function doing this job? I could
not find any:(
0
Comment
Question by:elazabi
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1322370
1. In general to change style - you must use SDK style functions (these mix into your MFC program okay)

LONG lStyle = ::GetWindowLong( ::GetDlgItem( m_hWnd, ID_MYCONTROL ), GWL_STYLE ) ;

lStyle &= ~WSBORDER ; // remove WS_BORDER style
lStyle &= ~SS_LEFT ; // remove SS_LEFT say from a static
lStyle |= SS_RIGHT ; // add SS_RIGHT say to a static

::SetWindowLong( ::GetDlgItem( m_hWnd, ID_MYCONTROL ), GWL_STYLE ), lStyle ) ;

You will find this works reasonably well under Win32.  The results are however unpredictable under Win16 on many controls (basically try it and see what happens).  Unfortunately this is a Windows API limitation (MFC builds on top of this), so there is no way to fix this.  However there are ways around...

2. If you want to just enable/disable, show/hide a control you can do this without changing the style.  This works under Win16 or Win32

GetDlgItem( ID_MYCONTROL )->EnableWindow( FALSE ) ; // disable a control
GetDlgItem( ID_MYCONTROL )->EnableWindow( TRUE ) ; // enable a control
GetDlgItem( ID_MYCONTROL )->ShowWindow( SW_SHOW ) ; // show a control
GetDlgItem( ID_MYCONTROL )->ShowWindow( SW_HIDE ) ; // hide a control

In general you will want to only hide controls if they are disabled otherwise the user will get odd results (they can tab into an invisible control for example)

3. Now looking at your specific problem (switching a list box from multi-line to single line selection on the fly)

You will find changing a single list box between the 2 styles does not really work under Win16 (I don't think it really works under Win32).  However you can produce the exact same appearance to the user quite easily :-

i. In the dialog resource add _TWO_ list boxes at identical positions, set one to be multi-selection, set one to be single selection

ii. Add a member function to flip to multi-line selection mode.  This would hide the single selection list and disable it, and then show the multi selection list and enable that.

iii. Add a member function to flip to single selection mode.  This would hide the multi selection list and disable it, and then show the single selection list and enable that.

iv. Add a member function(s) to test which mode you are in, e.g.

BOOL CMyDialog::IsMultiSelectionMode()
{
   return GetDlgItem( ID_LIST_MULTI )->IsWindowEnabled() ;
}

BOOL CMyDialog::IsSingleSelectionMode()
{
   return GetDlgItem( ID_LIST_SINGLE )->IsWindowEnabled() ;
}


v. When you populate the list box initially (or update its contents later), make sure you make the same changes into both list boxes (alternatively you ould update only the current list box, and then copy items over to the other one only when you switch mode).

vi. In WM_INITDIALOG (OnInitDialog) set the initial state (single or multi selection) of the list box by calling ii. or iii.

vii. When you want to flip mode do the same - call ii or iii.

viii. If you want to make the whole thing a bit cleaner (e.g. if you're doing lots of updating on the contents of the lists) you can write your own AddString, etc. function which calls the AddString for both list boxes (rather than you directly calling it yourself)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 56
Expand to include initial dialog with two choices. 9 67
maven archtype selection in eclipse 1 53
mapAB Challlenge 35 126
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now