home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_16 / EMFScope / EMFSCOPE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-10  |  6.1 KB  |  258 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : emfscope.cpp                                                         //
  10. //  Description: EMFScope, main program                                              //
  11. //  Version    : 1.00.001, July 10, 2000                                             //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #include <windows.h>
  16. #include <assert.h>
  17. #include <commctrl.h>
  18.  
  19. #include "Winpp.h"
  20.  
  21. #include "spehon32\\spehon32.h"
  22. #include "resource.h"
  23.  
  24. #include "Emfscope.h"
  25.  
  26. #include "Canvas.h"
  27. #include "Toolbar.h"
  28.  
  29.  
  30. KEmfScope EmfScope;
  31.  
  32.  
  33. void KEmfScope::LinkClientServer(void)
  34. {
  35.     SetProp(hwnd_Spooler, Prop_Client, (HANDLE) ToolBar->m_hWnd);
  36. }
  37.  
  38.  
  39. void KEmfScope::UnlinkClientServer(void)
  40. {
  41.     RemoveProp(hwnd_Spooler, Prop_Client);
  42. }
  43.  
  44.  
  45. void KEmfScope::WakeUp(void)
  46. {
  47.     RECT rect;
  48.  
  49.     GetClientRect(m_hWnd, &rect);
  50.                     
  51.     if ( (rect.right<default_w) || (rect.bottom<default_h) )
  52.         SetWindowPos(m_hWnd, NULL, 0, 0,
  53.              extra_w + default_w,
  54.              extra_h + default_h,
  55.              SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOMOVE);
  56. }
  57.  
  58.  
  59. BOOL KEmfScope::InstallSpehon32(HINSTANCE hInstance)
  60. {
  61.     if ( GetVersion() < 0x80000000)                // Windows NT
  62.         return TRUE;
  63.  
  64.     BOOL spehon32_installed = FALSE;
  65.  
  66.     // try to fin Win95 Spooler main Window
  67.     hwnd_Spooler  = FindWindow(__TEXT("SpoolProcessClass"), __TEXT("Spooler Process"));
  68.     hwnd_Spehon32 = NULL;
  69.  
  70.     if (hwnd_Spooler==NULL)
  71.     {
  72.         HCURSOR oldCursor;
  73.         int     i;
  74.  
  75.         // try to load spooler if can't be found
  76.         WinExec("Spool32.exe", SW_SHOWNORMAL);
  77.         
  78.         oldCursor = SetCursor(LoadCursor(hInstance, IDC_WAIT));
  79.  
  80.         // wait up to 1 second for the Spooler Window to appear, should we ?
  81.         for (i=0; i<10; i++)
  82.         {
  83.             hwnd_Spooler = FindWindow(__TEXT("SpoolProcessClass"), __TEXT("Spooler Process"));
  84.             if (hwnd_Spooler) break;
  85.  
  86.             Sleep(100);
  87.         }
  88.  
  89.         SetCursor(oldCursor);
  90.     }
  91.     else
  92.     {
  93.         hwnd_Spehon32 = (int) GetProp(hwnd_Spooler, Prop_Server);
  94.  
  95.         if (hwnd_Spehon32==id_seed)
  96.             spehon32_installed = TRUE;
  97.     }
  98.  
  99.  
  100.     if (hwnd_Spooler==NULL)
  101.     {
  102.         char title[64];
  103.  
  104.         strcpy(title, LoadStringTemp(IDS_APPTITLE));
  105.         
  106.         MessageBox(0, LoadStringTemp(IDS_SPOOLERMISSING), title, MB_OK);
  107.         
  108.         return FALSE;
  109.     }
  110.     else
  111.     {
  112.         LinkClientServer();
  113.  
  114.         // install message hook if it's not installed yet
  115.         if (!spehon32_installed)
  116.             // set message hook from within spehon32.dll
  117.             SetSpoolerHook(GetWindowThreadProcessId(hwnd_Spooler, NULL), 
  118.                            hwnd_Spooler);
  119.  
  120.         return TRUE;
  121.     }
  122. }
  123.  
  124. const int gap        = 2;
  125. const int margin     = 2;
  126.  
  127. LRESULT KEmfScope::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  128. {
  129.     switch (uMsg)
  130.     {
  131.         case WM_CREATE:
  132.             {
  133.                 HMENU hSys  = GetSystemMenu(hWnd, FALSE);
  134.                 RECT  rect;
  135.     
  136.                 m_hWnd    = hWnd;
  137.                 hMainMenu = LoadMenu(hinst_EmfScope, MAKEINTRESOURCE(IDR_MAIN));
  138.  
  139.                 ToolBar = new KToolBar(hinst_EmfScope, hWnd);
  140.                 
  141.                 GetClientRect(hWnd, &rect);
  142.                 default_w = rect.right - rect.left;
  143.                 default_h = rect.bottom - rect.top;
  144.  
  145.                 Canvas  = new KCanvasWindow (hinst_EmfScope, hWnd, 
  146.                                                2, 2+ToolBar->n_height+2, 
  147.                                             unmapx(default_w-mapx(2*margin)), 
  148.                                             unmapy(default_h-mapy(2*margin-gap-ToolBar->n_height)));
  149.                 
  150.                 // let toolbar communicate scale to Canvas window
  151.                 ToolBar->SetCanvas(Canvas);
  152.  
  153.                 GetWindowRect(hWnd, &rect);
  154.                 
  155.                 extra_w = rect.right - rect.left - default_w;
  156.                 extra_h = rect.bottom - rect.top - default_h;
  157.  
  158.                 SetWindowPos(hWnd, NULL, 0, 0,
  159.                              extra_w + mapx(ToolBar->n_width  + margin*2),
  160.                              extra_h + mapy(ToolBar->n_height + margin*2),
  161.                              SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
  162.             }
  163.             break;
  164.  
  165.         
  166.         case WM_SIZE:
  167.             if (Canvas)
  168.             {
  169.                 RECT rect;
  170.  
  171.                 GetClientRect(hWnd, &rect);
  172.  
  173.                 SetWindowPos(Canvas->m_hWnd, NULL, 0, 0,
  174.                              rect.right -rect.left-mapx(2*2), 
  175.                              rect.bottom-rect.top-mapy(2*3+ToolBar->n_height),
  176.                              SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
  177.             }                            
  178.             break;
  179.  
  180.         
  181.         case WM_DESTROY:
  182.             DestroyMenu(hMainMenu);
  183.             PostQuitMessage(0);
  184.             break;
  185.         
  186.         case WM_SPOOLERSTATUS:
  187.             {
  188.                 char temp[32];
  189.                 wsprintf(temp, "WM_SPOOLERSTATUS %d %d", wParam, lParam);
  190.                 OutputDebugString(temp);
  191.                 
  192.                 DWORD pcbNeeded;
  193.                 DWORD pcReturned;
  194.                 BOOL b= EnumJobs(NULL, 0, 10, 1, NULL, 0, &pcbNeeded, &pcReturned);
  195.             }
  196.             break;
  197.  
  198.         case WM_CLOSE:
  199.             ToolBar->SaveSettings();
  200.             UnlinkClientServer();
  201.             // break;
  202.  
  203.         default:
  204.             return DefWindowProc(hWnd, uMsg, wParam, lParam);
  205.     }
  206.  
  207.     return 0;
  208. }
  209.  
  210.  
  211. KEmfScope::~KEmfScope(void)
  212. {
  213.     if ( ToolBar )
  214.     {
  215.         delete ToolBar;   
  216.         ToolBar = NULL;
  217.     }
  218.  
  219.     if ( Canvas )
  220.     {
  221.         delete Canvas;      
  222.         Canvas  = NULL;
  223.     }
  224. }
  225.  
  226.  
  227. HWND KEmfScope::CreateMainWindow(HINSTANCE hInst)
  228. {
  229.     hinst_EmfScope = hInst;
  230.  
  231.     Createwindow("EmfScope",
  232.                   IDI_COLIVE,
  233.                   0,
  234.                   LoadStringTemp(IDS_APPTITLE), 
  235.                   WS_VISIBLE | WS_OVERLAPPEDWINDOW,
  236.                   CW_USEDEFAULT, 0,
  237.                   CW_USEDEFAULT, 0, 
  238.                   NULL,
  239.                   hInst,
  240.                   NULL, 0, GetSysColorBrush(COLOR_MENU));
  241.  
  242.     if (!InstallSpehon32(hInst))
  243.         return NULL;
  244.  
  245.     return m_hWnd;
  246. }
  247.  
  248.  
  249. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdShow)
  250. {
  251.     InitCommonControls();
  252.  
  253.     if ( !EmfScope.CreateMainWindow(hInstance) )
  254.         return FALSE;
  255.  
  256.     return EmfScope.MessageLoop(SW_SHOWNORMAL);
  257. }
  258.