#include "StdSDK.h" #define MAX_BUFFER_STRING 80 L_INT GetItemIndex ( L_INT* pnTable, L_INT nItem ) { L_INT nIndex = 0 ; while ( pnTable [ nIndex++ ] != nItem ) ; return nIndex - 1 ; } L_BOOL UpdataIfValid ( HWND hdlg, L_INT nItemID, L_INT nMin, L_INT nMax, L_INT* nDestVar, L_BOOL bSigned ) { L_INT nValue ; L_BOOL bTranslated ; static L_TCHAR message [MAX_BUFFER_STRING] ; HWND hItem = GetDlgItem ( hdlg, nItemID ) ; nValue = GetDlgItemInt ( hdlg, nItemID, ( L_INT*) &bTranslated, bSigned ) ; if ( ! bTranslated ) { if ( IsWindowEnabled ( hItem ) ) { wsprintf ( message, TEXT("Please enter a value between %d and %d"), nMin, nMax ) ; MessageBox ( NULL, message, TEXT("Error: Out of range"), MB_OK | MB_ICONEXCLAMATION ) ; SetFocus ( hItem ) ; Edit_SetSel ( hItem, 0, Edit_GetTextLength ( hItem ) ) ; return FALSE ; } return TRUE ; } if ( nValue < nMin || nValue > nMax ) { if ( IsWindowEnabled ( hItem ) ) { wsprintf ( message, TEXT("Please enter a value between %d and %d"), nMin, nMax ) ; MessageBox ( NULL, message, TEXT("Error: Out of range"), MB_OK | MB_ICONEXCLAMATION ) ; SetFocus ( hItem ) ; Edit_SetSel ( hItem, 0, Edit_GetTextLength ( hItem ) ) ; return FALSE ; } return TRUE ; } *nDestVar = nValue ; return TRUE ; } L_UINT CALLBACK CFHookProc ( HWND hdlg, L_UINT uiMsg, WPARAM wParam, LPARAM lParam ) { UNREFERENCED_PARAMETER(lParam) ; UNREFERENCED_PARAMETER(wParam) ; switch ( uiMsg ) { case WM_INITDIALOG: { CenterWindow ( hdlg, NULL ) ; } return TRUE; } return FALSE ; } L_BOOL DoChooseFont ( HWND hdlg, LPLOGFONT plogFont ) { CHOOSEFONT chf ; chf.lStructSize = sizeof(CHOOSEFONT) ; chf.hwndOwner = NULL ; chf.hDC = NULL ; chf.lpLogFont = plogFont ; chf.iPointSize = 0 ; chf.Flags = CF_TTONLY | CF_SCREENFONTS | CF_EFFECTS | CF_ENABLEHOOK ; chf.rgbColors = RGB ( 0, 0, 0 ) ; chf.lCustData = 0 ; chf.lpfnHook = CFHookProc ; chf.lpTemplateName = NULL ; chf.hInstance = GetWindowInstance ( hdlg ) ; chf.lpszStyle = NULL ; chf.nFontType = SIMULATED_FONTTYPE ; chf.nSizeMin = 0 ; chf.nSizeMax = 0 ; return ChooseFont ( &chf ) ; } L_UINT CALLBACK CCHookProc(HWND hdlg, L_UINT uiMsg, WPARAM wParam, LPARAM lParam ) { UNREFERENCED_PARAMETER(lParam) ; UNREFERENCED_PARAMETER(wParam) ; switch ( uiMsg ) { case WM_INITDIALOG: { CenterWindow ( hdlg, NULL ) ; } return TRUE; } return FALSE ; } COLORREF DoChooseColor ( COLORREF crCurrentColor ) { CHOOSECOLOR cc ; static COLORREF crCustColors[16] ; cc.lStructSize = sizeof (CHOOSECOLOR) ; cc.hwndOwner = NULL ; cc.hInstance = NULL ; cc.rgbResult = crCurrentColor ; cc.lpCustColors = crCustColors ; cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK ; cc.lCustData = 0L ; cc.lpfnHook = CCHookProc ; cc.lpTemplateName = NULL ; ChooseColor( &cc ) ; return cc.rgbResult ; } LPCTSTR GetAppName ( ) { static TCHAR AppName [_MAX_FNAME + 1] = TEXT("") ; DWORD Length ; HMODULE hmod ; TCHAR AppFileSpec [_MAX_PATH + 1] ; // Get the module handle of this application hmod = GetModuleHandle (NULL) ; ASSERT (NULL != hmod) ; // Get the full path and file name for this application Length = GetModuleFileName (hmod, AppFileSpec, DIM (AppFileSpec)) ; ASSERT (0 != Length) ; // Convert to the displayable filename // Don't cache the display name in case user changes preferences VERIFY(GetFileTitle (AppFileSpec, AppName, DIM (AppName))) ; // Return the file name component return AppName ; } LPCTSTR GetHelpFileName ( ) { static TCHAR HelpFile [_MAX_PATH] = TEXT("") ; DWORD Length ; HMODULE hmod ; TCHAR AppFileSpec [_MAX_PATH] ; TCHAR AppDrive [_MAX_DRIVE] ; TCHAR AppPath [_MAX_DIR] ; TCHAR AppFileName [_MAX_FNAME] ; // Return help file name if already known if (TEXT('\0') != HelpFile [0]) return HelpFile ; // Get the module handle of this application hmod = GetModuleHandle (NULL) ; ASSERT (NULL != hmod) ; // Get the full path and file name for this application Length = GetModuleFileName (hmod, AppFileSpec, DIM (AppFileSpec)) ; ASSERT (0 != Length) ; // Extract the drive, path and file name components ignoring extension _wsplitpath ( ( const wchar_t * ) AppFileSpec, ( wchar_t * ) AppDrive, ( wchar_t * ) AppPath, ( wchar_t * ) AppFileName, NULL ) ; // Combine the drive, path and file name components with a .hlp extension _wmakepath ( ( wchar_t * ) HelpFile, ( const wchar_t * ) AppDrive, ( const wchar_t * ) AppPath, ( const wchar_t * ) AppFileName, ( const wchar_t * ) TEXT(".hlp") ) ; return HelpFile ; } L_BOOL CenterWindow (HWND hwndCentered, HWND hwndPreferredOwner) { L_BOOL Result ; POINT OwnerCenter , CenteredUL ; RECT WindowRect, OwnerRect, WorkArea ; SIZE CenteredWindow ; ASSERT (NULL != hwndCentered) ; ASSERT (IsWindow (hwndCentered)) ; // If a preferred owner isn't specified... if (NULL == hwndPreferredOwner) { // Use the owner of the window to be centered hwndPreferredOwner = GetWindowOwner (hwndCentered) ; } // Get the rectangle for the workarea Result = SystemParametersInfo (SPI_GETWORKAREA, sizeof (RECT), &WorkArea, 0 ) ; // If the above call failed, the new shell probably isn't running // therefore there is no tray and no workarea. // Use the screen size as the workarea size. if (!Result) { SetRect (&WorkArea, 0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN)) ; } // Center around the owner window, if one, // otherwise center in the work area if (NULL != hwndPreferredOwner) { ASSERT (IsWindow (hwndPreferredOwner)) ; GetWindowRect (hwndPreferredOwner, &OwnerRect) ; } else { OwnerRect = WorkArea ; } // Preferred center point OwnerCenter.x = (OwnerRect.left + OwnerRect.right) / 2 ; OwnerCenter.y = (OwnerRect.top + OwnerRect.bottom) / 2 ; // Get the centered window's rectangle and compute height/width GetWindowRect (hwndCentered, &WindowRect) ; CenteredWindow.cx = WindowRect.right - WindowRect.left ; CenteredWindow.cy = WindowRect.bottom - WindowRect.top ; // Center window in owner horizontally // Calculates the left side coordinate CenteredUL.x = OwnerCenter.x - CenteredWindow.cx / 2 ; // Center window in owner vertically // Calculates the top side coordinate CenteredUL.y = OwnerCenter.y - CenteredWindow.cy / 2 ; // If the left edge of the centered window is clipped by the workarea // move the window horizontally to the right until left edge is exposed. if (CenteredUL.x < WorkArea.left) { CenteredUL.x = WorkArea.left ; } // If the right edge of the centered window is clipped by the workarea // move the window horizontally to the left until right edge is exposed. else { if (CenteredUL.x + CenteredWindow.cx > WorkArea.right) { CenteredUL.x = WorkArea.right - CenteredWindow.cx ; } } // If the top edge of the centered window is clipped by the workarea // move the window vertically down until top edge is exposed. if (CenteredUL.y < WorkArea.top) { CenteredUL.y = WorkArea.top ; } // If the bottom edge of the centered window is clipped by the workarea // move the window vertically up until bottom edge is exposed. else { if (CenteredUL.y + CenteredWindow.cy > WorkArea.bottom) { CenteredUL.y = WorkArea.bottom - CenteredWindow.cy ; } } // Reposition the window centered (within visibility constraints) return SetWindowPos (hwndCentered, NULL, CenteredUL.x, CenteredUL.y, 0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER) ; } #ifdef _DEBUG L_BOOL assertFailedOnLine ( LPCSTR FileName, L_INT Line ) { HWND hWndParent ; L_INT Code ; char Message [_MAX_PATH*2] ; // Format the output string sprintf (Message, "File %hs, Line %d", FileName, Line) ; // Get the last active popup window for the current thread hWndParent = GetActiveWindow ( ) ; if ( hWndParent != NULL ) { hWndParent = GetLastActivePopup ( hWndParent ) ; } // display the assert Code = MessageBoxA ( hWndParent, Message, "Assertion Failed!", MB_ABORTRETRYIGNORE | MB_ICONERROR | MB_SETFOREGROUND | MB_TASKMODAL ) ; switch ( Code ) { case IDIGNORE: // Ignore { return FALSE ; } case IDRETRY: // Retry { DebugBreak ( ) ; return TRUE ; } default: // Abort { return TRUE ; } } } #endif // _DEBUG