Maus.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "Maus.h"
  2. #include "Bild.h"
  3. #include "Punkt.h"
  4. using namespace Framework;
  5. // Inhalt der Maus Klasse aus Maus.h
  6. // Konstruktor
  7. Maus::Maus()
  8. : ReferenceCounter(),
  9. hMaus(LoadCursor(0, IDC_ARROW))
  10. {}
  11. // nicht constant
  12. void Maus::ladeMaus(int mausId)
  13. {
  14. if (mausId == MausId::nichts) hMaus = 0;
  15. if (mausId == MausId::normal) hMaus = LoadCursor(0, IDC_ARROW);
  16. if (mausId == MausId::hand) hMaus = LoadCursor(0, IDC_HAND);
  17. if (mausId == MausId::warten) hMaus = LoadCursor(0, IDC_APPSTARTING);
  18. if (mausId == MausId::verschieben) hMaus = LoadCursor(0, IDC_SIZEALL);
  19. if (mausId == MausId::text) hMaus = LoadCursor(0, IDC_IBEAM);
  20. if (mausId == MausId::wahgerecht) hMaus = LoadCursor(0, IDC_SIZEWE);
  21. if (mausId == MausId::senkrecht) hMaus = LoadCursor(0, IDC_SIZENS);
  22. if (mausId == MausId::diagonal1) hMaus = LoadCursor(0, IDC_SIZENWSE);
  23. if (mausId == MausId::diagonal2) hMaus = LoadCursor(0, IDC_SIZENESW);
  24. if (mausId == MausId::verboten) hMaus = LoadCursor(0, IDC_NO);
  25. SetCursor(hMaus);
  26. }
  27. void Maus::ladeMaus(Bild* maus)
  28. {
  29. HBITMAP hAndMaskBitmap;
  30. HBITMAP hXorMaskBitmap;
  31. HDC hDC = GetDC(0);
  32. HDC hAndMaskDC = CreateCompatibleDC(hDC);
  33. HDC hXorMaskDC = CreateCompatibleDC(hDC);
  34. hAndMaskBitmap
  35. = CreateCompatibleBitmap(hDC, maus->getBreite(), maus->getHeight());
  36. hXorMaskBitmap
  37. = CreateCompatibleBitmap(hDC, maus->getBreite(), maus->getHeight());
  38. // Select the bitmaps to DC
  39. HBITMAP hOldAndMaskBitmap
  40. = (HBITMAP)SelectObject(hAndMaskDC, hAndMaskBitmap);
  41. HBITMAP hOldXorMaskBitmap
  42. = (HBITMAP)SelectObject(hXorMaskDC, hXorMaskBitmap);
  43. // Scan each pixel of the souce bitmap and create the masks
  44. int y;
  45. for (int x = 0; x < maus->getBreite(); ++x)
  46. {
  47. for (y = 0; y < maus->getHeight(); ++y)
  48. {
  49. int pixel = maus->getPixel(x, y);
  50. if (((pixel >> 24) & 0xFF) == 0)
  51. {
  52. SetPixel(hAndMaskDC, x, y, RGB(255, 255, 255));
  53. SetPixel(hXorMaskDC, x, y, RGB(0, 0, 0));
  54. }
  55. else
  56. {
  57. SetPixel(hAndMaskDC, x, y, RGB(0, 0, 0));
  58. SetPixel(hXorMaskDC,
  59. x,
  60. y,
  61. RGB((pixel >> 16) & 0xFF,
  62. (pixel >> 8) & 0xFF,
  63. pixel & 0xFF));
  64. }
  65. }
  66. }
  67. SelectObject(hAndMaskDC, hOldAndMaskBitmap);
  68. SelectObject(hXorMaskDC, hOldXorMaskBitmap);
  69. DeleteDC(hXorMaskDC);
  70. DeleteDC(hAndMaskDC);
  71. ReleaseDC(0, hDC);
  72. ICONINFO iconinfo = {0};
  73. iconinfo.fIcon = 0;
  74. iconinfo.xHotspot = 0;
  75. iconinfo.yHotspot = 0;
  76. iconinfo.hbmMask = hAndMaskBitmap;
  77. iconinfo.hbmColor = hXorMaskBitmap;
  78. hMaus = CreateIconIndirect(&iconinfo);
  79. SetCursor(hMaus);
  80. }
  81. void Maus::update()
  82. {
  83. SetCursor(hMaus);
  84. }
  85. // constant
  86. HCURSOR Maus::getMausHandle()
  87. {
  88. return hMaus;
  89. }