Jump to content
  • Advertisement
Sign in to follow this  
GetWindowRect

Window Style changing Question with SetWindowLong

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to change a listbox's sorted style to either be there or not. I use SetWindowLong and have tried SetWindowLongPtr but neither seem to change the style. I was wondering if I am doing something wrong or if this particular style cannot be changed. Here is the code I have used so far:
m_Sorted = sort;

long style = GetWindowLong(m_hWnd, GWL_STYLE);
if (m_Sorted) 
	style |= LBS_SORT;
else 
	style &= ~(LBS_SORT);


SetWindowLong(m_hWnd, GWL_STYLE, style);
	
InvalidateRect(m_hWnd, NULL, false);
UpdateWindow(m_hWnd);


Share this post


Link to post
Share on other sites
Advertisement
I'm not sure exactly how LBS_SORT works, but it may be that it sorts on insertion, rather than at draw-time.

Try clearing the listbox or combobox (or whatever it is) with LB_/CB_RESETCONTENT and then re-inserting the elements after the style change.

Share this post


Link to post
Share on other sites
I just tried before adding anything to the listbox to change the style to include LBS_SORT, and when the items loaded, it was still unsorted. Thank you for the idea though, that was something I haven't thought to try

Share this post


Link to post
Share on other sites
SetWindowLong Function

Quote:

Remarks

Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.
...


Here's a VB example of modifying a window's style.
Here's a C# example of modifying a window's style.

I haven't been able to find a C++ version. The C# version shouldn't be too difficult to adapt.

Share this post


Link to post
Share on other sites
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kippesoep
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.


Probably a stupid question but thought I would double check... So this means I cannot (without deleting the listbox and recreating it) change the sort style after it has been created?

Share this post


Link to post
Share on other sites
Quote:
Original post by GetWindowRect
Quote:
Original post by Kippesoep
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.


Probably a stupid question but thought I would double check... So this means I cannot (without deleting the listbox and recreating it) change the sort style after it has been created?


It looks like it that is the case.

You could try using the slightly more complex but powerful ListView control, and use a hidden 'dummy' column to record the original input order. Then, use the ListView_SortItems() function with an appropriate comparison function to either sort by original input order ('unsorted') or by value ('sorted').

Another would be to clear the listbox, and either reinsert the items in whatever order they come in, or sort them yourself (using an stl container and std::sort would save you having to write your own sort function).

Share this post


Link to post
Share on other sites
Thank you a bunch for all your help, I am still new to c++, only been playing around with it for a couple weeks really (window programming wise), but I will look into the ListView control. I use VB mainly but would like to get into c++, it is a lot more fun, and I can learn how things actually work.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!