---- 小灯Light的C++(www.cppentry.com)定义文件 ---- /* light.h */ #ifndef LIGHT_H #define LIGHT_H class Light { private: int state_var; public: Light(); void turnOn(); void turnOff(); void performAction(); }; #endif ---- 小灯Light的C++(www.cppentry.com)实现文件 ---- /* light.cpp */ #include <iostream> using namespace std; #include "light.h" extern void signal_to_light_UI(int); Light::Light() { state_var = 0; } void Light::turnOn() { if (state_var != 0)return; state_var = 1; this->performAction(); } void Light::turnOff() { if (state_var != 1)return; state_var = 0; this->performAction(); } void Light::performAction() { if (state_var == 0) signal_to_light_UI(0); else if (state_var == 1) signal_to_light_UI(1); } //end ---- 冰箱Refrigerator的C++(www.cppentry.com)定义文件 ---- /* refrigerator.h */ #ifndef REFRIGERATOR_H #define REFRIGERATOR_H #include "light.h" class Refrigerator { private: int state_var; Light* light_obj; public: Refrigerator(); void openTheDoor(); void closeTheDoor(); void performAction(); }; #endif ---- 冰箱Refrigerator的C++(www.cppentry.com)实现文件 ---- /* refrigerator.cpp */ #include <iostream> using namespace std; #include "refrigerator.h" #include "mb.h" extern void signal_to_button1_UI(int); extern void signal_to_button2_UI(int); void Refrigerator::openTheDoor() { if (state_var != 0)return; state_var = 1; this->performAction(); } void Refrigerator::closeTheDoor() { if (state_var != 1)return; state_var = 0; this->performAction(); } void Refrigerator::performAction() { Light* light_obj; light_obj = MB::get_light_obj(); if (state_var == 0) { signal_to_button1_UI(0); signal_to_button2_UI(1); light_obj->turnOff(); } else if (state_var == 1) { signal_to_button1_UI(1); signal_to_button2_UI(0); light_obj->turnOn(); }} Refrigerator::Refrigerator() { state_var = 0; signal_to_button1_UI(0); signal_to_button2_UI(1); } //end ---- MB的C++(www.cppentry.com)定义文件 ---- /* mb.h */ #ifndef MB_H #define MB_H #include "refrigerator.h" #include "light.h" class MB { private: static Refrigerator* refri_obj; static Light* light_obj; public: MB(); static void initial(); static Refrigerator* get_refrigerator_obj(); static Light* get_light_obj(); }; #endif ---- MB的C++(www.cppentry.com)实现文件 ---- /* mb.cpp */ #include <iostream> using namespace std; #include "mb.h" #include "refrigerator.h" #include "light.h" MB::MB(){} void MB::initial() { refri_obj = new Refrigerator(); light_obj = new Light(); }
Refrigerator* MB::get_refrigerator_obj() { return refri_obj; } Light* MB::get_light_obj() { return light_obj; }
Light* MB::light_obj = NULL; Refrigerator* MB::refri_obj = NULL; //end ---- Win32的GUI定义/实现文件 ---- /* main.cpp */ #include <windows.h> #include "resource.h" #include "refrigerator.h" #include "mb.h" const char ClassName[] = "MainWindowClass"; HWND hWndButton1, hWndButton2, hWndButton3; PAINTSTRUCT ps; HDC hdc; int st; Refrigerator* refri; void signal_to_button1_UI(int ab) { EnableWindow(hWndButton1, ab); } void signal_to_button2_UI(int ab) { EnableWindow(hWndButton2, ab); } void signal_to_light_UI(int st) { if (st == 1) SetWindowText(hWndButton3, "Light(ON)"); else SetWindowText(hWndButton3, "Light(OFF)"); } LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) { switch (Msg){ case WM_CREATE: { HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE); hWndButton1= CreateWindowEx( 0, "BUTTON", "Door(close)", WS_VISIBLE | WS_CHILD, 10, 30, 120, 35, hWnd, (HMENU)IDB_BUTTON1, hInstance, NULL);
hWndButton2= CreateWindowEx( 0, "BUTTON", "Door(open)", WS_VISIBLE | WS_CHILD, 10, 120, 120, 35, hWnd, (HMENU)IDB_BUTTON2, hInstance, NULL);
hWndButton3= CreateWindowEx( 0, "BUTTON", "Light(OFF)", WS_VISIBLE | WS_CHILD, 200, 60, 120, 35, hWnd, (HMENU)IDB_BUTTON3, hInstance, NULL); } //-------------------------------------- MB::initial(); refri = MB::get_refrigerator_obj(); //-------------------------------------- break; case WM_COMMAND: { switch(LOWORD(wParam)){ case IDM_FILE_EXIT: PostMessage(hWnd, WM_CLOSE, 0, 0); break; case IDB_BUTTON1: { switch (HIWORD(wParam)){ case BN_CLICKED: refri->closeTheDoor(); break; } } break; case IDB_BUTTON2: { switch (HIWORD(wParam)){ case BN_CLICKED: refri->openTheDoor(); break; } } break; case IDM_ITEM: MessageBox(NULL, "Not Implemented", "", MB_OK | MB_ICONINFORMATION); break; } return 0; } break; case WM_CLOSE: DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return (DefWindowProc(hWnd, Msg, wParam, lParam)); } return 0; } INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow ) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wc.lpszClassName = ClassName; if (!RegisterClassEx(&wc))return 0; HWND hWnd; hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,ClassName, "Class Menu", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); if (!hWnd)return 0; ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd); MSG Msg; while (GetMessage(&Msg, NULL, 0, 0)){ TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } |