home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / oledb / tablecopy / winmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-12  |  13.7 KB  |  469 lines

  1. //-----------------------------------------------------------------------------
  2. // Microsoft OLE DB TABLECOPY Sample
  3. // Copyright (C) 1995-1998 Microsoft Corporation
  4. //
  5. // @doc
  6. //
  7. // @module WINMAIN.CPP
  8. //
  9. //-----------------------------------------------------------------------------
  10.  
  11.  
  12. ///////////////////////////////////////////////////////////////////////
  13. // Includes
  14. //
  15. ///////////////////////////////////////////////////////////////////////
  16. #include "winmain.h"
  17. #include "wizard.h"
  18. #include "common.h"
  19. #include "tablecopy.h"
  20. #include "table.h"
  21. #include "spy.h"
  22.  
  23. //////////////////////////////////////////////////////////////////
  24. // int WINAPI WinMain
  25. //
  26. // Main application entry point
  27. //////////////////////////////////////////////////////////////////
  28. int WINAPI WinMain(    HINSTANCE hInstance,
  29.                     HINSTANCE hPrevInstance,
  30.                     CHAR*  pszCmdLine,
  31.                     INT    nCmdShow)
  32. {
  33.     HRESULT hr = E_FAIL;
  34.     CMallocSpy* pCMallocSpy = NULL; 
  35.     CWizard* pCWizard = NULL;
  36.  
  37.     //Register IMallocSpy
  38.     XTESTC(hr = MallocSpyRegister(&pCMallocSpy)); 
  39.     
  40.     pCWizard = new CWizard(NULL, hInstance);
  41.  
  42.     //CoInitialize - COM/OLE
  43.     XTESTC(hr = CoInitialize(NULL));
  44.     
  45.     //Initlialize Window Controls
  46.     InitCommonControls();
  47.     
  48.     //Main Execution
  49.     pCWizard->Display();
  50.  
  51. CLEANUP:
  52.     delete pCWizard;
  53.     MallocSpyUnRegister(pCMallocSpy);
  54.     CoUninitialize();
  55.     
  56.     //Dump all the leaks
  57.     MallocSpyDump(pCMallocSpy);
  58.     delete pCMallocSpy;
  59.  
  60.     return (hr==S_OK);
  61. }
  62.  
  63.  
  64. //////////////////////////////////////////////////////////////////
  65. // void SyncSibling
  66. //
  67. //////////////////////////////////////////////////////////////////
  68. void SyncSibling(HWND hToWnd, HWND hFromWnd)
  69. {
  70.     ASSERT(hToWnd && hFromWnd);
  71.  
  72.     //Make both windows synched, 
  73.     //Get the current selection from the Source
  74.     LONG iItem = SendMessage(hFromWnd, LVM_GETNEXTITEM, (WPARAM)-1, (LPARAM)LVNI_SELECTED);
  75.     
  76.     //Tell the Target to select the same selection
  77.     if(iItem != LVM_ERR)
  78.     {
  79.         //Get the current selection from the Target and Unselect it
  80.         LONG iOldItem = SendMessage(hToWnd, LVM_GETNEXTITEM, (WPARAM)-1, (LPARAM)LVNI_SELECTED);
  81.         if(iItem != iOldItem)
  82.         {
  83.             //Unselect previous one
  84.             LV_SetItemState(hToWnd, iOldItem, 0, 0, LVIS_SELECTED);
  85.  
  86.             //Select the new one
  87.             LV_SetItemState(hToWnd, iItem, 0, LVIS_SELECTED, LVNI_SELECTED);
  88.  
  89.             //Ensure that it is visible
  90.             SendMessage(hToWnd, LVM_ENSUREVISIBLE, (WPARAM)iItem, (LPARAM)FALSE);
  91.         }
  92.     }
  93. }                
  94.  
  95.  
  96. //////////////////////////////////////////////////////////////////
  97. // int InternalAssert
  98. //
  99. //////////////////////////////////////////////////////////////////
  100. int InternalAssert(                    // 1 to break, 0 to skip.
  101.     char*    pszExp,                    // The expression causing assert
  102.     char*    pszFile,                // The file name
  103.     UINT    iLine                    // Line number of assert
  104.     )
  105. {
  106.     CHAR    szMsg[MAX_QUERY_LEN];
  107.     _snprintf(szMsg, MAX_NAME_LEN, "Assertion Error!\n File '%s', Line '%lu'\n"
  108.                     "Expression '%s'\n\n"
  109.                     "Do you wish to Continue?  (Press 'OK' to ignore the assertion."
  110.                     "  Press 'Cancel to debug.)",pszFile, iLine, pszExp);
  111.     
  112.     //Popup a MessageBox
  113.     LONG dwSelection = MessageBoxA(NULL, szMsg,    "Microsoft OLE DB TableCopy - Error",
  114.         MB_TASKMODAL | MB_ICONSTOP | MB_OKCANCEL | MB_DEFBUTTON1 );
  115.  
  116.     switch(dwSelection)
  117.     {
  118.         case IDOK:
  119.             return 0;
  120.         case IDCANCEL:
  121.             return 1;
  122.         default:
  123.             ASSERT(!L"Unhandled Choice");
  124.     }
  125.  
  126.     return 0;
  127. }
  128.  
  129.  
  130. //////////////////////////////////////////////////////////////////
  131. // void InternalTrace
  132. //
  133. //////////////////////////////////////////////////////////////////
  134. void InternalTrace(WCHAR*    pwszFmt, ...)
  135. {
  136.     va_list        marker;
  137.     WCHAR        wszBuffer[MAX_NAME_LEN];
  138.     CHAR        szBuffer[MAX_NAME_LEN];
  139.  
  140.     // Use format and arguements as input
  141.     //This version will not overwrite the stack, since it only copies
  142.     //upto the max size of the array
  143.     va_start(marker, pwszFmt);
  144.     _vsnwprintf(wszBuffer, MAX_NAME_LEN, pwszFmt, marker);
  145.     va_end(marker);
  146.  
  147.     //Make sure there is a NULL Terminator, vsnwprintf will not copy
  148.     //the terminator if length==MAX_NAME_LEN
  149.     wszBuffer[MAX_NAME_LEN-1] = EOL;
  150.     
  151.     //Convert to MBCS
  152.     ConvertToMBCS(wszBuffer, szBuffer, MAX_NAME_LEN);
  153.     
  154.     //Output to the DebugWindow
  155.     OutputDebugString(szBuffer);
  156. }
  157.  
  158.  
  159. //////////////////////////////////////////////////////////////////
  160. // void InternalTrace
  161. //
  162. //////////////////////////////////////////////////////////////////
  163. void InternalTrace(CHAR*    pszFmt, ...)
  164. {
  165.     va_list        marker;
  166.     CHAR        szBuffer[MAX_NAME_LEN];
  167.  
  168.     // Use format and arguements as input
  169.     //This version will not overwrite the stack, since it only copies
  170.     //upto the max size of the array
  171.     va_start(marker, pszFmt);
  172.     _vsnprintf(szBuffer, MAX_NAME_LEN, pszFmt, marker);
  173.     va_end(marker);
  174.  
  175.     //Make sure there is a NULL Terminator, vsnwprintf will not copy
  176.     //the terminator if length==MAX_NAME_LEN
  177.     szBuffer[MAX_NAME_LEN-1] = '\0';
  178.     
  179.     OutputDebugStringA(szBuffer);
  180. }
  181.  
  182.  
  183. //////////////////////////////////////////////////////////////////
  184. // void Busy
  185. //
  186. //////////////////////////////////////////////////////////////////
  187. void Busy(BOOL bValue)
  188. {
  189.     static HCURSOR    hWaitCursor = LoadCursor(NULL, IDC_WAIT);
  190.  
  191.     if(bValue) 
  192.         SetCursor(hWaitCursor);
  193.     else 
  194.         SetCursor(NULL);
  195. }
  196.  
  197.  
  198. //////////////////////////////////////////////////////////////////
  199. // void OutOfMemory
  200. //
  201. //////////////////////////////////////////////////////////////////
  202. void OutOfMemory(HWND hWnd)
  203. {
  204.     //Unicode version is supported on Win95/WinNT
  205.     MessageBoxW(hWnd, L"Out of memory", wsz_ERROR, MB_TASKMODAL | MB_OK);
  206. }
  207.  
  208.  
  209. //////////////////////////////////////////////////////////////////
  210. // BOOL CenterDialog
  211. //
  212. //////////////////////////////////////////////////////////////////
  213. BOOL CenterDialog(HWND hdlg)
  214. {
  215.     RECT  rcParent;                         // Parent window client rect
  216.     RECT  rcDlg;                            // Dialog window rect
  217.     int   nLeft, nTop;                      // Top-left coordinates
  218.     int   cWidth, cHeight;                  // Width and height
  219.     HWND    hwnd;
  220.  
  221.     // Get frame window client rect in screen coordinates
  222.     hwnd = GetParent(hdlg);
  223.     if(hwnd == NULL || hwnd == GetDesktopWindow()) 
  224.     {
  225.         rcParent.top = rcParent.left = 0;
  226.         rcParent.right = GetSystemMetrics(SM_CXFULLSCREEN);
  227.         rcParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
  228.     }
  229.     else 
  230.         GetWindowRect(hwnd, &rcParent);
  231.  
  232.     // Determine the top-left point for the dialog to be centered
  233.     GetWindowRect(hdlg, &rcDlg);
  234.     cWidth  = rcDlg.right  - rcDlg.left;
  235.     cHeight = rcDlg.bottom - rcDlg.top;
  236.     nLeft   = rcParent.left + 
  237.             (((rcParent.right  - rcParent.left) - cWidth ) / 2);
  238.     nTop    = rcParent.top  +
  239.             (((rcParent.bottom - rcParent.top ) - cHeight) / 2);
  240.     if (nLeft < 0) nLeft = 0;
  241.     if (nTop  < 0) nTop  = 0;
  242.  
  243.     // Place the dialog
  244.     return MoveWindow(hdlg, nLeft, nTop, cWidth, cHeight, TRUE);
  245. }
  246.  
  247.  
  248. //////////////////////////////////////////////////////////////////
  249. // ULONG wMessageBox
  250. //
  251. //////////////////////////////////////////////////////////////////
  252. INT wMessageBox(
  253.     HWND hwnd,                            // Parent window for message display
  254.     UINT uiStyle,                        // Style of message box
  255.     WCHAR* pwszTitle,                    // Title for message
  256.     WCHAR* pwszFmt,                        // Format string
  257.     ...                                    // Substitution parameters
  258.     )
  259. {
  260.     va_list        marker;
  261.     WCHAR        wszBuffer[MAX_QUERY_LEN];
  262.  
  263.     // Use format and arguements as input
  264.     //This version will not overwrite the stack, since it only copies
  265.     //upto the max size of the array
  266.     va_start(marker, pwszFmt);
  267.     _vsnwprintf(wszBuffer, MAX_QUERY_LEN, pwszFmt, marker);
  268.     va_end(marker);
  269.    
  270.     //Make sure there is a NULL Terminator, vsnwprintf will not copy
  271.     //the terminator if length==MAX_QUERY_LEN
  272.     wszBuffer[MAX_QUERY_LEN-1] = EOL;
  273.  
  274.     //Unicode version is supported on both Win95 / WinNT do need to convert
  275.     return MessageBoxW(hwnd, wszBuffer, pwszTitle, uiStyle);
  276. }
  277.  
  278.  
  279.  
  280.  
  281. //////////////////////////////////////////////////////////////////
  282. // void wSetDlgItemText
  283. //
  284. //////////////////////////////////////////////////////////////////
  285. void wSetDlgItemText(HWND hWnd, INT DlgItem, WCHAR* pwszFmt, ...)
  286. {
  287.     va_list        marker;
  288.     WCHAR        wszBuffer[MAX_NAME_LEN];
  289.     CHAR        szBuffer[MAX_NAME_LEN];
  290.  
  291.     // Use format and arguements as input
  292.     //This version will not overwrite the stack, since it only copies
  293.     //upto the max size of the array
  294.     va_start(marker, pwszFmt);
  295.     _vsnwprintf(wszBuffer, MAX_NAME_LEN, pwszFmt, marker);
  296.     va_end(marker);
  297.  
  298.     //Make sure there is a NULL Terminator, vsnwprintf will not copy
  299.     //the terminator if length==MAX_NAME_LEN
  300.     wszBuffer[MAX_NAME_LEN-1] = EOL;
  301.  
  302.     //convert to MBCS
  303.     ConvertToMBCS(wszBuffer, szBuffer, MAX_NAME_LEN);
  304.     
  305.     SetDlgItemTextA(hWnd, DlgItem, szBuffer);
  306. }
  307.  
  308.  
  309. //////////////////////////////////////////////////////////////////
  310. // UINT wGetDlgItemText
  311. //
  312. //////////////////////////////////////////////////////////////////
  313. UINT wGetDlgItemText(HWND hWnd, INT DlgItem, WCHAR* pwsz, INT nMaxSize)
  314. {
  315.     ASSERT(pwsz);
  316.     CHAR szBuffer[MAX_NAME_LEN];
  317.  
  318.     UINT iReturn = GetDlgItemTextA(hWnd, DlgItem, szBuffer, MAX_NAME_LEN);
  319.  
  320.     //convert to WCHAR
  321.     ConvertToWCHAR(szBuffer, pwsz, MAX_NAME_LEN);
  322.     return iReturn;
  323. }
  324.  
  325.  
  326.  
  327. //////////////////////////////////////////////////////////////////
  328. // LRESULT wSendMessage
  329. //
  330. //////////////////////////////////////////////////////////////////
  331. LRESULT wSendMessage(HWND hWnd, UINT Msg, WPARAM wParam, WCHAR* pwszBuffer)
  332. {
  333.     CHAR szBuffer[MAX_NAME_LEN];                          
  334.     szBuffer[0] = '\0';
  335.     
  336.     if(pwszBuffer && Msg != WM_GETTEXT && Msg != LVM_GETITEM && Msg != CB_GETLBTEXT)
  337.     {
  338.         //Convert to ANSI before sending, since we don't know if this was a GET/SET message
  339.         ConvertToMBCS(pwszBuffer, szBuffer, MAX_NAME_LEN);
  340.     }
  341.  
  342.     //Send the message with an ANSI Buffer 
  343.     LRESULT lResult = SendMessageA(hWnd, Msg, (WPARAM)wParam, (LPARAM)szBuffer);
  344.  
  345.     if(pwszBuffer && Msg == WM_GETTEXT || Msg == LVM_GETITEM || Msg == CB_GETLBTEXT)
  346.     {
  347.         //Now convert the result into the users WCHAR buffer
  348.         ConvertToWCHAR(szBuffer, pwszBuffer, MAX_NAME_LEN);
  349.     }
  350.     return lResult;
  351. }
  352.  
  353.  
  354. //////////////////////////////////////////////////////////////////
  355. // BOOL GetEditBoxValue
  356. //
  357. //////////////////////////////////////////////////////////////////
  358. BOOL GetEditBoxValue(HWND hEditWnd, ULONG ulMin, ULONG ulMax, ULONG* pulCount)
  359. {
  360.     ASSERT(hEditWnd);
  361.     ASSERT(pulCount);
  362.  
  363.     ULONG    ulCount = 0;
  364.     WCHAR    wszBuffer[MAX_NAME_LEN];
  365.     WCHAR*  pwszEnd = NULL;
  366.     
  367.     //Get the EditText
  368.     wSendMessage(hEditWnd, WM_GETTEXT, MAX_NAME_LEN, wszBuffer);
  369.         
  370.     //Convert to ULONG
  371.     ulCount = wcstoul(wszBuffer, &pwszEnd, 10);
  372.     if(!wszBuffer[0] || ulCount<ulMin || ulCount>ulMax || pwszEnd==NULL || pwszEnd[0]!=EOL) 
  373.     {
  374.         wMessageBox(hEditWnd, MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK,     wsz_ERROR, 
  375.             wsz_INVALID_VALUE_, wszBuffer, ulMin, ulMax);
  376.         SetFocus(hEditWnd);
  377.         return FALSE;
  378.     }
  379.  
  380.     *pulCount = ulCount;
  381.     return TRUE;
  382. }
  383.  
  384.  
  385. //////////////////////////////////////////////////////////////////
  386. // BOOL LV_InsertColumn
  387. //
  388. //////////////////////////////////////////////////////////////////
  389. LONG LV_InsertColumn(HWND hWnd, LONG iColumn, CHAR* szName)
  390. {
  391.     //Setup LV_COLUMNINFO
  392.     LV_COLUMN lvColumnHeader = { LVCF_TEXT | LVCF_FMT | LVCF_SUBITEM, LVCFMT_LEFT, 0, szName, MAX_NAME_LEN, 0};
  393.     
  394.     //LVM_INSERTCOLUMN
  395.     return SendMessage(hWnd, LVM_INSERTCOLUMN, (WPARAM)iColumn, (LPARAM)&lvColumnHeader);
  396. }
  397.  
  398.  
  399. //////////////////////////////////////////////////////////////////
  400. // BOOL LV_InsertItem
  401. //
  402. //////////////////////////////////////////////////////////////////
  403. LONG LV_InsertItem(HWND hWnd, LONG iItem, LONG iSubItem, CHAR* szName, LONG iParam, LONG iImage)
  404. {
  405.     //LVM_INSERTITEM
  406.     if(iSubItem==0)
  407.     {
  408.         LV_ITEM lvItem = { LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM, iItem, iSubItem, 0, 0, szName, 0, iImage, iParam};
  409.         return SendMessage(hWnd, LVM_INSERTITEM, (WPARAM)0, (LPARAM)&lvItem);
  410.     }
  411.     //LVM_SETITEM
  412.     else
  413.     {
  414.         LV_ITEM lvItem = { LVIF_TEXT, iItem, iSubItem, 0, 0, szName, 0, 0, iParam};
  415.         return SendMessage(hWnd, LVM_SETITEM, (WPARAM)0, (LPARAM)&lvItem);
  416.     }
  417. }
  418.  
  419.  
  420. //////////////////////////////////////////////////////////////////
  421. // BOOL LV_SetItemState
  422. //
  423. //////////////////////////////////////////////////////////////////
  424. LONG LV_SetItemState(HWND hWnd, LONG iItem, LONG iSubItem, LONG lState, LONG lStateMask)
  425. {
  426.     //LVM_SETITEM
  427.     LV_ITEM lvItem = { LVIF_STATE, iItem, iSubItem, lState, lStateMask, NULL, 0, 0, 0};
  428.     return SendMessage(hWnd, LVM_SETITEMSTATE, (WPARAM)iItem, (LPARAM)&lvItem);
  429. }
  430.  
  431.  
  432. //////////////////////////////////////////////////////////////////
  433. // BOOL LV_SetItemText
  434. //
  435. //////////////////////////////////////////////////////////////////
  436. LONG LV_SetItemText(HWND hWnd, LONG iItem, LONG iSubItem, CHAR* szName)
  437. {
  438.     //LVM_SETITEM
  439.     LV_ITEM lvItem = { LVIF_TEXT, iItem, iSubItem, 0, 0, szName, 0, 0, 0};
  440.     return SendMessage(hWnd, LVM_SETITEMTEXT, (WPARAM)iItem, (LPARAM)&lvItem);
  441. }
  442.  
  443.  
  444. //////////////////////////////////////////////////////////////////
  445. // BOOL LV_FindItem
  446. //
  447. //////////////////////////////////////////////////////////////////
  448. LONG LV_FindItem(HWND hWnd, CHAR* szName, LONG iStart)
  449. {
  450.     //LVM_FINDITEM
  451.     LV_FINDINFO lvFindInfo = { LVFI_STRING, szName, 0, 0, 0};
  452.     return SendMessage(hWnd, LVM_FINDITEM, (WPARAM)iStart, (LPARAM)&lvFindInfo);
  453. }
  454.  
  455.  
  456. //////////////////////////////////////////////////////////////////
  457. // BOOL TV_InsertItem
  458. //
  459. //////////////////////////////////////////////////////////////////
  460. HTREEITEM TV_InsertItem(HWND hWnd, HTREEITEM hParent, HTREEITEM hInsAfter, CHAR* szName, LONG iParam, LONG iImage, LONG iSelectedImage)
  461. {
  462.     TV_INSERTSTRUCT tvInsertStruct = { hParent, hInsAfter, { TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, 0, 0, 0, szName, 0, iImage, iSelectedImage, 0, iParam} };
  463.     
  464.     //TVM_INSERTITEM
  465.     return (HTREEITEM)SendMessage(hWnd, TVM_INSERTITEM, (WPARAM)0, (LPARAM)&tvInsertStruct);
  466. }
  467.  
  468.  
  469.