Click here to Skip to main content
Click here to Skip to main content

A snap to screen border dialog class

, 8 Apr 2000
Rate this:
Please Sign up or sign in to vote.
Dialog class that implement a snap-to-screen-border feature like Winamp
  • Download demo project - 14 Kb
  • Download source files - 2 Kb
  • Introduction

    This class is used to implement the same feature seen in the popular Winamp player. With this class you are able to snap the dialog to the screen border. If you drag the dialog near the screen border, the dialog will automatically stick on the screen border (like a magnet).

    How to use it

    1. Add SnapDialog.h and SnapDialog.cpp to your project.
    2. Add #include "SnapDialog.h" in your dialog class.
    3. Replace all occurrence of CDialog in your dialog class by CSnapDialog.
    4. Enjoy!

    To change the limit after which the dialog is automatically stickled to the screen border, set these two variables. Default values are 15 pixels.

    m_nXOffset = 15;
    m_nYOffset = 15;
    

    That's all!

    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

    François Gagné

    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralVisual Studio Magazine... PinmvpDave Kreskowiak11-Dec-08 9:31 
    Generalapp that makes all your windows snap PinmemberIvan Heckman24-Apr-02 8:08 
    GeneralMFC and Winamp PinmemberBob H2-Mar-01 11:26 
    GeneralThere are better ways.... PinsussEric Tetz18-Apr-00 9:53 
    The meat of this program, OnWindowPosChanging(), is over-complicated and results in quirky behavior for the window. For example, it does not deal with the case where the task bar is hidden - it insist on snapping the window to the screen position where the task bar WOULD be, if it were visible.
     
    You can fix these problems and use much less code doing it by replacing the code in OnWindowPosChanging (which is over 50 lines in the example!) with a call to this function:
     
    void HandleSnapToDesktopEdgeMsg (WINDOWPOS* wndPos)
    {
    RECT rcScrn;
    SystemParametersInfo (SPI_GETWORKAREA, 0, &rcScrn, 0);
     
    // Snap X axis
    if (abs(wndPos->x - rcScrn.left) <= m_EdgeSnapGap)
    {
    wndPos->x = rcScrn.left;
    }
    else if (abs(wndPos->x + wndPos->cx - rcScrn.right) <= m_EdgeSnapGap)
    {
    wndPos->x = rcScrn.right - wndPos->cx;
    }

    // Snap Y axis
    if (abs(wndPos->y - rcScrn.top) <= m_EdgeSnapGap)
    {
    wndPos->y = rcScrn.top;
    }
    else if (abs(wndPos->y + wndPos->cy - rcScrn.bottom) <= m_EdgeSnapGap)
    {
    wndPos->y = rcScrn.bottom - wndPos->cy;
    }
    }
     
    Cheers,
    Eric
    GeneralRe: There are better ways.... PinmemberAnonymous30-Oct-01 5:14 
    GeneralRe: There are better ways.... PinmemberTodd Smith30-Oct-01 6:59 
    GeneralRe: There are better ways.... PinsussJean-Claude25-Nov-02 1:47 
    QuestionWhat about SDI or MDI window? PinsussDmitry Sokolov10-Apr-00 17:43 
    AnswerRe: What about SDI or MDI window? PinsussDmitry Sokolov10-Apr-00 17:46 
    GeneralMy pet peeve... multimon support! PinsussTom Spilman9-Apr-00 21:08 

    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 | Terms of Use | Mobile
    Web03 | 2.8.141223.1 | Last Updated 9 Apr 2000
    Article Copyright 2000 by François Gagné
    Everything else Copyright © CodeProject, 1999-2014
    Layout: fixed | fluid