splash.c
上传用户:jlteech
上传日期:2007-01-06
资源大小:349k
文件大小:4k
源码类别:

压缩解压

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include "wiz.h"
  3. #ifdef WIN32
  4. HWND hSplashScreen;
  5. HBITMAP LoadResourceBitmap(HINSTANCE hInstance, LPSTR lpString,
  6.                            HPALETTE FAR* lphPalette);
  7. HPALETTE CreateDIBPalette (LPBITMAPINFO lpbmi, LPINT lpiNumColors);
  8. HPALETTE CreateDIBPalette (LPBITMAPINFO lpbmi, LPINT lpiNumColors)
  9. {
  10.    LPBITMAPINFOHEADER  lpbi;
  11.    LPLOGPALETTE     lpPal;
  12.    HANDLE           hLogPal;
  13.    HPALETTE         hPal = NULL;
  14.    int              i;
  15.    lpbi = (LPBITMAPINFOHEADER)lpbmi;
  16.    if (lpbi->biBitCount <= 8)
  17.        *lpiNumColors = (1 << lpbi->biBitCount);
  18.    else
  19.        *lpiNumColors = 0;  // No palette needed for 24 BPP DIB
  20.    if (*lpiNumColors)
  21.       {
  22.       hLogPal = GlobalAlloc (GHND, sizeof (LOGPALETTE) +
  23.                              sizeof (PALETTEENTRY) * (*lpiNumColors));
  24.       lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
  25.       lpPal->palVersion    = 0x300;
  26.       lpPal->palNumEntries = (WORD)*lpiNumColors;
  27.       for (i = 0;  i < *lpiNumColors;  i++)
  28.          {
  29.          lpPal->palPalEntry[i].peRed   = lpbmi->bmiColors[i].rgbRed;
  30.          lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
  31.          lpPal->palPalEntry[i].peBlue  = lpbmi->bmiColors[i].rgbBlue;
  32.          lpPal->palPalEntry[i].peFlags = 0;
  33.          }
  34.       hPal = CreatePalette (lpPal);
  35.       GlobalUnlock (hLogPal);
  36.       GlobalFree   (hLogPal);
  37.    }
  38.    return hPal;
  39. }
  40. HBITMAP LoadResourceBitmap(HINSTANCE hInstance, LPSTR lpString,
  41.                            HPALETTE FAR* lphPalette)
  42. {
  43.     HRSRC  hRsrc;
  44.     HGLOBAL hGlobal, hTemp;
  45.     DWORD dwSize;
  46.     HBITMAP hBitmapFinal = NULL;
  47.     LPBITMAPINFOHEADER  lpbi;
  48.     LPSTR lpRes, lpNew;
  49.     HDC hdc;
  50.     int iNumColors;
  51.     if ((hRsrc = FindResource(hInstance, lpString, RT_BITMAP)) != NULL)
  52.     {
  53.        hTemp = LoadResource(hInstance, hRsrc);
  54.        dwSize = SizeofResource(hInstance, hRsrc);
  55.        lpRes = LockResource(hTemp);
  56.        hGlobal = GlobalAlloc(GHND, dwSize);
  57.        lpNew = GlobalLock(hGlobal);
  58.        memcpy(lpNew, lpRes, dwSize);
  59.        FreeResource(hTemp);
  60.        lpbi = (LPBITMAPINFOHEADER)lpNew;
  61.        hdc = GetDC(NULL);
  62.        *lphPalette =  CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors);
  63.        if (*lphPalette)
  64.        {
  65.           SelectPalette(hdc,*lphPalette,FALSE);
  66.           RealizePalette(hdc);
  67.        }
  68.        hBitmapFinal = CreateDIBitmap(hdc,
  69.                  (LPBITMAPINFOHEADER)lpbi,
  70.                  (LONG)CBM_INIT,
  71.                  (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
  72.                  (LPBITMAPINFO)lpbi,
  73.                  DIB_RGB_COLORS );
  74.        ReleaseDC(NULL,hdc);
  75.        GlobalUnlock(hGlobal);
  76.        GlobalFree(hGlobal);
  77.     }
  78.     return (hBitmapFinal);
  79. }
  80. void ShowSplashScreen(void)
  81. {
  82. HWND hWndDesktop;
  83. RECT rc;
  84. HBITMAP hBitmap,hOldBitmap;
  85. HPALETTE hPalette;
  86. HDC hMemDC, hdc;
  87. BITMAP bm;
  88. hBitmap = LoadResourceBitmap(hInst,"splash", &hPalette);
  89. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  90. hWndDesktop = GetDesktopWindow();
  91. GetClientRect(hWndDesktop, &rc);
  92. rc.left = (rc.right-bm.bmWidth)/2;
  93. rc.top  = (rc.bottom-bm.bmHeight)/2;
  94. hSplashScreen = CreateWindow("STATIC", NULL,
  95.         WS_CHILD|WS_VISIBLE | WS_DLGFRAME |
  96.         WS_EX_TOPMOST,
  97.         rc.left,
  98.         rc.top,
  99.         bm.bmWidth+(2*GetSystemMetrics(SM_CXFIXEDFRAME)),
  100.         bm.bmHeight+(2*GetSystemMetrics(SM_CYFIXEDFRAME)),
  101.         hWndDesktop,
  102.         NULL, /* Use class menu */
  103.         (HANDLE)GetWindowLong (hWndDesktop, GWL_HINSTANCE),
  104.         NULL);
  105. SetWindowPos(hSplashScreen,
  106.              HWND_TOP,
  107.              rc.left,
  108.              rc.top,
  109.              bm.bmWidth+(2*GetSystemMetrics(SM_CXFIXEDFRAME)),
  110.              bm.bmHeight+(2*GetSystemMetrics(SM_CYFIXEDFRAME)),
  111.              SWP_SHOWWINDOW);
  112. hdc = GetDC(hSplashScreen);
  113. hMemDC = CreateCompatibleDC(hdc);
  114. SelectPalette(hdc,hPalette,FALSE);
  115. RealizePalette(hdc);
  116. SelectPalette(hMemDC,hPalette,FALSE);
  117. RealizePalette(hMemDC);
  118. hOldBitmap = SelectObject(hMemDC,hBitmap);
  119. BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hMemDC,0,0,SRCCOPY);
  120. DeleteObject(SelectObject(hMemDC,hOldBitmap));
  121. DeleteDC(hMemDC);
  122. ReleaseDC(hSplashScreen,hdc);
  123. DeleteObject(hPalette);
  124. UpdateWindow(hWndDesktop);
  125. }
  126. #endif /* ?WIN32 */