home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / mysqlshutdown / mysqlshutdown.c < prev    next >
C/C++ Source or Header  |  2000-08-23  |  7KB  |  199 lines

  1. /****************************************************************************
  2.    MySqlShutdown - shutdown MySQL on system shutdown (Win95/98)
  3.  ----------------------------------------------------------------------------
  4.  Revision History :
  5.  Version  Author   Date         Description
  6.  001.00   Irena    21-12-99
  7. *****************************************************************************/
  8. #include <windows.h>
  9.  
  10. //-----------------------------------------------------------------------
  11. // Local  data
  12. //-----------------------------------------------------------------------
  13. static char szAppName[] = "MySqlShutdown";
  14. static HINSTANCE hInstance;
  15.  
  16. #define MYWM_NOTIFYICON        (WM_APP+100)
  17.  
  18. //-----------------------------------------------------------------------
  19. // Exported functions
  20. //-----------------------------------------------------------------------
  21. LRESULT CALLBACK MainWindowProc (HWND, UINT, WPARAM, LPARAM);
  22.  
  23. //-----------------------------------------------------------------------
  24. // Local functions
  25. //-----------------------------------------------------------------------
  26. static BOOL InitAppClass (HINSTANCE hInstance);
  27.  
  28. BOOL TrayMessageAdd(HWND hWnd, DWORD dwMessage)
  29. {
  30.     BOOL res;
  31.     HICON hIcon =LoadIcon (hInstance, "MySql");
  32.     char *szTip="MySql Shutdown";
  33.     NOTIFYICONDATA tnd;
  34.  
  35.     tnd.cbSize        = sizeof(NOTIFYICONDATA);
  36.     tnd.hWnd        = hWnd;
  37.     tnd.uID            = 101;
  38.  
  39.     tnd.uFlags        = NIF_MESSAGE|NIF_ICON|NIF_TIP;
  40.     tnd.uCallbackMessage    = MYWM_NOTIFYICON;
  41.     tnd.hIcon        = hIcon;
  42.     strcpy(tnd.szTip, szTip);
  43.     res = Shell_NotifyIcon(dwMessage, &tnd);
  44.  
  45.     if (hIcon) DestroyIcon(hIcon);
  46.  
  47.     return res;
  48. }
  49.  
  50. //-----------------------------------------------------------------------
  51. //   Name:      WinMain
  52. //   Purpose: Main application entry point
  53. //-----------------------------------------------------------------------
  54.  
  55. int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  56. {   HWND  hWnd;
  57.     MSG   Msg;
  58.  
  59.     hInstance=hInst;
  60.     // Register application class if needed
  61.     if (InitAppClass (hInstance) == FALSE) return (0);
  62.  
  63.  
  64.     hWnd = CreateWindow (szAppName, "MySql",
  65.                         WS_OVERLAPPEDWINDOW|WS_MINIMIZE,
  66.                         0, 0,
  67.                         GetSystemMetrics(SM_CXSCREEN)/4,
  68.                         GetSystemMetrics(SM_CYSCREEN)/4,
  69.                         0, 0, hInstance, NULL);
  70.  
  71.     if(!hWnd)
  72.     {
  73.         return (0);
  74.     }
  75.     ShowWindow (hWnd, SW_HIDE);
  76.     UpdateWindow (hWnd);
  77.     while (GetMessage (&Msg, 0, 0, 0))
  78.     {  TranslateMessage (&Msg);
  79.         DispatchMessage (&Msg);
  80.     }
  81.     return ((int) (Msg.wParam));
  82. }
  83.  
  84. //-----------------------------------------------------------------------
  85. //   Name:    InitAppClass
  86. //   Purpose: Register the main application window class
  87. //-----------------------------------------------------------------------
  88. static BOOL InitAppClass (HINSTANCE hInstance)
  89. {
  90.     WNDCLASS cls;
  91.  
  92.     if (GetClassInfo (hInstance, szAppName, &cls) == 0)
  93.     {
  94.         cls.style          = CS_HREDRAW | CS_VREDRAW ;;
  95.         cls.lpfnWndProc    = (WNDPROC) MainWindowProc;
  96.         cls.cbClsExtra     = 0;
  97.         cls.cbWndExtra     = sizeof(HWND);
  98.         cls.hInstance      = hInstance;
  99.         cls.hIcon          = LoadIcon (hInstance, "MySql");
  100.         cls.hCursor        = LoadCursor (NULL, IDC_ARROW);
  101.         cls.hbrBackground  = GetStockObject (WHITE_BRUSH) ;
  102.         cls.lpszMenuName   = 0; //szAppName;
  103.         cls.lpszClassName  = szAppName;
  104.         return RegisterClass (&cls);
  105.     }
  106.     return (TRUE);
  107. }
  108. //-----------------------------------------------------------------------
  109. //   Name:      MainWindowProc
  110. //   Purpose: Window procedure for main application window.
  111. //-----------------------------------------------------------------------
  112. LRESULT CALLBACK MainWindowProc (HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam)
  113. {
  114.  static RECT   rect ;
  115.  HDC           hdc ;
  116.  PAINTSTRUCT   ps ;
  117.  static BOOL   bShutdown=FALSE;
  118.  
  119.     switch (Msg)
  120.     {
  121.           case WM_CREATE:
  122.                TrayMessageAdd(hWnd, NIM_ADD);
  123.                return TRUE;
  124. /***************
  125.           case WM_SYSCOMMAND:
  126.                if(wParam==SC_CLOSE)
  127.                { HANDLE hEventShutdown;
  128.  
  129.                 bShutdown=TRUE;
  130.                 InvalidateRect(hWnd,NULL,TRUE);
  131.                 ShowWindow (hWnd, SW_NORMAL);
  132.                 UpdateWindow(hWnd);
  133.                 hEventShutdown=OpenEvent(EVENT_MODIFY_STATE, 0, "MySqlShutdown");
  134.                 if(hEventShutdown)
  135.                 {
  136.                   SetEvent(hEventShutdown);
  137.                   CloseHandle(hEventShutdown);
  138.                   Sleep(1000);
  139.                   MessageBox(hWnd,"Shutdown", "MySql", MB_OK);
  140.                 }
  141.                TrayMessageAdd(hWnd, NIM_DELETE);
  142.                }
  143.                break;
  144. **************/
  145.           case WM_DESTROY:
  146.                TrayMessageAdd(hWnd, NIM_DELETE);
  147.                PostQuitMessage (0);
  148.                return 0;
  149.           case WM_SIZE:
  150.                GetClientRect (hWnd, &rect) ;
  151.                return 0 ;
  152.  
  153.           case WM_PAINT:
  154.                hdc = BeginPaint (hWnd, &ps) ;
  155.                if(bShutdown)
  156.                  DrawText (hdc, "MySql shutdown in progress...",
  157.                                 -1, &rect, DT_WORDBREAK) ;
  158.                EndPaint (hWnd, &ps) ;
  159.                return 0 ;
  160.           case WM_QUERYENDSESSION: //Shutdown MySql
  161.                { HANDLE hEventShutdown;
  162.  
  163.                 bShutdown=TRUE;
  164.                 InvalidateRect(hWnd,NULL,TRUE);
  165.                 ShowWindow (hWnd, SW_NORMAL);
  166.                 UpdateWindow(hWnd);
  167.                 hEventShutdown=OpenEvent(EVENT_MODIFY_STATE, 0, "MySqlShutdown");
  168.                 if(hEventShutdown)
  169.                 {
  170.                   SetEvent(hEventShutdown);
  171.                   CloseHandle(hEventShutdown);
  172.                   Sleep(1000);
  173.                   MessageBox(hWnd,"Shutdown", "MySql", MB_OK);
  174.                 }
  175.                }
  176.                return 1;
  177.  
  178.           case MYWM_NOTIFYICON:
  179.            switch (lParam)
  180.            {
  181.              case WM_LBUTTONDOWN:
  182.              case WM_RBUTTONDOWN:
  183.                   ShowWindow(hWnd, SW_SHOWNORMAL);
  184.                   SetForegroundWindow(hWnd);    // make us come to the front
  185.                   break;
  186.              default:
  187.                   break;
  188.              }
  189.              break;
  190.  
  191.     }
  192.     return DefWindowProc (hWnd, Msg, wParam, lParam);
  193. }
  194.  
  195.  
  196. // ----------------------- The end ------------------------------------------
  197.  
  198.  
  199.