Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.9 KiB

  1. // bitmap.cpp : implementation file
  2. //
  3. //+-------------------------------------------------------------------------
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1999
  7. //
  8. // File: bitmap.cpp
  9. //
  10. // Contents: Helper functions to copy bitmaps
  11. //
  12. // History: 27-Feb-97 WayneSc Created
  13. //
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <objbase.h>
  17. #include <basetyps.h>
  18. //+-------------------------------------------------------------------
  19. //
  20. // Member: CopyBitmap
  21. //
  22. // Synopsis: Make a copy of given bitmap & return handle to the copy.
  23. //
  24. // Returns: HBITMAP - NULL if error
  25. //
  26. // Note: Cannot use SC as we need to include too may headers.
  27. // which will make this dependent on mmcbase.lib, but
  28. // mmcbase.lib is dependent on this (UICore.lib).
  29. //
  30. //--------------------------------------------------------------------
  31. HBITMAP CopyBitmap(HBITMAP hbm)
  32. {
  33. if (!hbm)
  34. return NULL;
  35. HDC hdc = NULL;
  36. HDC hMemDCsrc = NULL;
  37. HDC hMemDCdst = NULL;
  38. HBITMAP hNewBm = NULL;
  39. BITMAP bm;
  40. ZeroMemory(&bm, sizeof(bm));
  41. hdc = GetDC (NULL);
  42. if (!hdc)
  43. goto Error;
  44. hMemDCsrc = CreateCompatibleDC (hdc);
  45. if (!hMemDCsrc)
  46. goto Error;
  47. hMemDCdst = CreateCompatibleDC (hdc);
  48. if (!hMemDCdst)
  49. goto Error;
  50. if (! GetObject (hbm, sizeof(BITMAP), (LPSTR)&bm))
  51. goto Error;
  52. /*hNewBm = +++CreateBitmap - Not Recommended(use CreateDIBitmap)+++ (dx, dy, bm.bmPlanes, bm.bmBitsPixel, NULL);*/
  53. hNewBm = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel, NULL);
  54. if (hNewBm){
  55. HBITMAP hbmSrcOld = (HBITMAP) SelectObject (hMemDCsrc, hbm);
  56. HBITMAP hbmDstOld = (HBITMAP) SelectObject (hMemDCdst, hNewBm);
  57. BitBlt (hMemDCdst,
  58. 0,
  59. 0,
  60. bm.bmWidth,
  61. bm.bmHeight,
  62. hMemDCsrc,
  63. 0,
  64. 0,
  65. SRCCOPY);
  66. SelectObject (hMemDCsrc, hbmSrcOld);
  67. SelectObject (hMemDCdst, hbmDstOld);
  68. }
  69. Cleanup:
  70. if (hdc)
  71. ReleaseDC (NULL,hdc);
  72. if (hMemDCsrc)
  73. DeleteDC (hMemDCsrc);
  74. if (hMemDCdst)
  75. DeleteDC (hMemDCdst);
  76. return hNewBm;
  77. Error:
  78. #ifdef DBG
  79. /*
  80. * Cannot use SC as we need to include too may headers.
  81. * which will make this dependent on mmcbase.lib, but
  82. * mmcbase.lib is dependent on this (UICore.lib).
  83. * So call outputstring in case of error.
  84. */
  85. LPVOID lpMsgBuf;
  86. FormatMessage(
  87. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  88. FORMAT_MESSAGE_FROM_SYSTEM |
  89. FORMAT_MESSAGE_IGNORE_INSERTS,
  90. NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  91. (LPTSTR) &lpMsgBuf, 0, NULL );
  92. OutputDebugString((LPTSTR)lpMsgBuf);
  93. LocalFree( lpMsgBuf );
  94. #endif
  95. hNewBm = NULL;
  96. goto Cleanup;
  97. }