Listing 3: demo.c
Main program and dialog functions
#include <windows.h> #include <windowsx.h> #include <tchar.h> #include "resource.h" #include "wnd_snap.h" #pragma warning( disable: 4100 ) // unref'd formal parameter BOOL CALLBACK boxDlgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { if ( WM_COMMAND == msg ) { const int id = GET_WM_COMMAND_ID ( wParam, lParam ); const int code = GET_WM_COMMAND_CMD( wParam, lParam ); if ( IDCANCEL == id && BN_CLICKED == code ) { EndDialog( hwnd, id ); } return TRUE; } if ( WM_MOVING == msg && IsDlgButtonChecked( hwnd, IDC_LIMIT ) ) { LPRECT prc = (LPRECT) lParam; RECT wa = { 0 }; SystemParametersInfo( SPI_GETWORKAREA, 0, &wa, 0 ); if ( prc->left < wa.left ) { OffsetRect( prc, wa.left - prc->left, 0 ); } else if ( wa.right < prc->right ) { OffsetRect( prc, wa.right - prc->right, 0 ); } if ( prc->top < wa.top ) { OffsetRect( prc, 0, wa.top - prc->top ); } else if ( wa.bottom < prc->bottom ) { OffsetRect( prc, 0, wa.bottom - prc->bottom ); } return SetDlgMsgResult( hwnd, WM_MOVING, TRUE ); } return FALSE; } BOOL CALLBACK snapDlgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { if ( WM_COMMAND == msg ) { const int id = GET_WM_COMMAND_ID ( wParam, lParam ); const int code = GET_WM_COMMAND_CMD( wParam, lParam ); if ( IDC_SNAP == id && BN_CLICKED == code ) { setSnap( hwnd, IsDlgButtonChecked( hwnd, IDC_SNAP ) ); } else if ( IDCANCEL == id && BN_CLICKED == code ) { EndDialog( hwnd, id ); } return TRUE; } return FALSE; } int WINAPI _tWinMain( HINSTANCE hinst, HINSTANCE h2, LPSTR p, int n ) { DialogBox( hinst, MAKEINTRESOURCE( IDD_BOX ), HWND_DESKTOP, boxDlgProc ); DialogBox( hinst, MAKEINTRESOURCE( IDD_SNAP ), HWND_DESKTOP, snapDlgProc ); return 0; }