/************************************************************************* * File : aga10win.cpp * Description : Simple Win32 program demonstrating use of aga10.dll * Supports Windows dialog box and file operations * Version : 1.7 2002.11.17 * Author : W.B. Peterson * Revisions : * Copyright (c) 2002 American Gas Association **************************************************************************/ #include "aga10win.h" #include "aga10.h" /* create pointer to a data structure for exchanging data with aga10.dll */ static AGA10STRUCT *A10 ; /* global variables for strings, filenames, etc */ static char szAppName[] = "aga10win" ; static char szBuffer[FIELD40] ; char szFileName[_MAX_PATH] ; char szTitleName[_MAX_FNAME + _MAX_EXT] ; /* declare one application instance */ HINSTANCE hInst ; /* global variables for units of measure and critical flow coefficient C* */ double total = 0.0 ; long int lPb_unit ; /* unit of measure for base pressure */ long int lPf_unit ; /* unit of measure for flowing pressure */ long int lTb_unit ; /* unit of measure for base temperature */ long int lTf_unit ; /* unit of measure for flowing temperature */ long int lRhob_unit ; /* unit of measure for density at base conditions */ long int lRhof_unit ; /* unit of measure for density at flowing conditions */ long int lSOS_unit ; /* unit of measure for speed of sound */ long int lEnthalpy_unit ; /* unit of measure for specific enthalpy */ long int lEntropy_unit ; /* unit of measure for specific entropy */ // prototypes for support functions not prototyped in aga10win.h bool FileRead(HWND, PSTR, AGA10STRUCT *); bool FileWrite(HWND, PSTR, AGA10STRUCT *) ; void ReadInputs(HWND, AGA10STRUCT *) ; void WriteInputs(HWND, AGA10STRUCT *) ; void WriteOutputs(HWND, AGA10STRUCT *) ; void SetDefaults(AGA10STRUCT *) ; /************************************************************************** * Function : WinMain() * Arguments : HINSTANCE, HINSTANCE, PSTR, int * Returns : int * Purpose : Every Windows application has a WinMain() * Revisions : **************************************************************************/ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hWnd ; MSG msg ; WNDCLASSEX wndclass ; /* set window class properties */ wndclass.cbSize = sizeof (wndclass) ; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = DLGWINDOWEXTRA ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (hInstance, szAppName) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1) ; wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1) ; wndclass.lpszClassName = szAppName ; wndclass.hIconSm = LoadIcon (hInstance, szAppName) ; /* register the class */ RegisterClassEx (&wndclass) ; /* create a dialog box */ hWnd = CreateDialog (hInstance, "aga10win", 0, NULL) ; /* start the application's message pump */ while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage(&msg) ; DispatchMessage(&msg) ; } return msg.wParam ; } /************************************************************************** * Function : WndProc() * Arguments : HWND, UINT, WPARAM, LPARAM * Returns : LRESULT * Purpose : One and only window process for this app * Revisions : **************************************************************************/ LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { int i = 0 ; double temp ; switch (iMsg) { case WM_CREATE : /* get application instance */ hInst = ((LPCREATESTRUCT) lParam)->hInstance ; /* initialize file data */ FileInitialize (hwnd) ; /* initialize calculation library */ AGA10_Init() ; /* create an object at the pointer we have already defined */ if (NULL == (A10 = new AGA10STRUCT)) return TRUE ; /* set the defaults for this application */ SetDefaults(A10) ; return FALSE ; case WM_COMMAND : /* refresh the input data, triggered by focus change */ if (lParam && HIWORD (wParam) == EN_KILLFOCUS) { ReadInputs(hwnd, A10) ; WriteInputs(hwnd, A10) ; lstrcpy(szBuffer, "Press Calculate") ; SetDlgItemText (hwnd, IDC_LSTATUS, szBuffer) ; return FALSE ; } /* decode WM_COMMAND messages */ switch (LOWORD (wParam)) { case IDOK : /* refresh input fields */ ReadInputs(hwnd, A10) ; WriteInputs(hwnd, A10) ; //ensure the compositions adds up before proceeding //find the current sum of fractions temp = 0.0 ; for (i = 0 ; i < NUMBEROFCOMPONENTS ; i++) temp += A10->adMixture[i] ; if (temp < 0.9999 || temp > 1.0001) { MessageBox (hwnd,"Error. Composition must total 100%, +/- 0.01%", szAppName, MB_OK | MB_ICONERROR) ; lstrcpy(szBuffer, "Error. Composition <> 100%.") ; SetDlgItemText (hwnd, IDC_LSTATUS, szBuffer) ; return FALSE ; } // ensure the pressure is acceptable before proceeding if (A10->dPf < P_MIN || A10->dPf > P_MAX) { MessageBox (hwnd,"Error. Pf out of range.", szAppName, MB_OK | MB_ICONERROR) ; lstrcpy(szBuffer, "Error. Pf out of range.") ; SetDlgItemText (hwnd, IDC_LSTATUS, szBuffer) ; return FALSE ; } // ensure the temperature is acceptable before proceeding if (A10->dTf < T_MIN || A10->dTf > T_MAX) { MessageBox (hwnd,"Error. Tf out of range.", szAppName, MB_OK | MB_ICONERROR) ; lstrcpy(szBuffer, "Error. Tf out of range.") ; SetDlgItemText (hwnd, IDC_LSTATUS, szBuffer) ; return FALSE ; } /* indicate that a calculation has begun */ lstrcpy(szBuffer, "Calculation In Progress") ; SetDlgItemText (hwnd, IDC_LSTATUS, szBuffer) ; /* run the sound speed AND C* calculation */ Crit(A10, 0.0) ; /* write the outputs to the dialog box */ WriteOutputs(hwnd, A10) ; return FALSE ; case IDC_CLEAR : /* zero out the composition and then display it */ for (i = 0 ; i < NUMBEROFCOMPONENTS ; i++) A10->adMixture[i] = 0.0 ; WriteInputs(hwnd, A10) ; return FALSE ; case IDC_NORMALIZE : //normalize the composition to equal 1.0000 ReadInputs(hwnd, A10) ; temp = 0.0 ; //find the current sum of fractions for (i = 0 ; i < NUMBEROFCOMPONENTS ; i++) temp += A10->adMixture[i] ; // adjust each non-zero entry by the required proportion for (i = 0 ; i < NUMBEROFCOMPONENTS ; i++) if (A10->adMixture[i] > 0.0) A10->adMixture[i] /= temp ; // write the adjusted values to the screen WriteInputs(hwnd, A10) ; return FALSE ; case IDCANCEL : /* start tear-down process */ SendMessage(hwnd, WM_CLOSE, 0,0L) ; return FALSE ; case IDRETRY : //reset the defaults SetDefaults(A10) ; //display the input data to the screen WriteInputs(hwnd, A10) ; //send a message back to this proc, requesting a calculation SendMessage(hwnd, WM_COMMAND, IDOK,0L) ; return FALSE ; case CM_FILEOPEN : // standard Windows file operations GetFileTitle (szFileName, szTitleName, sizeof (szTitleName)) ; if (FileOpenDlg (hwnd, szFileName, szTitleName)) { if (!FileRead (hwnd, szFileName, A10)) { MessageBox(hwnd,"Could not read file.", szTitleName, MB_OK | MB_ICONSTOP) ; } } else return FALSE ; //Write the new values to the window WriteInputs(hwnd, A10) ; //send a message back to this proc, requesting a calculation SendMessage(hwnd, WM_COMMAND, IDOK,0L) ; return FALSE ; case CM_FILESAVE : // standard Windows file operations GetFileTitle (szFileName, szTitleName, sizeof (szTitleName)) ; if (szFileName[0]) { if (FileWrite(hwnd, szFileName, A10)) { return TRUE ; } else { MessageBox(hwnd,"Could not write file.", szTitleName, MB_OK | MB_ICONSTOP) ; } return FALSE ; } // fall through case CM_FILESAVEAS : // standard Windows file operations GetFileTitle (szFileName, szTitleName, sizeof (szTitleName)) ; if (FileSaveDlg (hwnd, szFileName, szTitleName)) { if (FileWrite (hwnd, szFileName, A10)) { return 1 ; } else { MessageBox(hwnd,"Could not write file.", szTitleName, MB_OK | MB_ICONSTOP) ; } } return FALSE ; case CM_HELPABOUT : MessageBox (hwnd, "AGA10win.exe and AGA10.dll (c) American Gas Association, 2002", szAppName, MB_OK | MB_ICONINFORMATION) ; return FALSE ; } return FALSE ; case WM_CLOSE : /* un-initialize the calculation library */ AGA10_UnInit() ; // remove the AGA10STRUCT object from memory delete A10 ; /* request Windows to terminate the app */ DestroyWindow (hwnd) ; return FALSE ; case WM_DESTROY : /* final message exhange with Windows during shut-down */ PostQuitMessage (0) ; return FALSE ; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }