home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / simple.frm / formwnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  13.0 KB  |  445 lines

  1. /***********************************************************************
  2.  *
  3.  *  FORMWND.CPP
  4.  *
  5.  *
  6.  *  Copyright 1986-1996 Microsoft Corporation.  All Rights Reserved.
  7.  *
  8.  ***********************************************************************/
  9.  
  10. #include        "precomp.h"
  11.  
  12. BOOL CALLBACK
  13. AboutDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
  14.  
  15. //WM_SIZE handler
  16. void ON_Size(HWND hwnd, UINT state, int x, int y)
  17. {
  18.     CBaseForm * pform = (CBaseForm *) GetWindowLong(hwnd, 0);
  19.  
  20.     SetWindowPos(pform->m_hwndDialog, NULL, 0, 0, x, y,
  21.               SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  22.  
  23.  
  24. ////  A specialized version of the window proc which plays how messages
  25. //      are going to be handled.
  26. //   
  27.  
  28.  
  29. ////
  30.  
  31. BOOL CALLBACK FormDlgProcSend(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33.     CBaseForm *         pfrm;
  34.     RECT                rect;
  35.     int                 wmId;
  36.     int                 wmEvent;
  37.     
  38.     
  39.     pfrm = (CBaseForm *) GetWindowLong(GetParent(hdlg), 0);
  40.     
  41.     switch ( msg ) {
  42.     case WM_INITDIALOG:
  43.         GetWindowRect(hdlg, &rect);
  44.         pfrm->SetMinSize(rect.right - rect.left,
  45.                          rect.bottom - rect.top +
  46.                          GetSystemMetrics(SM_CYCAPTION) +
  47.                          GetSystemMetrics(SM_CYMENU));
  48.         break;
  49.  
  50.     case WM_SIZE:
  51.         {
  52.             HWND        hwnd;
  53.             RECT        rect;
  54.             int         cx;
  55.             int         cy;
  56.             
  57.             //resize edit to match the dialog
  58.             GetWindowRect(hdlg, &rect);
  59.             cx = rect.right;
  60.             cy = rect.bottom;
  61.  
  62.             HDWP hdwp = BeginDeferWindowPos(4);
  63.             if(NULL == hdwp)
  64.                 break;
  65.             
  66.             hwnd = GetDlgItem(hdlg, ID_TO);
  67.             GetWindowRect(hwnd, &rect);
  68.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  69.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  70.                          SWP_NOZORDER))
  71.             {
  72.                 break;
  73.             }
  74.  
  75.             hwnd = GetDlgItem(hdlg, ID_CC);
  76.             GetWindowRect(hwnd, &rect);
  77.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  78.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  79.                          SWP_NOZORDER))
  80.             {
  81.                 break;
  82.             }
  83.  
  84.             hwnd = GetDlgItem(hdlg, ID_SUBJECT);
  85.             GetWindowRect(hwnd, &rect);
  86.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  87.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  88.                          SWP_NOZORDER))
  89.             {
  90.                 break;
  91.             }
  92.  
  93.             hwnd = GetDlgItem(hdlg, ID_BODY);
  94.             GetWindowRect(hwnd, &rect);
  95.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left, cy - rect.top,
  96.                          SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER))
  97.             {
  98.                 break;
  99.             }
  100.  
  101.             EndDeferWindowPos(hdwp);
  102.         }
  103.         break;
  104.  
  105.     case WM_COMMAND:
  106.         wmId = GET_WM_COMMAND_ID(wParam, lParam);  
  107.         wmEvent = GET_WM_COMMAND_CMD(wParam, lParam);
  108.  
  109.         switch(wmId) {
  110.         case ID_TO_BUTTON:
  111.         case ID_CC_BUTTON:
  112.             switch (wmEvent) {
  113.             case BN_CLICKED:
  114.                 pfrm->Address(wmId);
  115.                 break;
  116.  
  117.             default:
  118.                 return FALSE;
  119.             }
  120.             break;
  121.         case ID_BODY:
  122.         {
  123.             HMENU hMenu = GetMenu(GetParent(hdlg));
  124.             if(wmEvent == EN_KILLFOCUS)
  125.             {
  126.                 EnableMenuItem(hMenu, IDC_EDIT_CUT, MF_BYCOMMAND|MF_GRAYED);
  127.                 EnableMenuItem(hMenu, IDC_EDIT_COPY, MF_BYCOMMAND|MF_GRAYED);
  128.                 EnableMenuItem(hMenu, IDC_EDIT_PASTE, MF_BYCOMMAND|MF_GRAYED);
  129.                 EnableMenuItem(hMenu, IDC_EDIT_UNDO, MF_BYCOMMAND|MF_GRAYED);
  130.             }
  131.             else if(wmEvent == EN_SETFOCUS)
  132.             {
  133.                 EnableMenuItem(hMenu, IDC_EDIT_CUT, MF_BYCOMMAND);
  134.                 EnableMenuItem(hMenu, IDC_EDIT_COPY, MF_BYCOMMAND);
  135.                 EnableMenuItem(hMenu, IDC_EDIT_PASTE, MF_BYCOMMAND);
  136.                 EnableMenuItem(hMenu, IDC_EDIT_UNDO, MF_BYCOMMAND);
  137.             }
  138.         }
  139.         break;
  140.         case ID_SUBJECT:
  141.             if (wmEvent == EN_KILLFOCUS) {
  142.                 char    sz[250];
  143.  
  144.                 if (Edit_GetText(GET_WM_COMMAND_HWND(wParam, lParam), sz, 200)) {
  145.                     lstrcat(sz, " - ");
  146.                     lstrcat(sz, g_szWindowCaption);
  147.                     SetWindowText(GetParent(hdlg), sz);
  148.                 }
  149.                 else
  150.                     SetWindowText(GetParent(hdlg), g_szWindowCaption);
  151.  
  152.                 break;
  153.             }
  154.             return FALSE;
  155.  
  156.         default:
  157.             return FALSE;
  158.         }
  159.         
  160.     default:
  161.         return FALSE;
  162.     }
  163.  
  164.     return TRUE;
  165. }
  166.  
  167. //// Window proc for frame window of both the read and send form.
  168. //   
  169. //  Send frame window does not receive IDC_VIEW_ITEMABOVE, IDC_VIEW_ITEMBELOW
  170. // commands.
  171. // Read frame window does not receive IDC_MESSAGE_SUBMIT command.
  172. LRESULT CALLBACK WndProcForm(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  173. {
  174.     HMENU               hMenu;
  175.     CBaseForm *         pform;
  176.     int                 wmId;
  177.     int                 wmEvent;
  178.  
  179.     pform = (CBaseForm *) GetWindowLong(hwnd, 0);
  180.     
  181.     switch( msg ) {
  182.     case WM_COMMAND:
  183.         wmId = GET_WM_COMMAND_ID(wParam, lParam);  
  184.         wmEvent = GET_WM_COMMAND_CMD(wParam, lParam);
  185.  
  186.         switch( wmId ) {
  187.         case IDC_MESSAGE_CLOSE:
  188.             pform->ShutdownForm(SAVEOPTS_PROMPTSAVE);
  189.             break;
  190.  
  191.         case IDC_MESSAGE_SAVE:
  192.             pform->DoSave();
  193.             break;
  194.         
  195.         case IDC_VIEW_ITEMABOVE:
  196.         case IDC_VIEW_ITEMBELOW:
  197.             {
  198.                 ULONG ulDir = IDC_VIEW_ITEMABOVE == wmId ? VCDIR_PREV:VCDIR_NEXT;
  199.                 pform->DoNext(ulDir);
  200.                 
  201.                 break;
  202.             }
  203.         case IDC_MESSAGE_DELETE:
  204.             pform->DoDelete();
  205.             break;
  206.         
  207.         case IDC_MESSAGE_REPLY:
  208.             pform->DoReply(eREPLY);
  209.             break;
  210.  
  211.         case IDC_MESSAGE_FORWARD:
  212.             pform->DoReply(eFORWARD);
  213.             break;
  214.  
  215.         case IDC_MESSAGE_SUBMIT:
  216.             pform->DoSubmit();
  217.             break;
  218.             
  219.         case IDC_MESSAGE_COPY:
  220.             pform->DoCopy();
  221.             break;
  222.  
  223.         case IDC_MESSAGE_MOVE:
  224.             pform->DoMove();
  225.             break;
  226.             
  227.         //the edit menu commands are enabled only if the ID_BODY text
  228.         //box has the focus
  229.         case IDC_EDIT_COPY:
  230.             SendMessage(GetDlgItem(pform->m_hwndDialog, ID_BODY), WM_COPY, 0,0);
  231.             break;
  232.         case IDC_EDIT_CUT:
  233.             SendMessage(GetDlgItem(pform->m_hwndDialog, ID_BODY), WM_CUT, 0,0);
  234.             break;
  235.         case IDC_EDIT_PASTE:
  236.             SendMessage(GetDlgItem(pform->m_hwndDialog, ID_BODY), WM_PASTE, 0,0);
  237.             break;
  238.         case IDC_EDIT_UNDO:
  239.             SendMessage(GetDlgItem(pform->m_hwndDialog, ID_BODY), WM_UNDO, 0,0);
  240.             break;
  241.  
  242.         case IDC_HELP_ABOUT:
  243.             DialogBox (g_hinst, "AboutBox", hwnd, AboutDlgProc);
  244.             break;
  245.  
  246.         default:
  247.             return DefWindowProc(hwnd, msg, wParam, lParam);
  248.         }
  249.         break;
  250.  
  251.         //
  252.         //  Do all of the correct menu graying
  253.         //
  254.     case WM_INITMENU:
  255.         hMenu = (HMENU) wParam;
  256.         pform->ConfigMenu(hMenu);
  257.    break;
  258.         
  259.         //
  260.         //  Deal with the System Close message
  261.         //
  262.     case WM_SYSCOMMAND:
  263.         if ((wParam & 0xfff0) == SC_CLOSE)
  264.         {
  265.             pform->ShutdownForm(SAVEOPTS_PROMPTSAVE);
  266.         }
  267.         else
  268.         {
  269.             goto DoDefault;
  270.         }
  271.         break;
  272.  
  273.     case WM_SETFOCUS:
  274.         SetFocus(pform->m_hwndDialog);
  275.         break;
  276.  
  277.         //
  278.         //  Let the object know that the window has been destroyed so 
  279.         //      that it will not talk to it any more
  280.         //
  281.     case WM_DESTROY:
  282.         pform->ClearWindow();
  283.         goto DoDefault;
  284.  
  285.     case WM_SIZE:
  286.         HANDLE_WM_SIZE(hwnd, wParam, lParam, ON_Size); 
  287.         goto DoDefault;
  288.         
  289.         
  290.     default:
  291. DoDefault:
  292.         return DefWindowProc (hwnd, msg, wParam, lParam);
  293.     }
  294.  
  295.     return 0;
  296. }
  297.  
  298.  
  299. ////
  300.  
  301. BOOL CALLBACK FormDlgProcRead(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
  302. {
  303.     CBaseForm *         pfrm;
  304.     RECT                rect;
  305.     int                 wmId;
  306.     int                 wmEvent;
  307.     
  308.     pfrm = (CBaseForm *) GetWindowLong(GetParent(hdlg), 0);
  309.     
  310.     switch ( msg ) {
  311.     case WM_INITDIALOG:
  312.         {
  313.             Assert(pfrm);
  314.  
  315.             GetWindowRect(hdlg, &rect);
  316.             pfrm->SetMinSize(rect.right - rect.left,
  317.                             rect.bottom - rect.top +
  318.                             GetSystemMetrics(SM_CYCAPTION) +
  319.                             GetSystemMetrics(SM_CYMENU));
  320.             break;
  321.         }                                                   
  322.     
  323.     case WM_SIZE:
  324.         {
  325.             HWND        hwnd;
  326.             RECT        rect;
  327.             int         cx;
  328.             int         cy;
  329.  
  330.             GetWindowRect(hdlg, &rect);
  331.             cx = rect.right;
  332.             cy = rect.bottom;
  333.  
  334.             HDWP hdwp = BeginDeferWindowPos(5);
  335.             if(NULL == hdwp)
  336.                 break;
  337.  
  338.             hwnd = GetDlgItem(hdlg, ID_FROM);
  339.             GetWindowRect(hwnd, &rect);
  340.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  341.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  342.                          SWP_NOZORDER))
  343.             {
  344.                 break;
  345.             }
  346.  
  347.             hwnd = GetDlgItem(hdlg, ID_SENT);
  348.             GetWindowRect(hwnd, &rect);
  349.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  350.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  351.                          SWP_NOZORDER))
  352.             {
  353.                 break;
  354.             }
  355.  
  356.             hwnd = GetDlgItem(hdlg, ID_TO);
  357.             GetWindowRect(hwnd, &rect);
  358.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  359.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  360.                          SWP_NOZORDER))
  361.             {
  362.                 break;
  363.             }
  364.  
  365.             hwnd = GetDlgItem(hdlg, ID_SUBJECT);
  366.             GetWindowRect(hwnd, &rect);
  367.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left - 2,
  368.                          rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOMOVE |
  369.                          SWP_NOZORDER))
  370.             {
  371.                 break;
  372.             }
  373.  
  374.             hwnd = GetDlgItem(hdlg, ID_BODY);
  375.             GetWindowRect(hwnd, &rect);
  376.             if(NULL == DeferWindowPos(hdwp, hwnd, NULL, 0, 0, cx - rect.left, cy - rect.top,
  377.                          SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER))
  378.             {
  379.                 break;
  380.             }
  381.  
  382.             EndDeferWindowPos(hdwp);
  383.         }
  384.         break;
  385.  
  386.     case WM_COMMAND:
  387.         wmId = GET_WM_COMMAND_ID(wParam, lParam);  
  388.         wmEvent = GET_WM_COMMAND_CMD(wParam, lParam);
  389.  
  390.         switch(wmId) {
  391.         case ID_BODY:
  392.         {
  393.             HMENU hMenu = GetMenu(GetParent(hdlg));
  394.             if(wmEvent == EN_KILLFOCUS)
  395.             {
  396.                 EnableMenuItem(hMenu, IDC_EDIT_CUT, MF_BYCOMMAND|MF_GRAYED);
  397.                 EnableMenuItem(hMenu, IDC_EDIT_COPY, MF_BYCOMMAND|MF_GRAYED);
  398.                 EnableMenuItem(hMenu, IDC_EDIT_PASTE, MF_BYCOMMAND|MF_GRAYED);
  399.                 EnableMenuItem(hMenu, IDC_EDIT_UNDO, MF_BYCOMMAND|MF_GRAYED);
  400.             }
  401.             else if(wmEvent == EN_SETFOCUS)
  402.             {
  403.                 EnableMenuItem(hMenu, IDC_EDIT_CUT, MF_BYCOMMAND);
  404.                 EnableMenuItem(hMenu, IDC_EDIT_COPY, MF_BYCOMMAND);
  405.                 EnableMenuItem(hMenu, IDC_EDIT_PASTE, MF_BYCOMMAND);
  406.                 EnableMenuItem(hMenu, IDC_EDIT_UNDO, MF_BYCOMMAND);
  407.             }
  408.         }
  409.         break;
  410.         default:
  411.             return FALSE;
  412.         }
  413.         
  414.     default:
  415.         return FALSE;
  416.     }
  417.  
  418.     return TRUE;
  419. }
  420.  
  421. BOOL CALLBACK
  422. AboutDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  423. {
  424.     char    rgchVersion[80];
  425.  
  426.     switch (msg)
  427.     {
  428.     case WM_INITDIALOG:
  429.         wsprintf(rgchVersion, "Version %d.%d.%d (%s)", rmj, rmm, rup,
  430.             szVerName && *szVerName ? szVerName : "BUDDY");
  431.         SetDlgItemText(hDlg, IDC_VERSION, rgchVersion);
  432.         return TRUE;
  433.  
  434.     case WM_COMMAND:
  435.         if (wParam == IDOK || wParam == IDCANCEL)
  436.         {
  437.             EndDialog (hDlg, TRUE);
  438.             return TRUE;
  439.         }
  440.         break;
  441.     }
  442.     return FALSE;
  443. }
  444.