FRAME.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:16k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*--------------------------------------------------------------------------
  2.   Frame.C --- Cursors main window procedure
  3.   Description:
  4.         This sample is spread across four files, each named for the role
  5.         the contained functions play.  Each file header contains a brief
  6.         description of its purpose and the routines it contains.
  7.         FRAME.C contains those functions used to manage the main frame
  8.         window.  The frame window, created by code in MAIN.C, owns the
  9.         MDI client window, the menu bar, and other major parts of the
  10.         interface.  The functions is contains are:
  11.             AdjustMenus    - Enable/disable menu items given current state
  12.             CloseAll       - Close all child windows
  13.             CloseAllEnum   - Window enumeration callback used by CloseAll
  14.             CloseChild     - Close the currently active child window
  15.             CreateChild    - Create a new child window
  16.             DoConnect      - Connect to a host
  17.             DoDisconnect   - Disconnect from a host
  18.             DoFrameMenu    - Process a menu request
  19.             FrameProc      - Process frame window messages
  20.   This code is furnished on an as-is basis as part of the ODBC SDK and is
  21.   intended for example purposes only.
  22. --------------------------------------------------------------------------*/
  23. /* Includes --------------------------------------------------------------*/
  24. #include    "headers.h"
  25. #include   "resource.h"
  26. #include    "crsrdemo.h"
  27. // Constants ---------------------------------------------------------------
  28. #define fDISABLED   (MF_BYCOMMAND | MF_DISABLED | MF_GRAYED)
  29. #define fENABLED    (MF_BYCOMMAND | MF_ENABLED)
  30. // Prototypes --------------------------------------------------------------
  31. void INTFUNC CloseAll(HWND);
  32. void INTFUNC CloseChild(HWND);
  33. HWND INTFUNC CreateChild(HWND);
  34. BOOL INTFUNC DoConnect(HWND);
  35. void INTFUNC DoDisconnect(void);
  36. BOOL INTFUNC DoFrameMenu(HWND, WPARAM, LPARAM);
  37. BOOL CALLBACK CloseAllEnum(HWND, LPARAM);
  38. /* AdjustMenus -------------------------------------------------------------
  39.     Description: Enable/disable appropriate menu items
  40.     --------------------------------------------------------------------------*/
  41. void INTFUNC AdjustMenus(void)
  42. {
  43.    LRESULT lresult;
  44.    BOOL    fMaximized;
  45.    HWND    hwnd;
  46.    HMENU   hmenu;
  47.    UINT    idMenu;
  48.    UINT    fOption;
  49.    lresult = SendMessage(g_hwndClient, WM_MDIGETACTIVE, 0, 0L);
  50. #ifdef WIN32
  51.    hwnd       = (HWND)lresult;
  52.    fMaximized = GetWindowLong(hwnd, GWL_STYLE) & WS_MAXIMIZE;
  53. #else
  54.    hwnd       = (HWND)(LOWORD(lresult));
  55.    fMaximized = HIWORD(lresult);
  56. #endif
  57.    if( hwnd ) {
  58.       LPCHILD lpchild;
  59.       lpchild = (LPCHILD)GetWindowLong(hwnd, 0);
  60.       idMenu = IDM_FETCH + (fMaximized ? 1 : 0);
  61.       if( lpchild->fResultSetExists ) {
  62.          EnableMenuItem(GetMenu(g_hwnd), idMenu, MF_BYPOSITION |
  63.                         MF_ENABLED);
  64.          fOption = fENABLED;
  65.       }
  66.       else {
  67.          EnableMenuItem(GetMenu(g_hwnd), idMenu, MF_BYPOSITION |
  68.                         MF_DISABLED   |
  69.                         MF_GRAYED);
  70.          fOption = fDISABLED;
  71.       }
  72.       hmenu = GetSubMenu(GetMenu(g_hwnd), idMenu);
  73.       EnableMenuItem(hmenu, IDM_FETCH_FIRST, fOption);
  74.       EnableMenuItem(hmenu, IDM_FETCH_PRIOR,  fOption);
  75.       EnableMenuItem(hmenu, IDM_FETCH_NEXT,   fOption);
  76.       EnableMenuItem(hmenu, IDM_FETCH_LAST,   fOption);
  77.       EnableMenuItem(hmenu, IDM_FETCH_ABSOLUTE, fOption);
  78.       EnableMenuItem(hmenu, IDM_FETCH_RELATIVE, fOption);
  79. #if 0
  80.       EnableMenuItem(hmenu, IDM_FETCH_RESUME, fOption);
  81. #endif  //  0
  82.       if( lpchild->dwOperation != OPER_SELECT ) {
  83.          EnableMenuItem(hmenu, (UINT)IDM_FETCH_DELETEROW, fDISABLED);
  84.          EnableMenuItem(hmenu, (UINT)IDM_FETCH_UPDATEROW, fDISABLED);
  85.       }
  86.       else {
  87.          EnableMenuItem(hmenu, (UINT)IDM_FETCH_DELETEROW, fOption);
  88.          EnableMenuItem(hmenu, (UINT)IDM_FETCH_UPDATEROW, fOption);
  89.       }
  90.       idMenu = IDM_FETCHCHILD + (fMaximized ? 1 : 0);
  91.       if( g_fConnected ) {
  92.          EnableMenuItem(GetMenu(g_hwnd), idMenu, MF_BYPOSITION |
  93.                         MF_ENABLED);
  94.          fOption = fENABLED;
  95.       }
  96.       else {
  97.          EnableMenuItem(GetMenu(g_hwnd), idMenu, MF_BYPOSITION  |
  98.                         MF_DISABLED    |
  99.                         MF_GRAYED);
  100.          fOption = fDISABLED;
  101.       }
  102.    }
  103.    else {
  104.       UINT    wCount;
  105.       
  106.       hmenu = GetMenu(g_hwnd);
  107.       wCount = GetMenuItemCount(hmenu);
  108.       for( idMenu = 0; idMenu < wCount; idMenu++ )
  109.          EnableMenuItem(hmenu, idMenu, MF_BYPOSITION | MF_ENABLED);
  110.    }
  111.    DrawMenuBar(g_hwnd);
  112.    return;
  113. }
  114. /* CloseAll ----------------------------------------------------------------
  115.     Description: Close all child windows
  116.     --------------------------------------------------------------------------*/
  117. void INTFUNC CloseAll(HWND hwnd)
  118. {
  119.    UNREF_PARAM(hwnd);
  120.    if (!g_fConnected)
  121.       return;
  122.    EnumChildWindows(g_hwndClient, CloseAllEnum, 0L);
  123.    return;
  124. }
  125. /* CloseAllEnum ------------------------------------------------------------
  126.     Description: EnumChildWindows procedure, it calls CloseChild for each
  127.                  child window passed
  128.                  --------------------------------------------------------------------------*/
  129. BOOL CALLBACK CloseAllEnum(HWND hwnd, LPARAM lparam)
  130. {
  131.    UNREF_PARAM(lparam);
  132.    if (!GetWindow(hwnd, GW_OWNER))
  133.       CloseChild(hwnd);
  134.    return TRUE;
  135. }
  136. /* CloseChild --------------------------------------------------------------
  137.     Description: Close a child window
  138.     --------------------------------------------------------------------------*/
  139. void INTFUNC CloseChild(HWND hwnd)
  140. {
  141.    // Destroy child window
  142.    FORWARD_WM_MDIDESTROY(g_hwndClient, hwnd, SendMessage);
  143.    // Decrement child count
  144.    g_cChild--;
  145.    // Set appropriate menu bar
  146.    if (!g_cChild)
  147. #ifdef WIN32
  148.       SendMessage(g_hwndClient,WM_MDISETMENU,(WPARAM)g_hmenuFrame,(LPARAM)g_hmenuFrameWindow);
  149. #else
  150.    FORWARD_WM_MDISETMENU(g_hwndClient,
  151.                          0, g_hmenuFrame, g_hmenuFrameWindow, SendMessage);
  152. #endif
  153.    return;
  154. }
  155. /* CreateChild -------------------------------------------------------------
  156.     Description: Create a child window
  157.     --------------------------------------------------------------------------*/
  158. HWND INTFUNC CreateChild(HWND hwnd)
  159. {
  160.    HWND            hwndChild;
  161.    MDICREATESTRUCT mdi;
  162.    int             x, y, cx, cy;
  163.    // Have child fill client area if it is the only child window
  164.    if (!g_cChild) {
  165.       RECT    rc;
  166.       GetClientRect(hwnd, &rc);
  167.       x  = rc.left;
  168.       y  = rc.top;
  169.       cx = rc.right - rc.left;
  170.       cy = rc.bottom - rc.top;
  171.    }
  172.    // Otherwise, accept default placement
  173.    else {
  174.       x  =
  175.          y  =
  176.             cx =
  177.                cy = CW_USEDEFAULT;
  178.    }
  179.    // Increment child count and cursor number
  180.    g_cChild++;
  181.    g_cCursor++;
  182.    // Create child via MDI interface
  183.    mdi.szClass = szCHILDCLASS;
  184.    mdi.szTitle = g_szDSN;
  185.    mdi.hOwner  = g_hinst;
  186.    mdi.x       = x;
  187.    mdi.y       = y;
  188.    mdi.cx      = cx;
  189.    mdi.cy      = cy;
  190.    mdi.style   = 0;
  191.    mdi.lParam  = (LPARAM)NULL;
  192.    hwndChild = FORWARD_WM_MDICREATE(g_hwndClient, ((LPSTR)&mdi), SendMessage);
  193.    if (hwndChild) {
  194.       if (g_cChild == 1)
  195. #ifdef WIN32
  196.          SendMessage(g_hwndClient,WM_MDISETMENU,(WPARAM)g_hmenuChild,(LPARAM)g_hmenuChildWindow);
  197. #else
  198.       FORWARD_WM_MDISETMENU(g_hwndClient,
  199.                             0, g_hmenuChild, g_hmenuChildWindow, SendMessage);
  200. #endif
  201.    }
  202.    else
  203.       g_cChild--;
  204.    return hwndChild;
  205. }
  206. /* DoConnect ---------------------------------------------------------------
  207.     Description: Connect to a data source
  208.     --------------------------------------------------------------------------*/
  209. BOOL INTFUNC DoConnect(HWND hwnd)
  210. {
  211.    HCURSOR   hcur;
  212.    char      sz[cbMAXSQL];
  213.    SWORD     cb;
  214.    SQLRETURN rc;
  215.    // If already connected, close all children and disconnect
  216.    if (g_fConnected) {
  217.       LoadString(g_hinst, IDS_CONWARN, sz, sizeof(sz));
  218.       rc = MessageBox(hwnd,
  219.                       sz, g_szTITLE,
  220.                       MB_ICONQUESTION | MB_OKCANCEL | MB_DEFBUTTON2);
  221.       if (rc == IDCANCEL)
  222.          return FALSE;
  223.       CloseAll(hwnd);
  224.       DoDisconnect();
  225.    }
  226.    hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
  227.    // Call SQLDriverConnect
  228.    rc = SQLDriverConnect(g_hdbc, g_hwnd, NULL, 0,
  229.                          (UCHAR FAR *)sz, sizeof(sz), &cb, SQL_DRIVER_COMPLETE);
  230.    // If successfully connected, get data source attributes
  231.    if (SUCCESS(rc)) {
  232.       LPSTR    lpszS, lpszD;
  233.       SQLHSTMT hstmt;
  234.       // Mark as connected
  235.       g_fConnected = TRUE;
  236.       // Extract and save data source name
  237.       lpszS = _fstrstr(sz, szDSNKEY);
  238.       if (lpszS) {
  239.          lpszS = _fstrstr(sz, szDSNKEY) + lstrlen(szDSNKEY);
  240.          lpszD = g_szDSN;
  241.          while (*lpszS && *lpszS != ';') *lpszD++ = *lpszS++;
  242.          *lpszD = '';
  243.       }
  244.       else
  245.          LoadString(g_hinst, IDS_NODSN, g_szDSN, sizeof(g_szDSN));
  246.       // Change to the appropriate menu bar
  247. #ifdef WIN32
  248.       SendMessage(g_hwndClient,WM_MDISETMENU,(WPARAM)g_hmenuFrame,(LPARAM)g_hmenuFrameWindow);
  249. #else
  250.       FORWARD_WM_MDISETMENU(g_hwndClient,
  251.                             0, g_hmenuFrame, g_hmenuFrameWindow, SendMessage);
  252. #endif
  253.       // Get maximum column name length
  254.       if (DBCError(hwnd, SQLGetInfo(g_hdbc, SQL_MAX_COLUMN_NAME_LEN,
  255.                                     &g_cbName, sizeof(g_cbName), NULL)))
  256.          g_cbName = 32;
  257.       // Get identifier quote character
  258.       if (DBCError(hwnd, SQLGetInfo(g_hdbc, SQL_IDENTIFIER_QUOTE_CHAR,
  259.                                     g_szQuoteChar, sizeof(g_szQuoteChar), NULL)))
  260.          *g_szQuoteChar = ' ';
  261.       // Determine if async support is available
  262.       rc = SQLAllocHandle(SQL_HANDLE_STMT,g_hdbc, &hstmt);
  263.       if (!SUCCESS(rc))
  264.          g_fAsyncSupported = FALSE;
  265.       else {
  266.          g_fAsyncSupported = SUCCESS(SQLSetStmtAttr(hstmt,
  267.                                                     SQL_ATTR_ASYNC_ENABLE,
  268.                                                     (SQLPOINTER) 1,
  269.                                                     SQL_IS_INTEGER));
  270.          SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
  271.       }
  272.    }
  273.    else if (rc != SQL_NO_DATA)
  274.       DBCError(hwnd, rc);
  275.    SetCursor(hcur);
  276.    return SUCCESS(rc);
  277. }
  278. /* DoDisconnect ------------------------------------------------------------
  279.     Description: Drop ODBC connection
  280.     --------------------------------------------------------------------------*/
  281. void INTFUNC DoDisconnect(void)
  282. {
  283.    // Change to the appropriate menu bar
  284. #ifdef WIN32
  285.    SendMessage(g_hwndClient,WM_MDISETMENU,(WPARAM)g_hmenuInit,(LPARAM)g_hmenuInitWindow);
  286. #else
  287.    FORWARD_WM_MDISETMENU(g_hwndClient,
  288.                          0, g_hmenuInit, g_hmenuInitWindow, SendMessage);
  289. #endif
  290.    // If not fully connected, return immediately
  291.    if (!g_fConnected)
  292.       return;
  293.    // Disconnect with ODBC
  294.    SQLDisconnect(g_hdbc);
  295.    // Reset connection related variables
  296.    g_cbName     = 0;
  297.    g_fConnected = FALSE;
  298.    g_szDSN[0]   = '';
  299.    g_cChild     = 0;
  300.    g_cCursor    = 0;
  301.    return;
  302. }
  303. /* DoFrameMenu -------------------------------------------------------------
  304.     Description: Respond to a request from the frame window menu
  305.     --------------------------------------------------------------------------*/
  306. BOOL INTFUNC DoFrameMenu(HWND  hwnd, WPARAM  wparam, LPARAM  lparam)
  307. {
  308.    HWND    hwndChild;
  309.    hwndChild = FORWARD_WM_MDIGETACTIVE(g_hwndClient, SendMessage);
  310.    switch (GET_WM_COMMAND_ID(wparam, lparam)) {
  311.      case IDM_STMT_ADDDSN:
  312.       SQLCreateDataSource(hwnd, NULL);
  313.       break;
  314.      case IDM_STMT_DISCONNECT:
  315.       if (g_cChild)
  316.          CloseAll(hwnd);
  317.       DoDisconnect();
  318.       break;
  319.      case IDM_STMT_CONNECT:
  320.       if (!DoConnect(hwnd))
  321.          break;
  322.      case IDM_STMT_NEW:
  323.       hwndChild = CreateChild(hwnd);
  324.       break;
  325.      case IDM_STMT_CLOSE:
  326.       CloseChild(hwndChild);
  327.       break;
  328.      case IDM_STMT_CLOSEALL:
  329.       CloseAll(hwnd);
  330.       break;
  331.      case IDM_STMT_EXIT:
  332.       PostMessage(hwnd, WM_CLOSE, 0, 0L);
  333.       break;
  334.      case IDM_WINDOW_ARRANGE:
  335.       FORWARD_WM_MDIICONARRANGE(g_hwndClient, SendMessage);
  336.       break;
  337.      case IDM_WINDOW_CASCADE:
  338.       FORWARD_WM_MDICASCADE(g_hwndClient, 0, SendMessage);
  339.       break;
  340.      case IDM_WINDOW_TILEH:
  341.      case IDM_WINDOW_TILEV: {
  342.         WPARAM  fTile;
  343.         fTile = (GET_WM_COMMAND_ID(wparam, lparam) == IDM_WINDOW_TILEH
  344.                  ? MDITILE_HORIZONTAL
  345.                  : MDITILE_VERTICAL);
  346.         FORWARD_WM_MDITILE(g_hwndClient, fTile, SendMessage);
  347.         break;
  348.      }
  349.      case IDM_HELP_ABOUT:
  350.       DoDialog(g_hwnd, IDD_ABOUTBOX, AboutDlgProc);
  351.       break;
  352.   
  353.      case IDM_HELP_HELP:
  354.       WinHelp(g_hwnd, szHELPFILE, HELP_KEY ,(DWORD)(LPTSTR)szKeyword );
  355. break;
  356.      default:
  357.       // Pass unrecognized request to the current child
  358.       SendMessage(hwndChild, WM_COMMAND, wparam, lparam);
  359.       return FALSE;
  360.    }
  361.    // Adjust menu state
  362.    AdjustMenus();
  363.    return TRUE;
  364. }
  365. /* FrameProc ---------------------------------------------------------------
  366.     Description: Frame window procedure
  367.     --------------------------------------------------------------------------*/
  368. LRESULT EXPFUNC FrameProc(HWND    hwnd,
  369.                           UINT   msg,
  370.                           WPARAM wparam,
  371.                           LPARAM lparam)
  372. {
  373.    switch (msg) {
  374.       // Create MDI client window
  375.      case WM_CREATE: {
  376.         CLIENTCREATESTRUCT  client;
  377.         client.hWindowMenu  = g_hmenuInitWindow;
  378.         client.idFirstChild = IDM_FIRSTCHILD;
  379.         g_hwndClient = CreateWindow(szMDICLIENT, NULL,
  380.                                     WS_CHILD        |
  381.                                     WS_CLIPCHILDREN |
  382.                                     WS_VISIBLE,
  383.                                     0, 0, 0, 0, hwnd, (HMENU)1, g_hinst,
  384.                                     &client);
  385.         break;
  386.      }
  387.       // Refresh brushes when colors change
  388.      case WM_SYSCOLORCHANGE:
  389.       if (g_hbrWin)    DeleteObject(g_hbrWin);
  390.       if (g_hbrBtn)    DeleteObject(g_hbrBtn);
  391.       if (g_hbrScroll) DeleteObject(g_hbrScroll);
  392.       g_hbrWin    = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  393.       g_hbrBtn    = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  394.       g_hbrScroll = CreateSolidBrush(GetSysColor(COLOR_SCROLLBAR));
  395.       break;
  396.       // Close all children, drop connection, and close frame window
  397.      case WM_CLOSE:
  398.       CloseAll(hwnd);
  399.       DoDisconnect();
  400.       DestroyWindow(hwnd);
  401.       break;
  402.       // Destroy menus not attached to the window and drop ODBC handles
  403.      case WM_DESTROY: {
  404.         HMENU   hmenu;
  405.         hmenu = GetMenu(hwnd);
  406.         if (g_hmenuInit  != hmenu) DestroyMenu(g_hmenuInit);
  407.         if (g_hmenuFrame != hmenu) DestroyMenu(g_hmenuFrame);
  408.         if (g_hmenuChild != hmenu) DestroyMenu(g_hmenuChild);
  409.         if (g_hdbc)
  410.            SQLFreeHandle(SQL_HANDLE_DBC,g_hdbc);
  411.         if (g_henv)
  412.            SQLFreeHandle(SQL_HANDLE_ENV,g_henv);
  413.         g_hwnd = NULL;
  414.         WinHelp(hwnd, szHELPFILE, HELP_QUIT, 0L);
  415.         PostQuitMessage(0);
  416.         break;
  417.      }
  418.       // Pass menu commands to handler routine
  419.      case WM_COMMAND:
  420.       if (DoFrameMenu(hwnd, wparam, lparam))
  421.          break;
  422.       // All other requests go to default MDI frame procedure
  423.      default:
  424.       return DefFrameProc(hwnd, g_hwndClient, msg, wparam, lparam);
  425.    }
  426.    return (LRESULT)NULL;
  427. }