Button scaling in winapi - user-interface

I am creating a simple calculator and I have a problem with scaling all elements. They have to scale with main window but I have no idea how to perform such a thing
Here is part of code:
#define ID_BUTTON0 0
#define ID_BUTTON1 1
#define ID_BUTTON2 2
#define ID_BUTTON3 3
#define ID_BUTTON4 4
...
case WM_CREATE:
{
CreateWindow(L"STATIC", L"", WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_TEXTFIELD), 0, NULL);
CreateWindow(L"BUTTON", L"CE",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONCE), 0, NULL);
CreateWindow(L"BUTTON", L"C",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONC), 0, NULL);
CreateWindow(L"BUTTON", L"/",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONDIV), 0, NULL);
CreateWindow(L"BUTTON", L"X",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTONMUL), 0, NULL);
CreateWindow(L"BUTTON", L"7",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON7), 0, NULL);
CreateWindow(L"BUTTON", L"8",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON8), 0, NULL);
CreateWindow(L"BUTTON", L"9",WS_VISIBLE | WS_CHILD,0, 0, 0, 0,hWnd, HMENU(ID_BUTTON9), 0, NULL);
...
resize_window(hWnd);
}
break;
case WM_SIZE:
{
resize_window(hWnd);
UpdateWindow(hWnd);
return 0;
}
void resize_window(HWND hWnd) {
int buttonxsize = WINDOWSIZEW / 4;
int buttonysize = WINDOWSIZEH / 6;
int posx = WINDOWSIZEW / 4;
int posy = WINDOWSIZEH / 6;
RECT rc;
GetClientRect(hWnd, &rc);
SetWindowPos(GetDlgItem(hWnd, ID_TEXTFIELD), 0, 0, 0, WINDOWSIZEW, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONC), 0, 0, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONCE), 0, posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONDIV), 0, 2 * posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
SetWindowPos(GetDlgItem(hWnd, ID_BUTTONMUL), 0, 3 * posx, posy, buttonxsize, buttonysize, SWP_NOZORDER);
...
}
and so on...
After compiling I obtain such a thing :
How do I make it work?

HWND hwndTextField = CreateWindow(L"Static", L"",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP,
0, 0, WINDOWSIZEW, WINDOWSIZEH / 6,
hWnd, NULL, GetModuleHandle(NULL), NULL);
hwndTextField is declared on stack, it will be forgotten as soon as it goes out of scope, you won't be able find the control later (not easily anyway). hwndTextField should be declared as static or global. Better yet, assign an ID for each control using HMENU. This ID can be used to find the control. Also, when you click the button it sends WM_COMMAND message with that ID.
The style for this control should be WS_VISIBLE | WS_CHILD | WS_TABSTOP. However WS_TABSTOP is ignored unless this is created in a dialog.
And resize the controls in a separate function, so that it can be done from both WM_CREATE and WM_SIZE
For example
#define ID_TEXT 100
...
case WM_CREATE:
{
CreateWindow(L"STATIC", L"", WS_VISIBLE | WS_CHILD,
0, 0, 0, 0, hWnd, HMENU(ID_TEXT), 0, NULL);
...
resize_window(hWnd);
}
case WM_SIZE:
{
resize_window(hWnd);
return 0;
}
void resize_window(HWND hwnd)
{
RECT rc;
GetClientRect(hwnd, &rc);
//update ****
WINDOWSIZEW = rc.right;
WINDOWSIZEH = rc.bottom;
...
SetWindowPos(GetDlgItem(hWnd, ID_TEXT), 0, x, y, w, h, SWP_NOZORDER);
...
}

Related

Double buffering is not working properly, produces screen flicker

I'm studying WinAPI double buffering, and when I run the code below, the screen flickers too much. I think I did it right, but where is the problem?
Draw memdc1 and memdc2 to memdc, and memdc to hdc.
switch (iMsg)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetClientRect(hwnd, &clientRt);
PlaceCenterScreen(hwnd);
SetTimer(hwnd, 1, 10, NULL);
hMixBit = CreateCompatibleBitmap(hdc, 819, 614);
hHouseBit = (HBITMAP)LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_HOUSE));
hTigerBit = (HBITMAP)LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_TIGER));
ReleaseDC(hwnd, hdc);
break;
case WM_TIMER:
posX += 3;
InvalidateRgn(hwnd, NULL, true);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
memdc = CreateCompatibleDC(hdc);
memdc1 = CreateCompatibleDC(memdc);
memdc2 = CreateCompatibleDC(memdc);
oldHouseBit = (HBITMAP)SelectObject(memdc1, hHouseBit);
oldTigerBit = (HBITMAP)SelectObject(memdc2, hTigerBit);
oldMixBit = (HBITMAP)SelectObject(memdc, hMixBit);
BitBlt(memdc, 0, 0, 819, 614, memdc1, 0, 0, SRCCOPY);
BitBlt(memdc, posX, 0, 67, 47, memdc2, 0, 0, SRCCOPY);
BitBlt(hdc, 0, 0, 819, 614, memdc, 0, 0, SRCCOPY);
SelectObject(memdc1, oldHouseBit);
SelectObject(memdc2, oldTigerBit);
SelectObject(memdc, oldMixBit);

