void HandleMenu(HWND hWnd, DWORD opt) { switch(opt) { case ID_SCALE_CONNECT: cEditBox.SetText("Trying to communicate with the scale..."); mScale.EditItem(ID_SCALE_CONNECT, "&Connecting...", MF_DISABLED); _beginthread(ScaleConnect, 0, (void*) hWnd); break; case ID_FILE_NEW: if (!bSaved && vMealItems.size() > 0) { switch(MessageBox(hWnd, "You haven't saved the current meal, would you like to?","Hold Up!", MB_YESNOCANCEL | MB_ICONINFORMATION)) { case IDYES: if (CheckAndSave()==-1) break; case IDNO: cListBox.Clear(); vMealItems.clear(); strcpy(cSaveName, ""); cWindow.SetTitle("MealTrack - Untitled"); cButton.SetText("Start Meal"); break; case IDCANCEL: default: break; } } else { cListBox.Clear(); vMealItems.clear(); strcpy(cSaveName, ""); cWindow.SetTitle("MealTrack - Untitled"); cButton.SetText("Start Meal"); } break; case ID_FILE_SAVE: GetSaveFile(hWnd); if (vMealItems.size() >= 1) CheckAndSave(); char tmp[313]; sprintf(tmp, "MealTrack - %s", strrchr(cSaveName,'\\') + 1); cWindow.SetTitle(tmp); break; case ID_FILE_EXIT: PostMessage(hWnd, WM_CLOSE, 0, 0); break; case ID_SCALE_ZERO: if (scale != NULL) scale->Zero(); break; } }
EditBox* FormWindow::CreateEditBox(const char* lbl_text, int ax, int ay, int aw, int ah, DWORD aid, DWORD pid) { EditBox* edit = 0; ActiveWindow* parent = this; if (pid) parent = FindControl(pid); edit = new(__FILE__,__LINE__) EditBox(parent, ax, ay, aw, ah, aid); if (edit) { edit->SetForm(this); edit->SetText(lbl_text); if (!shown) edit->Hide(); } return edit; }
void ScaleConnect(void* args) { //If we aren't connected, connect! if (scale == NULL) { //Create a new scale and attempt to connect scale = new Scale("COM1"); if (!scale->IsConnected()) { delete scale; scale = new Scale(); } if (!scale->IsConnected()) { char err[256]; sprintf(err, "Failed to connect! ERROR: %i", scale->GetError()); mScale.EditItem(ID_SCALE_CONNECT, "&Connect"); cEditBox.SetText(err); delete scale; scale = NULL; return; } //Begin the thread which sends a message to the Window's event and loops it hScaleThread = (HANDLE)_beginthread(GetScaleReading, 0, args); //Switch the menu to allow for disconnect & other features SendMessage((HWND)args, WM_SCALE_CONNECTED, 0, 0); } else { //Deactivate the meal bMealActive = 0; //Delete the scale which will disconnect it delete scale; scale = NULL; SendMessage((HWND)args, WM_SCALE_DISCONNECTED, 0, 0); } }
// This function is called by the Windows function DispatchMessage() LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT mMsg, WPARAM wParam, LPARAM lParam) { switch (mMsg) { case WM_CREATE: break; case WM_SIZE: break; case WM_COMMAND: //If handle is the window, handle the menu if (hWnd == cWindow.GetHandle()) HandleMenu(hWnd, LOWORD(wParam)); if (LOWORD(wParam) == IDR_START_BUTTON) { //Check if we have a connection to the scale if (scale != NULL && scale->IsConnected()) { //Check if we just finished a meal as well, if we didn't, start one if (!bMealActive) { //If the meal is not active, start it or reset it if (vMealItems.size() == 0) { _beginthread(Meal, 0, (void*)hWnd); } else { vMealItems.clear(); HandleMenu(hWnd, ID_FILE_NEW); } } else { //Otherwise, stop it if (strlen(cSaveName)<=1) GetSaveFile(hWnd); bMealActive = 0; } } } break; case WM_CLOSE: if (strlen(cSaveName) > 1 && !SaveMeal()) { if (CheckAndSave() == -1) break; } else if (!bSaved && vMealItems.size() > 0) { switch(MessageBox(hWnd, "You haven't saved the current meal, would you like to?","Hold Up!", MB_YESNOCANCEL | MB_ICONINFORMATION)) { case IDYES: if (CheckAndSave() == -1) break; case IDNO: if (scale) delete scale; DestroyWindow(hWnd); hWnd = NULL; break; case IDCANCEL: break; default: break; } } else { if (hWnd != NULL) { if (scale) delete scale; DestroyWindow(hWnd); } } break; case WM_DESTROY: PostQuitMessage(0); break; case WM_UPDATE_READING: //Destroy any spare messages if scale is null if (scale == NULL) break; //Retreive the information and print it on the button char tBuf[32]; sprintf(tBuf, "Connected on %s - %.2f %s %s", scale->GetName(), scale->GetWeight(), scale->GetUnit(), (scale->IsStable()?"":"(unstable)")); cEditBox.SetText(tBuf); hScaleThread = (HANDLE)_beginthread(GetScaleReading, 0, (void*) hWnd); break; case WM_SCALE_CONNECTED: ToggleScaleMenu(1); break; case WM_SCALE_DISCONNECTED: ToggleScaleMenu(0); cEditBox.SetText("Disconnected"); break; default: return DefWindowProc (hWnd, mMsg, wParam, lParam); } return 0; }