Annotation of mstools/ole20/samples/spoly2/statbar.cpp, revision 1.1.1.1

1.1       root        1: /*** 
                      2: *statbar.cpp
                      3: *
                      4: *  Copyright (C) 1992, Microsoft Corporation.  All Rights Reserved.
                      5: *  Information Contained Herein Is Proprietary and Confidential.
                      6: *
                      7: *Purpose:
                      8: *
                      9: *Implementation Notes:
                     10: *
                     11: *****************************************************************************/
                     12: 
                     13: #include <stdarg.h>
                     14: 
                     15: #include <windows.h>
                     16: #include <ole2.h>
                     17: #include <dispatch.h>
                     18: #include <string.h>
                     19: 
                     20: #include "statbar.h"
                     21: 
                     22: 
                     23: extern "C" long FAR PASCAL StatBarWndProc(HWND, UINT, WPARAM, LPARAM);
                     24: 
                     25: 
                     26: char FAR* CStatBar::m_szWndClass = "StatBarWndClass";
                     27: 
                     28: 
                     29: CStatBar::CStatBar()
                     30: {
                     31:     m_refs = 0;
                     32: 
                     33:     m_x = 0;
                     34:     m_y = 0;
                     35:     m_width = 0;
                     36:     m_height = 0;
                     37: 
                     38:     m_bstrMsg = NULL;
                     39: 
                     40:     m_hfont = (HANDLE)0;
                     41: }
                     42: 
                     43: CStatBar::~CStatBar()
                     44: {
                     45:     SysFreeString(m_bstrMsg);
                     46: }
                     47: 
                     48: 
                     49: /***
                     50: *PUBLIC CStatBar FAR* CStatBar::Create(HANDLE, HWND)
                     51: *
                     52: *Purpose:
                     53: *
                     54: *Entry:
                     55: *
                     56: *Exit:
                     57: *
                     58: ***********************************************************************/
                     59: CStatBar FAR*
                     60: CStatBar::Create(HANDLE hinst, HWND hwndFrame)
                     61: {
                     62:     CStatBar FAR* psb;
                     63: 
                     64:     psb = new FAR CStatBar();
                     65:     if(psb == NULL)
                     66:       return NULL;
                     67:     psb->AddRef();
                     68: 
                     69:     if(!psb->Register(hinst))
                     70:       goto LFail;
                     71: 
                     72:     psb->m_hwnd = CreateWindow(
                     73:       CStatBar::m_szWndClass,
                     74:       NULL,
                     75:       WS_CHILD | WS_CLIPSIBLINGS,
                     76:       0, 0, 0, 0,
                     77:       hwndFrame,
                     78:       0,
                     79:       hinst,
                     80:       NULL);
                     81: 
                     82:     if(!psb->m_hwnd)
                     83:       goto LFail;
                     84: 
                     85:     // Stash the newly created CStatBar* in the extra bytes of the
                     86:     // associated window so we can get at the instance in the message
                     87:     // proc.
                     88:     //
                     89:     // Note: we do not AddRef for this reference. We make sure that the
                     90:     // window is destroyed when the refcnt goes to 0.
                     91:     //
                     92:     SetWindowLong(psb->m_hwnd, 0, (LONG)psb);
                     93: 
                     94:     return psb;
                     95: 
                     96: LFail:;
                     97:     delete psb;
                     98:     return NULL;
                     99: }
                    100: 
                    101: 
                    102: //---------------------------------------------------------------------
                    103: //                     IUnknown Methods
                    104: //---------------------------------------------------------------------
                    105: 
                    106: 
                    107: STDMETHODIMP
                    108: CStatBar::QueryInterface(REFIID riid, void FAR* FAR* ppv)
                    109: {
                    110:     if(riid == IID_IUnknown){
                    111:       *ppv = this;
                    112:       AddRef();
                    113:       return NOERROR;
                    114:     }
                    115:     *ppv = (void FAR*)NULL;
                    116:     return ResultFromScode(E_NOINTERFACE);
                    117: }
                    118: 
                    119: 
                    120: STDMETHODIMP_(ULONG)
                    121: CStatBar::AddRef(void)
                    122: {
                    123:     return ++m_refs;
                    124: }
                    125: 
                    126: 
                    127: STDMETHODIMP_(ULONG)
                    128: CStatBar::Release(void)
                    129: {
                    130:     if(--m_refs == 0){
                    131: 
                    132:       // destroy the status bar window.
                    133:       //
                    134:       SendMessage(m_hwnd, WM_DESTROY, 0, 0L);
                    135: 
                    136:       delete this;
                    137:       return 0;
                    138:     }
                    139: 
                    140:     return m_refs;
                    141: }
                    142: 
                    143: 
                    144: //---------------------------------------------------------------------
                    145: //                     Introduced Methods
                    146: //---------------------------------------------------------------------
                    147: 
                    148: 
                    149: /***
                    150: *PRIVATE BOOL CStatBar::Register(HANDLE)
                    151: *
                    152: *Purpose:
                    153: *  Register the status bar window class.
                    154: *
                    155: *Entry:
                    156: *  None
                    157: *
                    158: *Exit:
                    159: *  return value = BOOL, TRUE if successful, FALSE if not.
                    160: *
                    161: ***********************************************************************/
                    162: BOOL
                    163: CStatBar::Register(HANDLE hinst)
                    164: {
                    165:     WNDCLASS  wc;
                    166: 
                    167:     // register the class, unless already registered.
                    168:     if(GetClassInfo(hinst, m_szWndClass, &wc) == 0){
                    169:       wc.style         = 0;
                    170:       wc.lpfnWndProc   = StatBarWndProc;
                    171:       wc.cbClsExtra    = 0;
                    172:       wc.cbWndExtra    = sizeof(CStatBar FAR*);
                    173:       wc.hInstance     = hinst;
                    174:       wc.hIcon         = 0;
                    175:       wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
                    176:       wc.hbrBackground = GetStockObject(LTGRAY_BRUSH); 
                    177:       wc.lpszMenuName  = 0;
                    178:       wc.lpszClassName = CStatBar::m_szWndClass;
                    179:       if(!RegisterClass(&wc))
                    180:         return FALSE;
                    181:     }
                    182:     return TRUE;
                    183: }
                    184: 
                    185: 
                    186: /***
                    187: *PUBLIC void CStatBar::Show(void)
                    188: *
                    189: *Purpose:
                    190: *  Show the status bar window associated with this CStatBar instance.
                    191: *
                    192: *Entry:
                    193: *  None
                    194: *
                    195: *Exit:
                    196: *  None
                    197: *
                    198: ***********************************************************************/
                    199: void
                    200: CStatBar::Show()
                    201: {
                    202:     ShowWindow(m_hwnd, SW_SHOW);
                    203: }
                    204: 
                    205: void
                    206: CStatBar::SetFont(HFONT hfont)
                    207: {
                    208:     HDC hdc;
                    209:     TEXTMETRIC tm;
                    210:     HFONT hfontOld;
                    211: 
                    212:     // compute the character sizes given this new font.
                    213:     //
                    214:     hdc = GetDC(m_hwnd);
                    215:     hfontOld = SelectObject(hdc, hfont);
                    216:     GetTextMetrics(hdc, &tm);
                    217:     m_dxFont = tm.tmAveCharWidth;
                    218:     m_dyFont = tm.tmHeight + tm.tmExternalLeading;
                    219:     SelectObject(hdc, hfontOld);
                    220:     ReleaseDC(m_hwnd, hdc);
                    221: 
                    222:     m_hfont = hfont;
                    223: }
                    224: 
                    225: /***
                    226: *PRIVATE CStatBar::WMPaint(void)
                    227: *
                    228: *Purpose:
                    229: *  This method is responsible for drawing the status bar, and is called
                    230: *  in response to a WM_PAINT message.
                    231: *
                    232: *Entry:
                    233: *  None
                    234: *
                    235: *Exit:
                    236: *  None
                    237: *
                    238: ***********************************************************************/
                    239: void
                    240: CStatBar::WMPaint()
                    241: {
                    242:     HDC hdc;
                    243:     RECT rcMsg;
                    244:     HRGN hrgn;
                    245:     HFONT hfontOld;
                    246:     PAINTSTRUCT ps;
                    247:     HPEN hpenBlack, hpenWhite, hpenGray, hpenOld;
                    248: 
                    249:     hdc = BeginPaint(m_hwnd, &ps);
                    250: 
                    251:     // compute the message box rect
                    252:     //
                    253:     rcMsg.top  = 3;
                    254:     rcMsg.bottom= m_height - 3;
                    255:     rcMsg.left = m_dxFont;
                    256:     rcMsg.right        = m_width - m_dxFont;
                    257: 
                    258:     // prepare the pens
                    259:     //
                    260:     hpenWhite  = GetStockObject(WHITE_PEN);
                    261:     hpenBlack  = GetStockObject(BLACK_PEN);
                    262:     hpenGray   = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
                    263: 
                    264:     // draw a top gray line
                    265:     //
                    266:     hpenOld = SelectObject(hdc, hpenGray);
                    267: #if   defined(WIN16)   
                    268:     MoveTo(hdc, ps.rcPaint.left, 0);
                    269: #elif defined(WIN32)   
                    270:     MoveToEx(hdc, ps.rcPaint.left, 0, NULL);
                    271: #endif
                    272:     LineTo(hdc, ps.rcPaint.right, 0);
                    273: 
                    274:     // draw a white line just under
                    275:     //
                    276:     SelectObject(hdc, hpenWhite);
                    277: #if   defined(WIN16)   
                    278:     MoveTo(hdc, ps.rcPaint.left, 1);
                    279: #elif defined(WIN32)   
                    280:     MoveToEx(hdc, ps.rcPaint.left, 1, NULL);
                    281: #endif
                    282:     LineTo(hdc, ps.rcPaint.right, 1);
                    283: 
                    284:     // do not overwrite the background color
                    285:     //
                    286:     SetBkMode(hdc, TRANSPARENT);
                    287: 
                    288:     // message area
                    289:     //
                    290:     SelectObject(hdc, hpenBlack);
                    291: #if   defined(WIN16)   
                    292:     MoveTo(hdc, rcMsg.left,  rcMsg.bottom);
                    293: #elif defined(WIN32)   
                    294:     MoveToEx(hdc, rcMsg.left,  rcMsg.bottom, NULL);
                    295: #endif 
                    296:     LineTo(hdc, rcMsg.left,  rcMsg.top);
                    297:     LineTo(hdc, rcMsg.right, rcMsg.top);
                    298: 
                    299:     SelectObject(hdc, hpenWhite);
                    300:     LineTo(hdc, rcMsg.right, rcMsg.bottom);
                    301:     LineTo(hdc, rcMsg.left,  rcMsg.bottom);
                    302: 
                    303:     // select the black pen for writing
                    304:     //
                    305:     SelectObject(hdc, hpenBlack);
                    306: 
                    307:     // select the status bar font to write in
                    308:     //
                    309:     hfontOld = SelectObject(hdc, m_hfont);
                    310: 
                    311:     // set the clipping region
                    312:     //
                    313:     hrgn = CreateRectRgn(
                    314:       rcMsg.left, rcMsg.top, rcMsg.right, rcMsg.bottom);
                    315: 
                    316:     SelectClipRgn(hdc, hrgn);
                    317: 
                    318:     // draw the status message
                    319:     //
                    320:     TextOut(
                    321:       hdc,
                    322:       rcMsg.left + (m_dxFont / 2),
                    323:       rcMsg.top + ((rcMsg.bottom - rcMsg.top - m_dyFont) / 2),
                    324:       m_bstrMsg, SysStringLen(m_bstrMsg));
                    325: 
                    326:     // cleanup
                    327:     //
                    328:     SelectObject(hdc, hpenOld);
                    329:     SelectObject(hdc, hfontOld);
                    330: 
                    331:     DeleteObject(hrgn);
                    332:     DeleteObject(hpenGray);
                    333: 
                    334:     EndPaint(m_hwnd, &ps);
                    335: }
                    336: 
                    337: extern "C" long FAR PASCAL
                    338: StatBarWndProc(
                    339:     HWND hwnd,
                    340:     UINT message,
                    341:     WPARAM wParam,
                    342:     LPARAM lParam)
                    343: {
                    344:     CStatBar FAR* psb;
                    345: 
                    346:     switch(message){
                    347:     case WM_SIZE:
                    348:       return 0;
                    349:     case WM_PAINT:
                    350:       psb = (CStatBar FAR*)GetWindowLong(hwnd, 0);
                    351:       psb->WMPaint();
                    352:       return 0;
                    353:     }
                    354:     return(DefWindowProc(hwnd, message, wParam, lParam));
                    355: }
                    356: 
                    357: 
                    358: //---------------------------------------------------------------------
                    359: //                    Status Bar Utilities
                    360: //---------------------------------------------------------------------
                    361: 
                    362: extern "C" void
                    363: SBprintf(CStatBar FAR* psb, char FAR* szFmt, ...)
                    364: {
                    365:     va_list args;
                    366: static char buf[256];
                    367: 
                    368:     va_start(args, szFmt);
                    369:     wvsprintf(buf, szFmt, args);
                    370:     psb->SetText(buf);
                    371:     psb->Update();
                    372: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.