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

unix.superglobalmegacorp.com

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