Click here to Skip to main content
Click here to Skip to main content
Go to top

Using the DeferWindowPos APIs

, 14 Dec 1999
Rate this:
Please Sign up or sign in to vote.
Presents a class to wrap the BeginDeferWindowPos/EndDeferWindowPos/DeferWindowPos APIs
  • Download source files - 1 Kb
  • <!-- Article Starts -->

    There's a set of little known APIs that allow you to move multiple windows in one fell swoop, which can reduce flicker in contrast to moving each window individually. Using the APIs is fairly straight forward, but like many such APIs they require you to use multiple steps. Forget a single step and things won't work as advertised.

    C++ programmers use a technique known as "resource allocation is acquisition" to ensure that any "finalization" steps are not forgotten. Basically, the constructor sets things up and the destructor insures the finalization occurs. The most widely known use of this technique is the std::auto_ptr class, which insures the pointer is deleted when the auto_ptr leaves scope.

    We can use this same technique to insure that the three steps required for DeferWindowPos are followed. We'll create a class to wrap DeferWindowPos. The constructor for the class will call BeginDeferWindowPos and the destructor will call EndDeferWindowPos. Member functions will call DeferWindowPos using the stored HDWP handle required by these APIs. Calls to these methods will delay the window positioning until the destructor is called. So, to move two windows at once is as easy as the following:

    {
    	CDeferPos dp(2);	// We'll move 2 windows
    	dp.MoveWindow(hWnd1, 0, 0, 10, 20, TRUE);
    	dp.MoveWindow(hWnd2, 10, 0, 10, 20, TRUE);
    }	// When dp goes out of scope both windows are moved at once
    

    Note that the constructor takes a parameter indicating how many windows we think we'll move. This is just a suggestion that will help Windows to optimize the memory allocation. If you don't know how many windows you'll move you can either guess or leave it to the default of 1. Windows will increase the memory size as needed when you move more windows than initially allocated.

    For the complete code listing for CDeferPos check out the download.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    William E. Kempf
    Web Developer
    United States United States
    Windows developer with 10+ years experience working in the banking industry.

    Comments and Discussions

     
    GeneralA simple query PinmemberHakunaMatada24-Jun-07 23:05 
    Does DeferWindowPos work only on child windows or do they work on popups too?
     
    ---
    Beer | [beer] Hakuna-Matata Beer | [beer]
    It means no worries for the rest of your days...
    It's our problem free, Philosophy

    Jig | [Dance]
     
    "I think my response was 'What idiot dreamed this up?'" -- Mary Ann Davidson, Oracle's chief security officer, in typical blunt manner, remembering her reaction to the company's scheme to brand its databases as "unbreakable."

    GeneralThanks a lot PinmemberKwint14-Dec-06 23:10 
    GeneralHelp! PinmemberGermán.30-Nov-04 13:57 
    GeneralThanks.. PinsussAnonymous19-Sep-02 21:35 
    GeneralNice! PinmemberWarren Marshall7-Dec-01 10:03 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140921.1 | Last Updated 15 Dec 1999
    Article Copyright 1999 by William E. Kempf
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid