home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR9 / WDOS0793.ZIP / BONNEAU.ZIP / MODELESS.C < prev    next >
C/C++ Source or Header  |  1993-05-05  |  6KB  |  200 lines

  1. /*****************************************************/
  2. /* modeless.c                                        */
  3. /* -- Demonstrates modeless common dialogs.          */
  4. /* -- To compile:                                    */
  5. /*    cc modeless.c modeless.rc toolhelp.lib         */
  6. /*****************************************************/
  7.  
  8. #include <windows.h>
  9. #include <windowsx.h>
  10. #include <commdlg.h>
  11. #include <toolhelp.h>
  12. #include "modeless.h"
  13.  
  14. LRESULT CALLBACK __export LWndProc(HWND, UINT, WPARAM,
  15.                                                     LPARAM);
  16. BOOL    FHook(HWND, UINT, WPARAM, LPARAM);
  17. BOOL    ModelessChooseFont(HWND);
  18.  
  19. char    szApp[] = "Modeless";   /* App's name. */
  20. HWND    hwndEdit;               /* Edit control */
  21.  
  22. #define didEdit     1       /* Edit control id. */
  23. #define didApply    0x402   /* Apply button id. */
  24.  
  25. #ifdef __BORLANDC__
  26.     #pragma argsused
  27. #endif
  28. int PASCAL WinMain(HINSTANCE hins, HINSTANCE hinsPrev,
  29.                                        LPSTR lsz, int wShow)
  30.     {
  31.     MSG     msg;
  32.     HWND    hwnd;
  33.  
  34.     if (hinsPrev == NULL)
  35.         {
  36.         WNDCLASS    wcs;
  37.  
  38.         wcs.style = CS_HREDRAW | CS_VREDRAW;
  39.         wcs.lpfnWndProc = LWndProc;
  40.         wcs.cbClsExtra = 0;
  41.         wcs.cbWndExtra = 0;
  42.         wcs.hInstance = hins;
  43.         wcs.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  44.         wcs.hCursor = LoadCursor(NULL, IDC_ARROW);
  45.         wcs.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  46.         wcs.lpszMenuName = MAKEINTRESOURCE(midFonts);
  47.         wcs.lpszClassName = szApp;
  48.         if (!RegisterClass(&wcs))
  49.             return 0;
  50.         }
  51.  
  52.     msg.wParam = 0;
  53.     if ((hwnd = CreateWindow(szApp, "Modeless Common "
  54.       "Dialog Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  55.       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  56.       NULL, NULL, hins, NULL)) != NULL)
  57.         {
  58.         ShowWindow(hwnd, wShow);
  59.         while (GetMessage(&msg, NULL, 0, 0))
  60.             {
  61.             TranslateMessage(&msg);
  62.             DispatchMessage(&msg);
  63.             }
  64.         }
  65.  
  66.     return msg.wParam;
  67.     }
  68.  
  69. LRESULT CALLBACK __export LWndProc(HWND hwnd, UINT wm,
  70.                                WPARAM wParam, LPARAM lParam)
  71.     {
  72.     switch (wm)
  73.         {
  74.     default:
  75.         break;
  76.  
  77.     case WM_CREATE:
  78.         if ((hwndEdit = CreateWindow("edit", "Type here",
  79.           WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  80.             ES_MULTILINE, 0, 0, 0, 0, hwnd,
  81.           (HMENU)didEdit, GetWindowInstance(hwnd),
  82.           NULL)) == NULL)
  83.             return -1;
  84.         break;
  85.  
  86.     case WM_SIZE:
  87.         SetWindowPos(hwndEdit, NULL, 0, 0,
  88.           LOWORD(lParam), HIWORD(lParam),
  89.           SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  90.         break;
  91.  
  92.     case WM_COMMAND:
  93.         if (wParam == idmFonts)
  94.             ModelessChooseFont(hwnd);
  95.         break;
  96.  
  97.     case WM_DESTROY:
  98.         PostQuitMessage(0);
  99.         break;
  100.  
  101.     case WM_USER:
  102.         return FHook(((LPMSG)lParam)->hwnd,
  103.           ((LPMSG)lParam)->message,
  104.           ((LPMSG)lParam)->wParam,
  105.           ((LPMSG)lParam)->lParam);
  106.         }
  107.  
  108.     return DefWindowProc(hwnd, wm, wParam, lParam);
  109.     }
  110.  
  111. BOOL FHook(HWND hwnd, UINT wm, WPARAM wParam, LPARAM lParam)
  112. /*****************************************************/
  113. /* -- ChooseFont dialog filter.                      */
  114. /*****************************************************/
  115.     {
  116.     LOGFONT         gft;
  117.     static HFONT    hfnt;
  118.     HFONT           hfntNew;
  119.  
  120.     if (wm == WM_INITDIALOG)
  121.         {
  122.         EnableWindow(GetWindowOwner(hwnd), TRUE);
  123.         return TRUE;
  124.         }
  125.  
  126.     if (wm != WM_COMMAND ||
  127.       !(wParam == didApply || wParam == IDOK))
  128.         return FALSE;
  129.  
  130.     SendMessage(hwnd, WM_CHOOSEFONT_GETLOGFONT, 0,
  131.       (LPARAM)(LPLOGFONT)&gft);
  132.     if ((hfntNew = CreateFontIndirect(&gft)) != NULL)
  133.         {
  134.         SendMessage(hwndEdit, WM_SETFONT,
  135.           (WPARAM)hfntNew, TRUE);
  136.         if (hfnt != NULL)
  137.             DeleteObject(hfnt);
  138.         hfnt = hfntNew;
  139.         }
  140.  
  141.     return wParam == didApply;
  142.     }
  143.  
  144. BOOL ModelessChooseFont(HWND hwnd)
  145. /*****************************************************/
  146. /* -- Display the choose font common dialog.         */
  147. /* -- Create another task to carry out this request. */
  148. /* -- hwnd  : Owner window for dialog.               */
  149. /*****************************************************/
  150.     {
  151.     HINSTANCE       hins;
  152.     TASKENTRY       tsk;
  153.     LPCHOOSEFONT    lpcsf;
  154.  
  155.     if ((hins = (HINSTANCE)WinExec("dlgservr.exe", 0))
  156.       < HINSTANCE_ERROR)
  157.         return FALSE;
  158.  
  159.     /* Find task.  Return false if we can't find it. */
  160.     tsk.dwSize = sizeof tsk;
  161.     if (!TaskFirst(&tsk))
  162.         return FALSE;
  163.     do
  164.         if (tsk.hInst == hins)
  165.             break;
  166.     while (TaskNext(&tsk));
  167.     if (tsk.hInst != hins)
  168.         return FALSE;
  169.  
  170.     /* Get block big enough for CHOOSEFONT, LOGFONT, */
  171.     /* and lpszStyle string. */
  172.     if ((lpcsf = GlobalAllocPtr(GHND | GMEM_SHARE,
  173.       sizeof(CHOOSEFONT) + sizeof(LOGFONT) +
  174.       LF_FACESIZE)) == NULL)
  175.         {
  176.         TerminateApp(tsk.hTask, NO_UAE_BOX);
  177.         return FALSE;
  178.         }
  179.  
  180.     lpcsf->lStructSize = sizeof(CHOOSEFONT);
  181.     lpcsf->hwndOwner = hwnd;
  182.     lpcsf->lpLogFont = (LPLOGFONT)((LPBYTE)lpcsf +
  183.       sizeof(CHOOSEFONT));
  184.     lpcsf->Flags = CF_SCREENFONTS | CF_EFFECTS |
  185.       CF_APPLY | CF_ENABLEHOOK |
  186.       CF_INITTOLOGFONTSTRUCT | CF_USESTYLE |
  187.       CF_FORCEFONTEXIST;
  188.     lpcsf->lpszStyle = (LPSTR)lpcsf->lpLogFont +
  189.       sizeof(LOGFONT);
  190.     lpcsf->nFontType = SCREEN_FONTTYPE;
  191.  
  192.     /* Store pointer here so it gets passed to hook */
  193.     /* via WM_INITDIALOG. */
  194.     lpcsf->lCustData = (LPARAM)lpcsf;
  195.  
  196.     PostAppMessage(tsk.hTask, WM_USER, 0,
  197.       (LPARAM)lpcsf);
  198.     return TRUE;
  199.     }
  200.