Winapi, change the color of groupbox frame

I want to make dark semitransparent GUI using WinAPI. I made a custom button, but I cant make my own groupbox. In WM_DRAWITEM case of message handler, the callback drawing my groupbox is not even called. This callback contains MessageBox call that is not even once appeared.
How to draw groupbox with colored frame and colored text? Also I want to make my groupboxes lying under the all other controls. How to do that?
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Main logic goes here...
ShowMainWindow();
}
void RegisterWndClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = Proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(0x14, 0x14, 0x14));
wc.lpszMenuName = NULL;
wc.lpszClassName = szName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, TEXT("Cannot register class"), TEXT("Error"), MB_OK);
}
}
// Draw custom button
void DrawButton(DRAWITEMSTRUCT *Item, HWND hwnd)
{
SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial Black"));
FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(RGB(0x14, 0x14, 0x14)));
SelectObject(Item->hDC, CreateSolidBrush(0));
if (Item->itemState & ODS_SELECTED)
{
SetTextColor(Item->hDC, 0);
SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0xFF00));
}
else
{
SetTextColor(Item->hDC, 0x00FF00);
SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
}
SetBkMode(Item->hDC, TRANSPARENT);
RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
int len = GetWindowTextLength(Item->hwndItem);
LPSTR lpBuff[255];
GetWindowTextA(Item->hwndItem, lpBuff, len + 1);
DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
}
// Trying to draw customgroupbox
void DrawKeyGroup(DRAWITEMSTRUCT *Item, HWND hwnd)
{
SelectObject(Item->hDC, CreateSolidBrush(RGB(0, 255, 0)));
SelectObject(Item->hDC, CreateSolidBrush(0));
SetTextColor(Item->hDC, 0x00FF00);
SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
MessageBox(NULL, "", "", MB_OK);
//SetBkMode(Item->hDC, TRANSPARENT);
RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 200, 200);
}
//Main windows proc
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
RECT rect;
switch (uMsg)
{
case WM_CREATE: // Init interface
// Text field
CreateWindow(TEXT("EDIT"),
NULL,
WS_VISIBLE | WS_CHILD | ES_LEFT | ES_AUTOHSCROLL,
10, 280, 350, 20,
hWnd,
(HMENU)ID_KEY_FIELD,
NULL,
NULL);
// Button to enter the text
CreateWindowEx(0, TEXT("BUTTON"),
TEXT("Unlock"),
WS_VISIBLE | WS_CHILD | BS_OWNERDRAW,
370, 280, 150, 20,
hWnd,
(HMENU)ID_KEY_ENTER,
GetModuleHandle(NULL),
NULL);
// Groupbox
CreateWindowEx(0, TEXT("BUTTON"),
TEXT("Key"),
WS_VISIBLE | WS_CHILD | BS_GROUPBOX | BS_OWNERDRAW,
5, 262, 523, 47,
hWnd,
(HMENU)ID_KEY_GROUP,
GetModuleHandle(NULL),
NULL);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
EndPaint(hWnd, &ps);
break;
// Color text field
case WM_CTLCOLOREDIT:
{
HDC hdc = (HDC)wParam;
HWND hwnd = (HWND)lParam;
if (GetDlgCtrlID(hwnd) == ID_KEY_FIELD)
{
SetTextColor(hdc, 0xFF00);
SetBkColor(hdc, RGB(0x29, 0x29, 0x29));
return (LRESULT)CreateSolidBrush(RGB(0x29, 0x29, 0x29));
}
break;
}
// Color statie elements (group box caption)
/*case WM_CTLCOLORSTATIC:
SetTextColor((HDC)wParam, RGB(0x00, 0x8C, 0xBA));
SetBkMode((HDC)wParam, TRANSPARENT);
SetBkColor((HDC)wParam, RGB(0x14, 0x14, 0x14));
return (LRESULT)CreateSolidBrush(GetBkColor((HDC)wParam));*/
// Draw custom controls
case WM_DRAWITEM:
{
DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)lParam;
switch (dis->CtlID)
{
case ID_KEY_ENTER:
DrawButton(dis, GetDlgItem(hWnd, dis->CtlID));
break;
case ID_KEY_GROUP:
DrawKeyGroup(dis, GetDlgItem(hWnd, dis->CtlID));
break;
}
}
case WM_SIZE:
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
void ShowMainWindow(void)
{
HWND hMainWnd;
MSG msg;
RegisterWndClass(WndProc, TEXT("Info"), COLOR_WINDOW);
hMainWnd = CreateWindowEx(WS_EX_LAYERED, TEXT("Info"), TEXT("Info"),
WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, 550, 360, (HWND)NULL, (HMENU)NULL,
GetModuleHandle(NULL), NULL);
SetWindowLong(hMainWnd, GWL_EXSTYLE, GetWindowLong(hMainWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hMainWnd, 0, (255 * 70) / 100, LWA_ALPHA);
if (!hMainWnd)
{
MessageBox(NULL, TEXT("Can\'t create main window."), TEXT("Error"), MB_OK);
return;
}
ShowWindow(hMainWnd, SW_SHOW);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

WinAPI CreateWindow WS_POPUP resize flickering

I'm creating a window with no borders (WS_POPUP in CreateWindow) and need to resize it when mouse is over and change back when mouse is out of the application. It works OK, but it flickers when I change the position using SetWindowPos. I need to decrease y by 30 pixels and increase height, so the bottom of the window remains in the same position.
Below is the code:
WNDCLASS windowClass;
ZeroMemory(&windowClass, sizeof(WNDCLASS));
windowClass.style = 0;// CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = (WNDPROC) WindowProc;
windowClass.hInstance = hInst;
windowClass.lpszClassName = PROCESS_WINDOW_CLASS;
windowClass.hIcon = NULL;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
windowHandle = CreateWindowEx(
0,
PROCESS_WINDOW_CLASS,
L"",
WS_POPUP | WS_CLIPSIBLINGS,
x, //x
y, //y
width, //width
height, //height
NULL,
NULL,
hInst,
NULL);
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_MOUSEMOVE: {
if (!mouseOver) {
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hwnd;
if (TrackMouseEvent(&tme)) {
RECT rect;
GetWindowRect(hwnd, &rect);
int y = rect.top;
int height = rect.bottom - y;
y -= BUTTONS_AREA_HEIGHT;
height += BUTTONS_AREA_HEIGHT;
//tried with MoveWindow, DeferWindowPos - same thing
//MoveWindow(hwnd, rect.left, y, rect.right - rect.left, height, TRUE);
//HDWP winPos = BeginDeferWindowPos(1);
//DeferWindowPos(winPos, hwnd, NULL, rect.left, y, rect.right - rect.left, height, 0);
//EndDeferWindowPos(winPos);
SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW | SWP_NOSENDCHANGING);
rect.top = y;
rect.bottom = height - y;
InvalidateRect(hwnd, &rect, FALSE);
//0);
RedrawWindow(hwnd, NULL, NULL, RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_NOERASE);
mouseOver = TRUE;
}
}
}
break;
case WM_MOUSELEAVE: {
RECT rect;
GetWindowRect(hwnd, &rect);
int y = rect.top;
int height = rect.bottom - y;
y += BUTTONS_AREA_HEIGHT;
height -= BUTTONS_AREA_HEIGHT;
MoveWindow(hwnd, rect.left, y, rect.right - rect.left, height, TRUE);
//SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height,
// SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
//InvalidateRect(hwnd, &rect, FALSE);
//SetWindowPos(hwnd, NULL, rect.left, y, rect.right - rect.left, height, 0);
mouseOver = FALSE;
}
case WM_CREATE: {
LONG lExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
lExStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE |
WS_SYSMENU | WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
SetWindowLongPtr(hwnd, GWL_EXSTYLE, lExStyle | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
return 0;
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
I tried all combinations of SetWindowPos, MoveWindow etc but when mouse is over the window, it seems that window is first settings Y position, then it increases the height after a few milliseconds and this is visible.
Does anyone know a solution for this?
I fixed the problem by using the SetWindowRgn function.
This still doesn't answer the question why flickering happens, but it fixed the problem for me as I was able to achieve the same thing. Code is below:
HRGN region = CreateRectRgn(0, BUTTONS_AREA_HEIGHT, width, height);
SetWindowRgn(windowHandle, region, TRUE);
DeleteObject(region);

Trackbar control disappearing on resize

I'm trying to write a WinAPI wrapper using C++11 but I've encountered a problem.
My tracksbars seem to go invisible whenever I resize the main window ( this problem does not occur with buttons/labels/progressbars/comboboxes/etc)
void Trackbar::Create(Window* parent)
{
this->handle = CreateWindowEx(
0,
TRACKBAR_CLASS
nullptr,
WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE | TBS_TRANSPARENTBKGND,
170,
150,
100,
50,
parent->GetHandle(),
0,
GetModuleHandle(0),
nullptr);
if(!this->handle)
ERRORMB();
SendMessage(this->handle, TBM_SETRANGE, true, (LPARAM)MAKELONG(0, 100));
SendMessage(this->handle, TBM_SETPAGESIZE, 0, 4);
SendMessage(this->handle, TBM_SETSEL, false, (LPARAM)MAKELONG(0, 100));
SendMessage(this->handle, TBM_SETPOS, true, 0);
}
Why is this happening and how can I fix it?

My code which uses UpdateLayeredWindow doesn't work

I attampted to draw a irregular window with the UpdateLayeredWindow(), in msvc2008, xp sp3.
Here is part of my code:
//Add something(CreateWindowEx()):
hwndCyauWnd = CreateWindowEx(
/*WS_EX_TOOLWINDOW |*/ WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_LAYERED,
lpwsCyauClassName,
lpwsCyauWndName,
WS_CLIPSIBLINGS | WS_POPUP,
GetSystemMetrics(SM_CXSCREEN)-320,
GetSystemMetrics(SM_CYSCREEN)-232,
320, 200,
NULL,
NULL,
hInstance,
NULL);
//Skip Lines
HDC hdcCyauWnd = GetDC(hwndCyauWnd);
HDC hdcBuffer = CreateCompatibleDC(hdcCyauWnd);
//HBITMAP hbmCyau = CreateCompatibleBitmap(hdcBuffer,120, 93);
//SelectObject(hdcBuffer, hbmCyau);
POINT ptZero = {0, 0};
POINT ptDrawPos = {0, 0};
RECT rctCyauWnd;
GetWindowRect(hwndCyauWnd, &rctCyauWnd);
SIZE szCyauWnd={rctCyauWnd.right - rctCyauWnd.left, rctCyauWnd.bottom - rctCyauWnd.top};
BLENDFUNCTION blendPixelFunction = { AC_SRC_OVER, 0, 100, AC_SRC_ALPHA};
Graphics gphCyauWnd(hdcBuffer);
Image imgCyau(L"surface0000.png");
gphCyauWnd.DrawImage(&imgCyau, 0, 0, 125, 93);
UpdateLayeredWindow(hwndCyauWnd,
hdcCyauWnd, &ptZero,
&szCyauWnd,
hdcBuffer, &ptZero,
0, //RGB(255, 255, 255),
&blendPixelFunction,
ULW_ALPHA);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
I have tried several method to use this function, but all failed, NOTHING APPEAR on the screen.
Could anybody tell me what happens and how to slove it?
Add:
Whole source file have been upload to my skydrive, anyone can edit, much appreciation! (I have become a poor underdog now...)
You mixed up GDI and GDI+, which is not a good idea. Here is a working example:
hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
// Load PNG
CImage img;
img.Load("BACKGR.png");
// Get image sizes
int nWidth = img.GetWidth();
int nHeight = img.GetHeight();
// Create memory DC
HDC hdcScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hdcScreen);
// Create memory bitmap
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, nWidth, nHeight);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
// Draw image to memory bitmap (currently selected in memory DC)
img.Draw(hDC, 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight);
// Call UpdateLayeredWindow
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 128;// half transparent
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptLocation = {0, 0};
SIZE szWnd = {nWidth, nHeight};
POINT ptSrc = {0, 0};
UpdateLayeredWindow(hWnd, hdcScreen, &ptLocation, &szWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
ShowWindow(hWnd, SW_SHOW);
SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
ReleaseDC(NULL, hdcScreen);
If you want GDI+ to draw to an image with an alpha channel, you have to draw to a Bitmap, not an HDC, and you have to specify that the Bitmap's format has alpha. To do that with an HBITMAP, you have to also point GDI+ to the bitmap bits.
Something like this:
BITMAPINFOHEADER bih;
HBITMAP hbmp;
HDC hdc;
void *bits;
bih.biSize = sizeof(bih);
bih.biWidth = width;
bih.biHeight = -height;
bih.biPlanes = 1;
bih.biBitCount = 32;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
hdc = CreateCompatibleDC(NULL);
hbmp = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits, NULL, 0);
Bitmap bitmap(width, height, 0, PixelFormat32bppPARGB, bits);
Graphics graphics(bitmap);
graphics->DrawWhatever(...);
graphics->Flush();
SelectObject(hdc, hbitmap);
UpdateLayeredWindow(hwnd, hdc, ...

Resources