前提・実現したいこと
前回の質問から改めてAlphaBlendのレイヤードウィンドウのレンダリングについて質問です。
親ウィンドウはレイヤードウィンドウでWS_EX_RAYEREDを使い
子ウィンドウはWS_ES_TRANSPARENTでウィンドウを作りました。
今回子ウィンドウに透過DIBをAlphaBlendしようと思ったのですがうまくレンダリングされません。
うまく配列代入ができていないのでしょうか。
該当のソースコード
C
1#define UNICODE 2 3#include <Windows.h> 4 5#pragma comment(lib,"msimg32.lib") 6 7LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 8LRESULT CALLBACK ChildWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 9 10HINSTANCE hInst; 11 12const wchar_t CLASS_NAME[] = L"Layered Window"; 13const wchar_t CHILD_CLASS_NAME[] = L"Transparent Window"; 14 15int imgWidth = 300; 16int imgHeight = 500; 17 18int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) 19{ 20 hInst = hInstance; 21 22 WNDCLASSEX wc = { 23 sizeof(WNDCLASSEX),CS_VREDRAW | CS_HREDRAW,WindowProc,0, 24 0,hInstance,NULL,LoadCursor(NULL, IDC_ARROW), 25 (HBRUSH)WHITE_BRUSH,NULL,CLASS_NAME,NULL 26 }; 27 28 RegisterClassEx(&wc); 29 30 HWND hwnd = CreateWindowEx( 31 WS_EX_LAYERED, CLASS_NAME, CLASS_NAME, WS_OVERLAPPEDWINDOW, 32 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 33 NULL, NULL, hInstance, NULL 34 ); 35 if (hwnd == NULL)return 0; 36 37 ShowWindow(hwnd, nCmdShow); 38 39 MSG msg = {}; 40 41 while (GetMessage(&msg, NULL, 0, 0)) 42 { 43 TranslateMessage(&msg); 44 DispatchMessage(&msg); 45 } 46 47 return 0; 48} 49 50LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 51{ 52 COLORREF cr = RGB(255, 0, 0); 53 switch (uMsg) { 54 case WM_CREATE: 55 { 56 SetLayeredWindowAttributes(hwnd, cr, 0x0, LWA_COLORKEY); 57 58 WNDCLASSEX wc = { 59 sizeof(WNDCLASSEX),CS_VREDRAW | CS_HREDRAW,ChildWindowProc,0, 60 0,hInst,NULL,LoadCursor(NULL, IDC_ARROW), 61 (HBRUSH)WHITE_BRUSH,NULL,CHILD_CLASS_NAME,NULL 62 }; 63 64 RegisterClassEx(&wc); 65 66 HWND chwnd = CreateWindowEx( 67 WS_EX_TRANSPARENT, CHILD_CLASS_NAME, NULL, WS_VISIBLE | WS_THICKFRAME | WS_CHILD, 68 50, 50, 300, 500, 69 hwnd, NULL, hInst, NULL 70 ); 71 72 if (chwnd == NULL)return 0; 73 74 ShowWindow(chwnd, SW_SHOW); 75 } 76 break; 77 78 case WM_DESTROY: 79 PostQuitMessage(0); 80 return 0; 81 82 case WM_PAINT: 83 { 84 PAINTSTRUCT ps; 85 HDC hdc = BeginPaint(hwnd, &ps); 86 HBRUSH hBrush = CreateSolidBrush(cr); 87 88 FillRect(hdc, &ps.rcPaint, hBrush); 89 90 EndPaint(hwnd, &ps); 91 DeleteObject(hBrush); 92 93 SetLayeredWindowAttributes(hwnd, cr, 0x0, LWA_COLORKEY); 94 } 95 break; 96 97 } 98 99 return DefWindowProc(hwnd, uMsg, wParam, lParam); 100} 101 102LRESULT CALLBACK ChildWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 103 switch (uMsg) { 104 case WM_PAINT: 105 { 106 HDC hdc; //for window 107 HDC himgdc; //for image 108 BLENDFUNCTION bf; 109 BITMAPINFO bmpi; 110 HBITMAP hbitmap; 111 int winWidth, winHeight; 112 RECT rClient; 113 114 hdc = GetDC(hwnd); 115 GetClientRect(hwnd, &rClient); 116 117 winWidth = rClient.right - rClient.left; 118 winHeight = rClient.bottom - rClient.top; 119 120 himgdc = CreateCompatibleDC(hdc); 121 122//子ウィンドウDIB定義 123 DWORD **image = (DWORD**)malloc(sizeof(DWORD*)*imgWidth); 124 DWORD *image_c = (DWORD*)malloc(sizeof(DWORD)*imgWidth*imgHeight); 125 for (int i = 0; i < imgWidth; i++)image[i] = image_c + i * imgHeight; 126 for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)image[i][j] = 0xff000000; 127 128 DWORD** dib = (DWORD**)calloc(imgWidth*imgHeight, sizeof(DWORD*)); 129 130 for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)dib[j+i*imgWidth] = &image[j][i]; 131 //image配列のxy座標系をDIBのxy座標系に変換するもの 132 133 134 ZeroMemory(&bmpi, sizeof(BITMAPINFO)); 135 136 bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 137 bmpi.bmiHeader.biWidth = imgWidth; 138 bmpi.bmiHeader.biHeight = imgHeight; 139 bmpi.bmiHeader.biPlanes = 1; 140 bmpi.bmiHeader.biBitCount = 32; 141 bmpi.bmiHeader.biCompression = BI_RGB; 142 143//おそらく間違っている場所 144 hbitmap = CreateDIBSection(himgdc, &bmpi, DIB_RGB_COLORS, (void**)&dib, NULL, 0x0); 145 if(hbitmap==NULL)MessageBox(hwnd, L"Failed to male CreateDIBSection", L"CAUTION", IDOK); 146 147 SelectObject(himgdc, hbitmap); 148 149 bf.BlendOp = AC_SRC_OVER; 150 bf.BlendFlags = 0; 151 bf.SourceConstantAlpha = 0xff; 152 bf.AlphaFormat = AC_SRC_ALPHA; 153 154 if (!AlphaBlend(hdc, 0, 0, 300, 500, himgdc, 0, 0, imgWidth, imgHeight, bf))return 0; 155 156 DeleteObject(hbitmap); 157 DeleteDC(himgdc); 158 DeleteDC(hdc); 159 160 free(image_c); 161 free(image); 162 } 163 break; 164 } 165 return DefWindowProc(hwnd, uMsg, wParam, lParam); 166}
補足情報(FW/ツールのバージョンなど)
Windows10 Pro
VisualStudio 2017 Community
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/17 15:48
2018/09/17 16:44