home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / db3stuff / tipwnd.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-03  |  4.3 KB  |  164 lines

  1. //
  2. // TIPWND.C
  3. //
  4. // This is linked with DBTTIP.VBX. It contains all the code managing
  5. // the tool tip window.
  6. //
  7. // "Real programmers don't write comments - the code is obvious"
  8. //
  9.  
  10. #include "dbttip.h"
  11.  
  12.  
  13. void PaintTheTip( HDC hDC, LPRECT lpRect, HCTL hctl)
  14. {
  15.  extern void PipeCopy( LPSTR, LPCSTR );
  16.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  17.  PIC pic;
  18.  
  19.  VBGetPic( lpDBTTip->uUse.hPic, &pic );
  20.  lpDBTTip = LPDBTTIPDEREF( hctl );
  21.  if ( pic.picType == PICTYPE_METAFILE )
  22.   {  //Draw the WMF
  23.    short sdc = SaveDC( hDC );
  24.    SetMapMode( hDC, MM_ANISOTROPIC );
  25.    SetWindowExt( hDC, pic.picData.wmf.xExt, pic.picData.wmf.yExt );
  26.    SetViewportExt( hDC, lpRect->right, lpRect->bottom );
  27.    PlayMetaFile( hDC, pic.picData.wmf.hmeta );
  28.    RestoreDC( hDC, sdc );
  29.   }
  30.  else
  31.    return;  // Error - no picture
  32.  
  33.  { // Draw the text
  34.   char szText[1200];
  35.   HFONT hFontOld = NULL;
  36.   UINT uAlign;
  37.  
  38.   switch ( lpDBTTip->uUse.nAlignment )
  39.    {
  40.     case 0: uAlign = DT_LEFT; break;
  41.     case 1: uAlign = DT_RIGHT; break;
  42.     case 2: uAlign = DT_CENTER; break;
  43.    }
  44.   if ( lpDBTTip->uUse.hFont )
  45.     hFontOld = SelectObject( hDC, lpDBTTip->uUse.hFont );
  46.   SetBkMode( hDC, TRANSPARENT );
  47.   SetTextColor( hDC, lpDBTTip->uUse.rgbForeColor );
  48.   PipeCopy( szText, VBDerefHsz(lpDBTTip->hszText) );
  49.   DrawText( hDC, szText, -1, &(LPDBTTIPDEREF(hctl)->rectText), uAlign|DT_WORDBREAK );
  50.   if ( hFontOld )
  51.      SelectObject( hDC, hFontOld );
  52.  }
  53.  
  54. }
  55.  
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // A normal window procedure
  59.  
  60. long FAR PASCAL __export TipWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  61. {
  62.  switch ( message )
  63.   {
  64.    case WM_PAINT: {
  65.      PAINTSTRUCT ps;
  66.      RECT rect;
  67.      HDC hDC = BeginPaint( hWnd, &ps );
  68.      GetClientRect( hWnd, &rect );
  69.      PaintTheTip( hDC, &rect, (HCTL)GetWindowLong(hWnd,0) );
  70.      EndPaint( hWnd, &ps );
  71.      return 0; }
  72.   }
  73.  
  74.  return DefWindowProc(hWnd, message, wParam, lParam);
  75. }
  76.  
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // Tool tip window creation and destruction
  80.  
  81. static char const szClassName[] = "DBTTipClass";
  82.  
  83. void CreateTipWnd( HCTL hctl )
  84. {
  85.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  86.  short nHotX = (short)( ((LONG)lpDBTTip->nTipWidth*(LONG)lpDBTTip->uUse.nHotSpotX) / 100L );
  87.  short nHotY = (short)( ((LONG)lpDBTTip->nTipHeight*(LONG)lpDBTTip->uUse.nHotSpotY) / 100L );
  88.  short nPosX = lpDBTTip->pMouse.x + lpDBTTip->uUse.nMouseOffX + lpDBTTip->nExtraOffX - nHotX;
  89.  short nPosY = lpDBTTip->pMouse.y + lpDBTTip->uUse.nMouseOffY + lpDBTTip->nExtraOffY - nHotY;
  90.  
  91.  // Posistion the tip so that it's always visible
  92.  if ( nPosX<0 )
  93.    nPosX = 0;
  94.  if ( nPosY<0 )
  95.    nPosY = 0;
  96.  if ( (nPosX+lpDBTTip->nTipWidth) > GetSystemMetrics(SM_CXSCREEN) )
  97.    nPosX = GetSystemMetrics(SM_CXSCREEN)-lpDBTTip->nTipWidth;
  98.  if ( (nPosY+lpDBTTip->nTipHeight) > GetSystemMetrics(SM_CYSCREEN) )
  99.    nPosY = GetSystemMetrics(SM_CYSCREEN)-lpDBTTip->nTipHeight;
  100.  
  101.  lpDBTTip->hWndTip = CreateWindow(
  102.     szClassName,
  103.     NULL,
  104.     WS_POPUP,
  105.     nPosX,
  106.     nPosY,
  107.     lpDBTTip->nTipWidth,
  108.     lpDBTTip->nTipHeight,
  109.     NULL,
  110.     NULL,
  111.     ghmodDLL,
  112.     NULL );
  113.  SetWindowPos( lpDBTTip->hWndTip, HWND_TOPMOST, 0, 0, 0, 0,
  114.                SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
  115.  SetWindowLong( lpDBTTip->hWndTip, 0, (LONG)hctl );
  116. }
  117.  
  118.  
  119. void DestroyTipWnd(HCTL hctl)
  120. {
  121.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  122.  
  123.  if ( lpDBTTip->hWndTip )
  124.    DestroyWindow( lpDBTTip->hWndTip );
  125.  lpDBTTip->hWndTip = NULL;
  126.  lpDBTTip->nExtraOffX = 0;
  127.  lpDBTTip->nExtraOffY = 0;
  128. }
  129.  
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // Tool tip class registration and unregistration
  133.  
  134. static short gnRegisters = 0;
  135.  
  136. BOOL RegisterTipClass(void)
  137. {
  138.  WNDCLASS wc;
  139.  
  140.  if ( gnRegisters++ )  // need only register once
  141.    return TRUE;
  142.  
  143.  wc.style            = CS_SAVEBITS; //NULL;
  144.  wc.lpfnWndProc        = TipWndProc;
  145.  wc.cbClsExtra        = 0;
  146.  wc.cbWndExtra        = sizeof(long);
  147.  wc.hInstance        = ghmodDLL;
  148.  wc.hIcon            = NULL;
  149.  wc.hCursor            = NULL;
  150.  wc.hbrBackground    = NULL;
  151.  wc.lpszMenuName    = NULL;
  152.  wc.lpszClassName    = szClassName;
  153.  
  154.  return RegisterClass( &wc );
  155. }
  156.  
  157.  
  158. void UnregisterTipClass(void)
  159. {
  160.  if ( gnRegisters-- )
  161.    return;
  162.  UnregisterClass( szClassName, ghmodDLL );
  163. }
  164.