質問
C/C++でCreateDIBSection
を使って32bit Bitmapを描画しようとしているのですがどうしても正常に色が表示されません。
argbの順番が違うかもしれませんがそれ以上に
すべて0x00000000にもかかわらず出力される色がは白、
0xffffff00にもかかわらず出力は黒というあべこべの結果となります。
何が原因なのでしょうか?
コード
164行目付近のlpCambusで色は変わります。
C++
1#include <cstdint> 2#include <Windows.h> 3 4namespace weapon 5{ 6 constexpr wchar_t CLASS_NAME[] = L"Sample"; 7 8 constexpr int64_t imageWidth = 768; 9 constexpr int64_t imageHeight= 420; 10 11 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 12 13 using WeaponError = 14 enum class _tagWeaponError 15 { 16 FAILED_ARG_NULLPTR = 0x0001, 17 FAILED_CREATE_DIB_SECTION, 18 SUCCESS = 0x1000 19 }; 20 21 WeaponError CreateBitmap(HBITMAP* hBitmap, HDC* hdcBitmap, HWND hwnd); 22 23 using ARGB = struct _tagARGB 24 { 25 uint8_t green; 26 uint8_t blue; 27 uint8_t red; 28 uint8_t alpha; 29 }; 30} 31 32int WINAPI wWinMain 33(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR nCmdLine, _In_ int nCmdShow) 34{ 35 UNREFERENCED_PARAMETER(hPrevInstance); 36 UNREFERENCED_PARAMETER(nCmdLine); 37 38 WNDCLASSEXW wc = { 39 sizeof(WNDCLASSEXW), CS_VREDRAW | CS_HREDRAW, weapon::WindowProc, 40 0, 0, hInstance, 41 nullptr, (HCURSOR)LoadCursor(nullptr, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), 42 nullptr, weapon::CLASS_NAME, nullptr 43 }; 44 45 RegisterClassExW(&wc); 46 47 HWND hwnd = CreateWindowEx( 48 0, weapon::CLASS_NAME, weapon::CLASS_NAME, WS_OVERLAPPEDWINDOW, 49 50, 50, 960, 525, 50 nullptr, nullptr, hInstance, nullptr 51 ); 52 53 UpdateWindow(hwnd); 54 ShowWindow(hwnd, nCmdShow); 55 56 MSG msg = {}; 57 58 while (GetMessageW(&msg, nullptr, 0, 0) > 0) { 59 TranslateMessage(&msg); 60 DispatchMessageW(&msg); 61 } 62 63 return (int)msg.wParam; 64} 65 66namespace weapon 67{ 68 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 69 { 70 static RECT rc = { 0 }; 71 static int64_t iWidth = 0; 72 static int64_t iHeight = 0; 73 74 static HBITMAP hBitmap = nullptr; 75 static HDC hdcBitmap = nullptr; 76 77 switch (uMsg) 78 { 79 case WM_CREATE: 80 { 81 GetClientRect(hwnd, &rc); 82 iWidth = rc.right - rc.left; 83 iHeight = rc.bottom - rc.top; 84 85 weapon::WeaponError err = CreateBitmap(&hBitmap, &hdcBitmap, hwnd); 86 87 int64_t _breakpoint = 0; 88 } 89 break; 90 91 case WM_SIZE: 92 { 93 RECT tmprc = { 0 }; 94 GetClientRect(hwnd, &tmprc); 95 int tmpWidth = tmprc.right - tmprc.left; 96 int tmpHeight = tmprc.bottom - tmprc.top; 97 98 if (tmpWidth != iWidth || tmpHeight != iHeight) { 99 rc = tmprc; 100 iWidth = tmpWidth; 101 iHeight = tmpHeight; 102 } 103 } 104 break; 105 106 case WM_PAINT: 107 { 108 PAINTSTRUCT ps = {}; 109 HDC hdc = BeginPaint(hwnd, &ps); 110 FillRect(hdc, &ps.rcPaint, (HBRUSH)GetStockObject(WHITE_BRUSH)); 111 BitBlt(hdc, (iWidth - weapon::imageWidth) / 2, (iHeight - weapon::imageHeight) / 2, weapon::imageWidth, weapon::imageHeight, hdcBitmap, 0, 0, NOTSRCCOPY); 112 /*StretchBlt 113 ( 114 hdc, (iWidth - weapon::imageWidth) / 2, (iHeight - weapon::imageHeight) / 2, weapon::imageWidth, weapon::imageHeight, 115 hdcBitmap, 0, 0, weapon::imageWidth, weapon::imageHeight, NOTSRCCOPY 116 );*/ 117 EndPaint(hwnd, &ps); 118 } 119 break; 120 121 case WM_DESTROY: 122 { 123 DeleteObject(hBitmap); 124 DeleteDC(hdcBitmap); 125 PostQuitMessage(0); 126 } 127 break; 128 } 129 130 return DefWindowProcW(hwnd, uMsg, wParam, lParam); 131 } 132 133 WeaponError CreateBitmap(HBITMAP* hBitmap, HDC* hdcBitmap, HWND hwnd) 134 { 135 if (hBitmap == nullptr)return WeaponError::FAILED_ARG_NULLPTR; 136 if (hdcBitmap == nullptr)return WeaponError::FAILED_ARG_NULLPTR; 137 if (hwnd == nullptr)return WeaponError::FAILED_ARG_NULLPTR; 138 139 HBITMAP rethbitmap = nullptr; 140 HDC rethdc = nullptr; 141 142 BITMAPINFO info = 143 { 144 sizeof(BITMAPINFOHEADER), 145 imageWidth, 146 imageHeight, 147 1, 148 32, 149 BI_RGB 150 }; 151 152 HDC hdc = GetDC(hwnd); 153 rethdc = CreateCompatibleDC(hdc); 154 ReleaseDC(hwnd, hdc); 155 156 weapon::ARGB* lpCambus = nullptr; 157 rethbitmap = CreateDIBSection(rethdc, &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&lpCambus), nullptr, 0); 158 if (rethbitmap == INVALID_HANDLE_VALUE) 159 { 160 DeleteDC(rethdc); 161 return WeaponError::FAILED_CREATE_DIB_SECTION; 162 } 163 164 for (int64_t i = 0; i < imageWidth * imageHeight; i++) 165 { 166 lpCambus[i].alpha = 0x00; 167 lpCambus[i].red = 0x00; 168 lpCambus[i].green = 0x00; 169 lpCambus[i].blue = 0x00; 170 } 171 172 SelectObject(rethdc, rethbitmap); 173 174 *hBitmap = rethbitmap; 175 *hdcBitmap = rethdc; 176 177 return WeaponError::SUCCESS; 178 } 179} 180
補足情報(FW/ツールのバージョンなど)
Windows 10
VisualStudio 2022
WindowsSDK ver. 10.0.22000.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。