home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / LISTHORZ / LISTHORZ.C_ / LISTHORZ.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  5.4 KB  |  205 lines

  1. /*
  2.  *
  3.  *  Window ListHScroll App
  4.  *
  5.  *  From Microsoft OnLine WinSDK Support
  6.  *
  7.  */
  8.  
  9. #include <windows.h>
  10. #include "listhorz.h"
  11.  
  12.  
  13. /*
  14.  * WinMain
  15.  *
  16.  * Purpose:
  17.  *  Main entry point of application.   Should register the app class
  18.  *  if a previous instance has not done so and do any other one-time
  19.  *  initializations.
  20.  *
  21.  * Parameters:
  22.  *  See Windows SDK Guide to Programming, page 2-3
  23.  *
  24.  * Return Value:
  25.  *  Value to return to Windows--termination code.
  26.  *
  27.  */
  28.  
  29. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  30.                     LPSTR lpszCmdLine, int nCmdShow)
  31. {
  32.     WNDCLASS    wndClass;
  33.     HWND        hWnd;
  34.     MSG         msg;
  35.  
  36.  
  37.  
  38.     if (!hPrevInstance)
  39.     {
  40.         wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  41.         wndClass.lpfnWndProc    = ListHScrollWndProc;
  42.         wndClass.cbClsExtra     = 0;
  43.         wndClass.cbWndExtra     = DLGWINDOWEXTRA;
  44.         wndClass.hInstance      = hInstance;
  45.         wndClass.hIcon          = LoadIcon(hInstance, "ListHScrollIcon");
  46.         wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  47.         wndClass.hbrBackground  = COLOR_WINDOW + 1;
  48.         wndClass.lpszMenuName   = NULL;
  49.         wndClass.lpszClassName  = "ListHScroll";
  50.  
  51.  
  52.         if (!RegisterClass(&wndClass))
  53.             return FALSE;
  54.     }
  55.  
  56.  
  57.     hWnd=CreateDialog(hInstance, "ListHScroll", 0, NULL);
  58.  
  59.     ShowWindow(hWnd, nCmdShow);
  60.     UpdateWindow(hWnd);
  61.  
  62.  
  63.     while (GetMessage(&msg, NULL, 0,0 ))
  64.     {
  65.         if (!IsDialogMessage (hWnd, &msg))
  66.         {
  67.             TranslateMessage(&msg);
  68.             DispatchMessage(&msg);
  69.         }
  70.     }
  71.  
  72.  
  73.  
  74.     return msg.wParam;
  75. }
  76.  
  77. /*
  78.  * ListHScrollWndProc
  79.  *
  80.  * Purpose:
  81.  *  Window class procedure.  Standard callback.
  82.  *
  83.  * Parameters:
  84.  *  The standard.  See Section 2.4 Windows SDK Guide to Programming,
  85.  *  page 2-4.
  86.  *
  87.  * Return Value:
  88.  *  See Parameters, above.
  89.  *
  90.  */
  91.  
  92.  
  93. long __export CALLBACK ListHScrollWndProc(HWND hWnd, UINT iMessage,
  94.                 WPARAM wParam, LPARAM lParam)
  95. {
  96.     static HFONT    hFont;
  97.     LOGFONT         lf;
  98.     static BOOL     fFirstPaint;
  99.     HWND            hList;
  100.     HWND            hEdit;
  101.     char            *pch;
  102.     WORD            cb;
  103.     WORD            iSel;
  104.     HANDLE          hMem;
  105.  
  106.     switch (iMessage)
  107.     {
  108.         case WM_CREATE:
  109.             lf.lfEscapement    = 0 ;
  110.             lf.lfOrientation   = 0 ;
  111.             lf.lfOutPrecision  = OUT_DEFAULT_PRECIS ;
  112.             lf.lfClipPrecision = CLIP_DEFAULT_PRECIS ;
  113.             lf.lfHeight        = 14;
  114.             lf.lfWidth         = 5;
  115.             lf.lfWeight        = 0;
  116.  
  117.             lf.lfItalic        = 0;
  118.             lf.lfUnderline     = 0;
  119.             lf.lfStrikeOut     = 0;
  120.  
  121.             lf.lfPitchAndFamily=FF_SWISS;
  122.  
  123.             hFont=CreateFontIndirect(&lf);
  124.             fFirstPaint=TRUE;
  125.             break;
  126.  
  127.         case WM_PAINT:
  128.             if (fFirstPaint)
  129.             {
  130.                 fFirstPaint=FALSE;
  131.                 if (!FInitListboxExtents(GetDlgItem(hWnd, ID_LISTBOX)))
  132.                 {
  133.                     iSel=MessageBox(hWnd,
  134.                             "No memory--scrolling will not work right, continue?",
  135.                             "Memory Error", MB_YESNO);
  136.  
  137.                     if (iSel==IDNO)
  138.                     {
  139.                         PostMessage(hWnd, WM_CLOSE, 0, 0L);
  140.                         return 0L;
  141.                     }
  142.                 }
  143.  
  144.                 SendDlgItemMessage(hWnd, ID_LISTBOX, WM_SETFONT, hFont, 1L);
  145.             }
  146.  
  147.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  148.  
  149.  
  150.         case WM_DESTROY:
  151.             FFreeListboxExtents(GetDlgItem(hWnd, ID_LISTBOX));
  152.             PostQuitMessage(0);
  153.             DeleteObject(hFont);
  154.             break;
  155.  
  156.         case WM_COMMAND:
  157.             hList=GetDlgItem(hWnd, ID_LISTBOX);
  158.  
  159.             switch (wParam)
  160.             {
  161.                 case ID_ADD:
  162.                     hEdit=GetDlgItem(hWnd, ID_STRINGEDIT);
  163.  
  164.                     /*
  165.                      * Allocate a buffer to copy the edit control text.
  166.                      * This must be done because EM_GETHANDLE does not
  167.                      * work with single-line controls.
  168.                      */
  169.                     cb=(WORD)(1+SendMessage(hEdit, EM_LINELENGTH, 0, 0L));
  170.  
  171.                     hMem=LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, cb+1);
  172.                     pch=(char *)LocalLock(hMem);
  173.  
  174.                     //Get the edit control text.
  175.                     SendMessage(hEdit, WM_GETTEXT, cb, (LONG)(LPSTR)pch);
  176.  
  177.                     //Update the scroll extent if necessary then add string.
  178.                     WAddExtentEntry(hList, pch);
  179.                     SendMessage(hList, LB_ADDSTRING, 0, (LONG)(LPSTR)pch);
  180.  
  181.                     LocalUnlock(hMem);
  182.                     LocalFree(hMem);
  183.                     break;
  184.  
  185.  
  186.                 case ID_DELETE:
  187.                     //Get the current selection index.
  188.                     iSel=(WORD)SendMessage(hList, LB_GETCURSEL, 0, 0L);
  189.  
  190.                     if (iSel!=LB_ERR)
  191.                     {
  192.                         //Change the extent if necessary, then delete the string.
  193.                         WRemoveExtentEntry(hList, iSel);
  194.                         SendMessage(hList, LB_DELETESTRING, iSel, 0L);
  195.                     }
  196.                     break;
  197.  
  198.             }
  199.  
  200.         default:
  201.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  202.     }
  203.  
  204.     return 0L;
  205. }