home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / mswindo / programm / misc / 4584 < prev    next >
Encoding:
Text File  |  1993-01-04  |  15.7 KB  |  452 lines

  1. Path: sparky!uunet!spool.mu.edu!yale.edu!jvnc.net!netnews.upenn.edu!prijat!jaguar.uofs.edu!das11
  2. From: das11@jaguar.uofs.edu
  3. Newsgroups: comp.os.ms-windows.programmer.misc
  4. Subject: Entryfield Border
  5. Message-ID: <1993Jan4.164144.1@jaguar.uofs.edu>
  6. Date: 4 Jan 93 21:41:44 GMT
  7. Sender: news@prijat.cs.uofs.edu
  8. Organization: University of Scranton
  9. Lines: 440
  10. Nntp-Posting-Host: jaguar.uofs.edu
  11.  
  12.  
  13. I am trying to create a ribbon window that appears at the top of the screen.
  14. Within the ribbon, there is an area for coordinates to be displayed, 
  15. a combobox and an entryfield.  The ribbon draws correctly and everything
  16. seems to work except:
  17.  
  18.     * How come there is no black line around the entryfield (border).
  19.  
  20. Is this a result becuase I am painting the window myself?
  21.  
  22. I have included the code of the RIBBON module.  This module is meant to
  23. be autonomus.  What happens is the a main window procedure will use the
  24. given APIs to control the behavior.
  25.  
  26. Thank you.
  27. David Sobeski
  28. DAS11@JAGUAR.UCS.UOFS.EDU
  29.  
  30.  
  31. *** Example of a window procedure for the main window: ***
  32.  
  33. #include "ribbon.h"
  34. LONG FAR PASCAL WndProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  35. {
  36.    static HWND hwndRibbon;       // handle of the ribbon window
  37.  
  38.    switch (msg)
  39.    {
  40.       case WM_CREATE:
  41.          hwndRibbon = RibbonCreate(hwnd);
  42.          break;
  43.      
  44.       case WM_SIZE:
  45.          POINT coord;
  46.          coord.x = LOWORD(lParam);
  47.          coord.y = HIWORD(lParam);
  48.  
  49.          RibbonResize(hwndRibbon, coord);
  50.          break;
  51.  
  52.       case WM_MOUSEMOVE:
  53.          if (wParam == MK_LBUTTON)
  54.          {
  55.             SetCapture(hwnd);
  56.             RibbonSetCoord(hwndRibbon, X_COORD, LOWORD(lParam));
  57.             RibbonSetCoord(hwndRibbon, Y_COORD, LOWORD(lParam));
  58.             ReleaseCapture();
  59.          }
  60.          break;
  61.  
  62.       case WM_DESTROY:
  63.          RibbonDestroy(hwndRibbon);
  64.          PostQuitMessage(0);
  65.          break;
  66.    }
  67.    return (DefWndProc(hwnd, msg, wParam, lParam));
  68. }
  69.  
  70. THE FOLLOWING IS THE CODE FOR THE RIBBON MODULE:
  71.  
  72. /* ---------------------------- ribbon.cpp ---------------------------- */
  73. /*                                                                      */
  74. /*    DESCRIPTION:                                                      */
  75. /*    -----------                                                       */
  76. /*    This module contains all the code necessary for an application to */
  77. /*    implement a ribbon.                                               */
  78. /*                                                                      */
  79. /* -------------------------------------------------------------------- */
  80.  
  81. #define WIN31
  82. #include <windows.h>
  83. #include <string.h>
  84. #include <stdlib.h>
  85.  
  86. #include "ribbon.h"
  87.  
  88. /**********
  89.  Constant definitions
  90.  **********/
  91. const char szClassName[]     = "RIBBON";     // class to register
  92. const char szAppName[]       = "";           // application name
  93. const int  cyCoord           = 57;           // icon bar height
  94. const int  RB_TEXTSIZE       = 5;            // size of coordinate text
  95.  
  96. /**********
  97.  Control ids
  98.  **********/
  99. const int  TX_SYMBOL         = 1000;
  100. const int  CB_SYMBOL         = 1001;
  101. const int  TX_CAPTION        = 1010;
  102. const int  EF_CAPTION        = 1011;
  103. const int  TX_ID             = 1020;
  104. const int  EF_ID             = 1021;
  105.  
  106. static char  INIT_COORD[] = "0";
  107. static char  szXCoord[RB_TEXTSIZE];
  108. static char  szYCoord[RB_TEXTSIZE];
  109. static char  szCXCoord[RB_TEXTSIZE];
  110. static char  szCYCoord[RB_TEXTSIZE];
  111.  
  112. /**********
  113.  Function prototypes
  114.  **********/
  115. long FAR PASCAL RibbonProc(HWND, WORD, WORD, LONG);
  116. void CreateRibbon(HWND);
  117. void PaintRibbon(HWND, WORD);
  118. HBRUSH CtlColorRibbon(HWND, WORD, LONG);
  119.  
  120. /* -------------------------------------------------------------------- */
  121. /*    Function:            RibbonCreate                                 */
  122. /*                         EXTERNAL FUNCTION                            */
  123. /*                                                                      */
  124. /*    Parameters:          hwndParent - handle of the parent window     */
  125. /*                                                                      */
  126. /*    Returns:             HWND       - handle of the icon bar          */
  127. /*                                                                      */
  128. /*    Description:         Creates the icon bar window.                 */
  129. /* -------------------------------------------------------------------- */
  130. HWND long FAR PASCAL RibbonCreate(HWND hwndParent)
  131. {
  132.    WNDCLASS   wndclass;      // window class for the icon bar
  133.    HWND       hwnd;          // handle of the icon bar window
  134.  
  135.    wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  136.    wndclass.lpfnWndProc      = (WNDPROC)RibbonProc;
  137.    wndclass.cbClsExtra       = 0;
  138.    wndclass.cbWndExtra       = 0;
  139.    wndclass.hInstance        = GetWindowWord(hwndParent, GWW_HINSTANCE);
  140.    wndclass.hIcon            = NULL;
  141.    wndclass.hCursor          = LoadCursor(NULL, IDC_ARROW);
  142.    wndclass.hbrBackground    = GetStockObject(LTGRAY_BRUSH);
  143.    wndclass.lpszMenuName     = 0;
  144.    wndclass.lpszClassName    = szClassName;
  145.  
  146.    RegisterClass(&wndclass);
  147.  
  148.    hwnd = CreateWindow(szClassName,
  149.                        szAppName,
  150.                        WS_CHILDWINDOW,
  151.                        0, 0, 0, 0,
  152.                        hwndParent,
  153.                        NULL,
  154.                        GetWindowWord(hwndParent, GWW_HINSTANCE),
  155.                        NULL);
  156.  
  157.    strcpy(szXCoord,  INIT_COORD);
  158.    strcpy(szYCoord,  INIT_COORD);
  159.    strcpy(szCXCoord, INIT_COORD);
  160.    strcpy(szCYCoord, INIT_COORD);
  161.  
  162.    ShowWindow(hwnd, SW_SHOW);
  163.    UpdateWindow(hwnd);
  164.  
  165.    return (hwnd);
  166. }
  167.  
  168. /* -------------------------------------------------------------------- */
  169. /*    Function:            RibbonDestroy                                */
  170. /*                         EXTERNAL FUNCTION                            */
  171. /*                                                                      */
  172. /*    Parameters:          hwndParent - handle of the parent window     */
  173. /*                                                                      */
  174. /*    Returns:             HWND       - handle of the icon bar          */
  175. /*                                                                      */
  176. /*    Description:         Destroys the icon bar window.                */
  177. /* -------------------------------------------------------------------- */
  178. BOOL long FAR PASCAL RibbonDestroy(HWND hwnd)
  179. {
  180.    HWND   hwndParent;        // handle of the parent window
  181.  
  182.    hwndParent = GetParent(hwnd);
  183.  
  184.    DestroyWindow(hwnd);
  185.    UnregisterClass(szClassName, GetWindowWord(hwndParent, GWW_HINSTANCE));
  186.  
  187.    return (TRUE);
  188. }
  189.  
  190. /* -------------------------------------------------------------------- */
  191. /*    Function:            RibbonResize                                 */
  192. /*                         EXTERNAL FUNCTION                            */
  193. /*                                                                      */
  194. /*    Parameters:          hwnd      - handle of the icon bar           */
  195. /*                         ptlClient - the cx and cy of the parent      */
  196. /*                                     window                           */
  197. /*                                                                      */
  198. /*    Returns:             BOOL      - function completed.              */
  199. /*                                                                      */
  200. /*    Description:         Sizes the icon bar to fit within its parent  */
  201. /*                         window.                                      */
  202. /* -------------------------------------------------------------------- */
  203. BOOL long FAR PASCAL RibbonResize(HWND hwnd, POINT ptlClient)
  204. {
  205.    MoveWindow(hwnd,          // handle of the icon bar
  206.               0,             // x-coordinate
  207.               31,            // y-coordinate
  208.               ptlClient.x,   // width of the icon bar
  209.               cyCoord+1,     // height of the icon bar
  210.               TRUE);         // force a repaint
  211.  
  212.    return (TRUE);
  213. }
  214.  
  215. /* -------------------------------------------------------------------- */
  216. /* -------------------------------------------------------------------- */
  217. BOOL FAR PASCAL RibbonSetCoord(HWND hwnd, int coord, int iCoord)
  218. {
  219.    HDC  hdc;
  220.    char szCoord[RB_TEXTSIZE];
  221.  
  222.    if (strlen(szCoord) > RB_TEXTSIZE)
  223.       return (FALSE);
  224.  
  225.    itoa(iCoord, szCoord, 10);
  226.    switch (coord)
  227.    {
  228.       case X_COORD   : strcpy(szXCoord,  szCoord);   break;
  229.       case Y_COORD   : strcpy(szYCoord,  szCoord);   break;
  230.       case CX_COORD  : strcpy(szCXCoord, szCoord);   break;
  231.       case CY_COORD  : strcpy(szCYCoord, szCoord);   break;
  232.       default        : return (FALSE);
  233.    }
  234.  
  235.    PaintRibbon(hwnd, NULL);
  236.    return (TRUE);    
  237. }
  238.  
  239. /* -------------------------------------------------------------------- */
  240. /*    Function:            RibbonWndProc                                */
  241. /*                         INTERNAL FUNCTION                            */
  242. /*                                                                      */
  243. /*    Parameters:          hwnd    - handle of the icon bar window      */
  244. /*                         msg     - message                            */
  245. /*                         wParam  - message parameter                  */
  246. /*                         lParam  - message parameter                  */
  247. /*                                                                      */
  248. /*    Returns:             LONG    - default window procedure           */
  249. /*                                                                      */
  250. /*    Description:         This is the function that handles all the    */
  251. /*                         messages to the icon bar.                    */
  252. /* -------------------------------------------------------------------- */
  253. long FAR PASCAL RibbonProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  254. {
  255.    static HBRUSH hBrush;
  256.  
  257.    switch (msg)
  258.    {
  259.       case WM_CREATE:
  260.          CreateRibbon(hwnd);
  261.          break;
  262.  
  263.       case WM_PAINT:
  264.          PaintRibbon(hwnd, WM_PAINT);
  265.          break;
  266.  
  267.       case WM_CTLCOLOR:
  268.          hBrush = CtlColorRibbon(hwnd, wParam, lParam);
  269.          return (hBrush);
  270.  
  271.       case WM_DESTROY:
  272.          DeleteObject(hBrush);
  273.          break;
  274.    }
  275.  
  276.    return (DefWindowProc(hwnd, msg, wParam, lParam));
  277. }
  278.  
  279. /* -------------------------------------------------------------------- */
  280. /* -------------------------------------------------------------------- */
  281. void CreateRibbon(HWND hwnd)
  282. {
  283.    CreateWindow("static",
  284.                 "Symbol",
  285.                 WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  286.                 210, 8, 50, 20,
  287.                 hwnd,
  288.                 TX_SYMBOL,
  289.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  290.                 NULL);
  291.  
  292.    CreateWindow("combobox",
  293.                 "",
  294.                 WS_CHILDWINDOW | WS_VISIBLE |
  295.                 CBS_DROPDOWN | CBS_SORT | CBS_AUTOHSCROLL,
  296.                 275, 5, 200, 200,
  297.                 hwnd,
  298.                 CB_SYMBOL,
  299.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  300.                 NULL);
  301.  
  302.    CreateWindow("static",
  303.                 "Id",
  304.                 WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  305.                 500, 8, 50, 20,
  306.                 hwnd,
  307.                 TX_ID,
  308.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  309.                 NULL);
  310.  
  311.    CreateWindow("edit",
  312.                 "",
  313.                 WS_CHILDWINDOW | WS_VISIBLE |
  314.                 ES_LEFT | ES_AUTOHSCROLL,
  315.                 530, 6, 50, 20,
  316.                 hwnd,
  317.                 EF_ID,
  318.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  319.                 NULL);
  320.  
  321.    CreateWindow("static",
  322.                 "Caption",
  323.                 WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
  324.                 210, 35, 50, 20,
  325.                 hwnd,
  326.                 TX_CAPTION,
  327.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  328.                 NULL);
  329.  
  330.    CreateWindow("edit",
  331.                 "",
  332.                 WS_CHILDWINDOW | WS_VISIBLE |
  333.                 ES_LEFT | ES_AUTOHSCROLL,
  334.                 275, 33, 175, 20,
  335.                 hwnd,
  336.                 EF_CAPTION,
  337.                 GetWindowWord(hwnd, GWW_HINSTANCE),
  338.                 NULL);
  339.  
  340.    SetFocus(GetDlgItem(hwnd, EF_CAPTION));
  341. }
  342.  
  343. /* -------------------------------------------------------------------- */
  344. /*    Function:            PaintRibbon                                  */
  345. /*                         INTERNAL FUNCTION                            */
  346. /*                                                                      */
  347. /*    Parameters:          hwnd    - handle of the icon bar window      */
  348. /*                                                                      */
  349. /*    Returns:             NONE                                         */
  350. /*                                                                      */
  351. /*    Description:         This is the function performs the drawing of */
  352. /*                         the icon bar.                                */
  353. /* -------------------------------------------------------------------- */
  354. void PaintRibbon(HWND hwnd, WORD msg)
  355. {
  356.    HDC            hdc;       // handle to the icon bar device context
  357.    PAINTSTRUCT    ps;        // the paint structure
  358.    RECT           rClient;   // coordinates of the icon bar client area
  359.  
  360.    if (msg == WM_PAINT)
  361.       hdc = BeginPaint(hwnd, &ps);
  362.    else
  363.       hdc = GetDC(hwnd);
  364.  
  365.    GetClientRect(hwnd, &rClient);
  366.  
  367.    /**********
  368.     Draw black line on the bottom of the icon bar.
  369.     **********/
  370.    SelectObject(hdc, GetStockObject(BLACK_PEN));
  371.    MoveTo(hdc, 0, cyCoord);
  372.    LineTo(hdc, rClient.right, cyCoord);
  373.  
  374.    /**********
  375.     Draw the white lines on the top and left side to give the icon bar
  376.     the chiseled look.
  377.     **********/
  378.    SelectObject(hdc, GetStockObject(WHITE_PEN));
  379.    MoveTo(hdc, 0, 0);
  380.    LineTo(hdc, rClient.right, 0);    // draw the left side 
  381.    MoveTo(hdc, 0, 0);
  382.    LineTo(hdc, 0, rClient.bottom);   // draw the line across the top
  383.  
  384.    /**********
  385.     Make the coordinate area chiseled.
  386.     **********/
  387.    SelectObject(hdc, GetStockObject(BLACK_PEN));
  388.    MoveTo(hdc, 4,   5);   
  389.    LineTo(hdc, 170, 5);   // horizontal line
  390.    MoveTo(hdc, 4,   5);
  391.    LineTo(hdc, 4,   50);  // vertical line
  392.    SelectObject(hdc, GetStockObject(WHITE_PEN));
  393.    MoveTo(hdc, 4,   50);
  394.    LineTo(hdc, 171, 50);  // horizontal line
  395.    MoveTo(hdc, 170, 5);
  396.    LineTo(hdc, 170, 50);  // vertical line
  397.  
  398.    /**********
  399.     Set the text in the ribbon to have a light gray background.
  400.     **********/
  401.    SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
  402.    SetTextColor(hdc, RGB(0, 0, 0));                    // black
  403.    SetBkColor(hdc, RGB(192, 192, 192));                // light gray
  404.  
  405.    TextOut(hdc, 10, 10, "X-Coord:",  strlen("X-Coord:"));
  406.    TextOut(hdc, 10, 34, "Y-Coord:",  strlen("Y-Coord:"));
  407.    TextOut(hdc, 90, 10, "CX-Coord:", strlen("CX-Coord:"));
  408.    TextOut(hdc, 90, 34, "CY-Coord:", strlen("CY-Coord:"));
  409.  
  410.    SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
  411.    SetTextColor(hdc, RGB(0, 0, 0));                 
  412.    SetBkColor(hdc, RGB(192, 192, 192));             
  413.  
  414.    TextOut(hdc, 60,  10, szXCoord,  strlen(szXCoord));
  415.    TextOut(hdc, 60,  34, szYCoord,  strlen(szYCoord));
  416.    TextOut(hdc, 145, 10, szCXCoord, strlen(szCXCoord));
  417.    TextOut(hdc, 145, 34, szCYCoord, strlen(szCYCoord));
  418.  
  419.    if (msg == WM_PAINT)
  420.       EndPaint(hwnd, &ps);
  421.    else
  422.       ReleaseDC(hwnd, hdc);
  423. }
  424.  
  425. /* -------------------------------------------------------------------- */
  426. /* -------------------------------------------------------------------- */
  427. HBRUSH CtlColorRibbon(HWND hwnd, WORD wParam, LONG lParam)
  428. {
  429.    HBRUSH hBrush;
  430.    POINT  point;
  431.  
  432.    if (HIWORD(lParam) == CTLCOLOR_EDIT)
  433.    {
  434.       hBrush = CreateSolidBrush(RGB(255, 255, 255));
  435.       SetBkColor(wParam, RGB(255, 255, 255));
  436.    }
  437.    else
  438.    {
  439.       hBrush = CreateSolidBrush(RGB(192, 192, 192));
  440.       SetBkColor(wParam, RGB(192, 192, 192));
  441.    }
  442.  
  443.  
  444.    SetTextColor(wParam, RGB(0, 0, 0));
  445.    UnrealizeObject(hBrush);
  446.    point.x = point.y = 0;
  447.    ClientToScreen(hwnd, &point);
  448.    SetBrushOrg(wParam, point.x, point.y);
  449.  
  450.    return ((DWORD)hBrush);
  451. }
  452.