I have created a custom window frame using DWM. The frame extends successfully but whenever I try to draw onto the frame, the extended frame coveres whatever I am trying to draw. I have seen other people try to input a top left within negative bounds, but even when I try to do that, the title bar still overlaps the main window's painting. Here is my code (note: i don't have any code for hit testing):
#include <Windows.h>
#include <numeric>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")
const auto s_brush = CreateSolidBrush(RGB(0, 0, 255));
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
LRESULT res;
if (DwmDefWindowProc(hwnd, msg, wparam, lparam, &res))
return res;
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
RECT r;
GetWindowRect(hwnd, &r);
SetWindowPos(hwnd, 0, 0, 0, r.right - r.left, r.bottom - r.top, SWP_FRAMECHANGED);
}
break;
case WM_ACTIVATE:
{
int metrics[4];
const auto window_dpi_ = GetDpiForWindow(hwnd);
metrics[0] = GetSystemMetricsForDpi(SM_CYCAPTION, window_dpi_);
metrics[1] = GetSystemMetricsForDpi(SM_CXFIXEDFRAME, window_dpi_);
metrics[2] = GetSystemMetricsForDpi(SM_CYSIZEFRAME, window_dpi_);
metrics[3] = GetSystemMetricsForDpi(SM_CYBORDER, window_dpi_);
const auto cy_titlebar_ = std::accumulate(metrics, metrics + sizeof metrics / sizeof(int), 0);
MARGINS margins{ 0, 0, cy_titlebar_, 0 };
DwmExtendFrameIntoClientArea(hwnd, &margins);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
const auto hdc = BeginPaint(hwnd, &ps);
const auto old = SelectObject(hdc, s_brush);
Rectangle(hdc, 0, 0, 50, 75);
SelectObject(hdc, old);
EndPaint(hwnd, &ps);
}
break;
case WM_NCCALCSIZE:
if (wparam == TRUE)
{
RECT& client_rect = reinterpret_cast<LPNCCALCSIZE_PARAMS>(lparam)->rgrc[0];
const auto window_dpi_ = GetDpiForWindow(hwnd);
const auto frame_width{ GetSystemMetricsForDpi(SM_CXFRAME, window_dpi_) };
const auto border_width{ GetSystemMetricsForDpi(SM_CXPADDEDBORDER, window_dpi_) };
const auto frame_height{ GetSystemMetricsForDpi(SM_CYFRAME, window_dpi_) };
client_rect.bottom -= frame_height + border_width;
client_rect.left += frame_width + border_width;
client_rect.right -= frame_width + border_width;
break;
}
default:
return DefWindowProcW(hwnd, msg, wparam, lparam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE, LPWSTR lpcmdline, int cmd_show)
{
WNDCLASS wc{ CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance,
0,0, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), 0, L"CustWnd" };
const auto hwnd = CreateWindow(MAKEINTATOM(RegisterClass(&wc)), L"Custom Window Frame", WS_OVERLAPPEDWINDOW,
0, 0, 500, 700, 0, 0, hinstance, 0);
ShowWindow(hwnd, cmd_show);
UpdateWindow(hwnd);
MSG msg;
while (GetMessageW(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}
The window class doesn't have a default cursor, it's going to show the wrong cursors as you move the mouse. Change wc to
WNDCLASS wc{ CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance, 0,
LoadCursor(NULL, IDC_ARROW),
reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), 0, L"CustWnd" };
WM_NCHITTEST should also be handled, otherwise title-bar will not grip. It is better to calculate the border thickness based on Windows style, or keep it as static value because it will be needed throughout the procedure, as well as title bar height.
Note that this code will look very different in Windows 10 versus Window 7 which has the weird transparent title-bar, you'll need 32-bit bitmap with alpha channel to draw on title-bar. Or use buffered paint with BufferedPaintSetAlpha as shown below
#include <Windows.h>
#include <Windowsx.h> //for `GET_X_LPARAM` etc.
...
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static int cy_titlebar_ = 100;
static RECT border_thickness;
LRESULT result;
if(DwmDefWindowProc(hWnd, msg, wParam, lParam, &result))
return result;
switch(msg)
{
case WM_CREATE:
{
//find border thickness
border_thickness = { 0 };
if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME)
{
AdjustWindowRectEx(&border_thickness,
GetWindowLongPtr(hWnd, GWL_STYLE) & ~WS_CAPTION, FALSE, NULL);
border_thickness.left *= -1;
border_thickness.top *= -1;
}
else if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_BORDER)
{
border_thickness = { 1,1,1,1 };
}
MARGINS margins = { 0, 0, cy_titlebar_, 0 };
DwmExtendFrameIntoClientArea(hWnd, &margins);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
return 0;
}
case WM_NCCALCSIZE:
{
if(wParam)
{
RECT& r = reinterpret_cast<LPNCCALCSIZE_PARAMS>(lParam)->rgrc[0];
r.left += border_thickness.left;
r.right -= border_thickness.right;
r.bottom -= border_thickness.bottom;
return 0;
}
break;
}
case WM_NCHITTEST:
{
result = DefWindowProc(hWnd, msg, wParam, lParam);
if(result == HTCLIENT)
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ScreenToClient(hWnd, &pt);
if(pt.y < border_thickness.top) return HTTOP;
if(pt.y < cy_titlebar_) return HTCAPTION;
}
return result;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(hWnd, &ps);
//paint opaque:
RECT rc{ 0, 0, 100, cy_titlebar_ };
BP_PAINTPARAMS params = { sizeof(params), BPPF_NOCLIP | BPPF_ERASE };
HDC memdc;
HPAINTBUFFER hbuffer = BeginBufferedPaint(
hdc, &rc, BPBF_TOPDOWNDIB, ¶ms, &memdc);
auto brush = CreateSolidBrush(RGB(255, 0, 0));
FillRect(memdc, &rc, brush);
DeleteObject(brush);
SetBkMode(memdc, TRANSPARENT);
DrawText(memdc, L"Opaque", -1, &rc, 0);
BufferedPaintSetAlpha(hbuffer, &rc, 255);
EndBufferedPaint(hbuffer, TRUE);
EndPaint(hWnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
Related
I am writing the WinAPI Application, and I tried to write a function, that would automate for me creating Rectangles for text input (with Font, BkMode, TextColor), that then is called by DrawContent function called by WM_PAINT. The text does not appear, after I call ELW::AddTextControl.
LRESULT CALLBACK ELW::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
{
ELW::DrawContent(hWnd, hInst, hdc);
}
break;
case WM_CLOSE:
{
DestroyWindow(hWnd);
UnregisterClass(CLASS_NAME, hInst);
}
break;
case WM_SYSCOMMAND:
{
if (wParam == SC_CLOSE);
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// Content drawing
void ELW::DrawContent(HWND hWnd, HINSTANCE hInst, HDC hdc)
{
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
RECT testowyRectangle = { 0, 1 , 20 , 30 };
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_GRAYTEXT + 1));
ELW::AddTextControl("Testowy test", 80, 80, 100, 100, CUSTOM_COLOR_YELLOW_COLORREF, 10, "Euroscope", hdc);
EndPaint(hWnd, &ps);
};
void ELW::AddTextControl(const char* text, int xPos, int yPos, int xSize, int ySize, COLORREF rgb, int fontSize, const char* fontName, HDC hdc)
{
RECT TextRectangle = { xPos, yPos, ( xPos + xSize ) , ( yPos + ySize ) }; // Left, Top, Right, Bottom
if (rgb == NULL)
rgb = RGB(0, 0, 0);
if (fontSize == NULL)
fontSize = 10;
if (fontName == NULL)
fontName = "Euroscope";
HFONT hFont = CreateFont(fontSize, 8, 0, 0, 600, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT(fontName));
SelectObject(hdc, hFont);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, rgb);
DrawText(hdc, TEXT(text), strlen(text), &TextRectangle, DT_CENTER | DT_NOCLIP);
}
Found an error:
strlen(text)
should be
strlen(TEXT(text))
I found this code in stackoverflow and it really draws a rectangle on a transparent window using a bitmap. But somehow I cannot change the transparency of the rectangle. More precisely, I can, but it gets darker. As if the bitmap itself has a black background. How do I make the rectangle transparent?
void paintRect(HDC hdc, RECT dim, COLORREF penCol, COLORREF brushCol, int opacity)
{
HDC tempHDC = CreateCompatibleDC(hdc);
BITMAPINFO bitmapInfo;
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = dim.right - dim.left;
bitmapInfo.bmiHeader.biHeight = dim.bottom - dim.top;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = (dim.right - dim.left) * (dim.bottom - dim.top) * 4;
HBITMAP hBitmap = CreateDIBSection(tempHDC, &bitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0x0);
SelectObject(tempHDC, hBitmap);
SetDCPenColor(tempHDC, RGB(0, 0, 255));
SetDCBrushColor(tempHDC, RGB(0, 0, 255));
FillRect(tempHDC, &dim, CreateSolidBrush(RGB(0, 0, 255)));
BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
AlphaBlend(hdc, dim.left, dim.top, dim.right, dim.bottom, tempHDC, dim.left, dim.top, dim.right, dim.bottom, blend);
}
Full code
#include <Windows.h>
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
WNDCLASS windowClass {};
windowClass.lpfnWndProc = windowProc;
windowClass.hInstance = hInstance;
windowClass.lpszClassName = L"Keystrokes";
windowClass.style = CS_NOCLOSE;
windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
if (!RegisterClass(&windowClass))
{
return 0;
}
HWND hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, L"Keystrokes", L"Keystrokes", WS_POPUP, 0, 0, 148, 140, 0, 0, hInstance, 0);
if (!hwnd)
{
return 0;
}
SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY);
ShowWindow(hwnd, nCmdShow);
MSG msg {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void paintRect(HDC hdc, RECT dim, COLORREF penCol, COLORREF brushCol, int opacity)
{
HDC tempHDC = CreateCompatibleDC(hdc);
BITMAPINFO bitmapInfo;
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = dim.right - dim.left;
bitmapInfo.bmiHeader.biHeight = dim.bottom - dim.top;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = (dim.right - dim.left) * (dim.bottom - dim.top) * 4;
HBITMAP hBitmap = CreateDIBSection(tempHDC, &bitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0x0);
SelectObject(tempHDC, hBitmap);
SetDCPenColor(tempHDC, RGB(0, 0, 255));
SetDCBrushColor(tempHDC, RGB(0, 0, 255));
FillRect(tempHDC, &dim, CreateSolidBrush(RGB(0, 0, 255)));
BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
AlphaBlend(hdc, dim.left, dim.top, dim.right, dim.bottom, tempHDC, dim.left, dim.top, dim.right, dim.bottom, blend);
}
LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDOWN:
{
SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
break;
case WM_MBUTTONDOWN:
{
PostQuitMessage(0);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
paintRect(hDC, { 0, 0, 48, 48 }, RGB(255, 0, 0), RGB(255, 255, 255), 255);
EndPaint(hwnd, &ps);
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
“As if the bitmap itself has a black background.”
Because your background is set to:
windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
Of course you can set it to NULL_BRUSH to make it look transparent:
windowClass.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
But when you move it, you will find that this is not to make the window transparent, but to stop drawing the background:
As #Ben answers here:
You haven't made the window transparent, you have just stopped drawing the background. What you see as the background is just whatever happened to be underneath the window when it was first drawn.
And you need to refer to Layered Windows
You can't add rectangles directly on the window, you should use a layered window, and then add what you need.
Here is a sample that you can refer to it:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("test window");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, szAppName,
TEXT("the hello program"),
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
148,
148,
NULL,
NULL,
hInstance,
NULL);
SetLayeredWindowAttributes(hwnd, 0, 1, LWA_ALPHA);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
VOID FadeRect(RECT* prc, HDC hdc)
{
BOOL fFade = FALSE;
static HWND hwnd;
SIZE size;
POINT ptSrc = { 0, 0 };
BLENDFUNCTION blend;
SystemParametersInfo(SPI_GETSELECTIONFADE, 0, &fFade, 0);
if (!fFade)
return;
if (!hwnd) hwnd = CreateWindowEx(WS_EX_LAYERED |
WS_EX_TRANSPARENT |
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
L"static", L"static", WS_POPUP | WS_VISIBLE, prc->left,
prc->top, prc->right, prc->bottom, NULL, (HMENU)0, NULL, NULL);
else MoveWindow(hwnd, prc->left, prc->top, prc->right, prc->bottom, TRUE);
RECT rect{ prc->left,prc->top,prc->right,prc->bottom };
size.cx = prc->right - prc->left;
size.cy = prc->bottom - prc->top;
blend.BlendOp = AC_SRC_OVER;
blend.BlendFlags = 0;
blend.AlphaFormat = 0;
blend.SourceConstantAlpha = 150;
UpdateLayeredWindow(hwnd, NULL, NULL, &size, hdc, &ptSrc, 0,
&blend, ULW_ALPHA);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
{
SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case WM_MBUTTONDOWN:
{
PostQuitMessage(0);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
GetClientRect(hwnd, &rect);
MapWindowPoints(hwnd, GetParent(hwnd), (LPPOINT)&rect, 2);
HDC hDC = BeginPaint(hwnd, &ps);
RECT rc{ rect.left,rect.top,rect.left + 48,rect.top + 48 };
FadeRect(&rc, hDC);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
I want to draw on a transparent window using a pen.
When drawing a line black area surround the line.
This image shows the problem:
How to solve this problem?
LRESULT __stdcall WindowProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
HDC hdc, backDC;
PAINTSTRUCT ps;
static Point prevPt;
// Draw or Erase
static bool isDraw = false;
static bool isErase = false;
// Select Pen Color
static int selectColor = 1;
// Color Pen(R, G, B) and Current Pen
static HPEN redPen;
static HPEN greenPen;
static HPEN bluePen;
static HPEN* currentPen = &redPen;
switch (iMessage)
{
case WM_CREATE:
{
redPen = CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
greenPen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0));
bluePen = CreatePen(PS_SOLID, 4, RGB(0, 0, 255));
return 0L;
}
case WM_DESTROY:
cout << "\n" << "destroying window" << endl;
PostQuitMessage(0);
return 0L;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0L;
case WM_LBUTTONDOWN:
prevPt.x = LOWORD(lParam);
prevPt.y = HIWORD(lParam);
isDraw = true;
return 0L;
case WM_LBUTTONUP:
isDraw = false;
return 0L;
case WM_MOUSEMOVE:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
if (isDraw)
{
hdc = GetDC(g_hWnd);
HPEN OldPen = (HPEN)SelectObject(hdc, *currentPen);
MoveToEx(hdc, prevPt.x, prevPt.y, NULL);
LineTo(hdc, x, y);
prevPt.x = x;
prevPt.y = y;
DeleteObject(OldPen);
ReleaseDC(g_hWnd, hdc);
}
}
return 0L;
case WM_RBUTTONDOWN:
isErase = true;
return 0L;
case WM_RBUTTONUP:
isErase = false;
return 0L;
case WM_MOUSEWHEEL:
if (selectColor > 3)
selectColor = 1;
if (selectColor == 1) // Red
currentPen = &redPen;
else if (selectColor == 2)
currentPen = &greenPen;
else if (selectColor == 3)
currentPen = &bluePen;
selectColor++;
return 0L;
}
return DefWindowProc(hWnd, iMessage, wParam, lParam);
}
void main()
{
HWND window;
LPCWSTR myclass = L"DrawTest";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_VREDRAW | CS_HREDRAW, WindowProc,
0, 0, NULL, LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW), (HBRUSH)WHITE_BRUSH, 0, myclass, LoadIcon(0,IDI_APPLICATION) };
if (RegisterClassEx(&wndclass))
{
window = CreateWindowEx(WS_EX_TRANSPARENT, myclass, L"title", WS_POPUP, 0, 0, GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN), 0, 0, NULL, 0);
}
VideoCapture* pCapture = nullptr;
pCapture = new VideoCapture(0);
if (pCapture)
{
if (!pCapture->isOpened())
{
cout << "Can not open video file." << endl;
return;
}
int fps = (int)(pCapture->get(CAP_PROP_FPS));
int delay = 0;
if (fps == 0)
fps = 24;
delay = 1000 / fps;
Mat colorMat;
while (1)
{
*pCapture >> colorMat;
if (colorMat.empty())
break;
Mat copyColor;
colorMat.copyTo(copyColor);
imshow("colorMat", copyColor);
int ckey = waitKey(delay);
if (ckey == 27)
break;
if (window)
{
ShowWindow(window, SW_SHOW);
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
GetMessage(&msg, 0, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
cv::destroyAllWindows();
}
}
As I said in my comment, create a layered window:
window = CreateWindowEx(WS_EX_LAYERED, myclass, L"title", WS_POPUP, 0, 0,
GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN),
HWND_DESKTOP, NULL, NULL, NULL);
set color transparency the same as your background brush:
SetLayeredWindowAttributes(window, RGB(255, 255, 255), 0, LWA_COLORKEY);
In WM_PAINT
hdc = BeginPaint(hwnd, &ps);
HPEN OldPen = (HPEN)SelectObject(hdc, *currentPen);
//set random values
MoveToEx(hdc, 50, 50, NULL);
LineTo(hdc, 450, 450);
SelectObject(hdc, OldPen);
EndPaint(hwnd, &ps);
return 0;
This code works, BUT you cant get mouse messages cause the window is transparent. That is the main issue, not the drawing.
EDIT
The problem is how to get mouse messages. The solution is to create a second window on top of your main window with opacity nearly zero so it is not visible but get the mouse messages!
window = CreateWindowEx(WS_EX_LAYERED, myclass, L"title", WS_POPUP, 0, 0,
GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN),
HWND_DESKTOP, NULL, NULL, NULL);
windowClone = CreateWindowEx(WS_EX_LAYERED, myclass, L"title", WS_POPUP, 0, 0,
GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN),
window, NULL, NULL, NULL);
Make your main window complete transparent:
//background color MUST be the same with color Key!
SetLayeredWindowAttributes(window, RGB(255, 255, 255), 0, LWA_COLORKEY);
Make nearly transparent your clone window
//The transparency is set to 1
SetLayeredWindowAttributes(windowClone, 0, 1, LWA_ALPHA);
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
static int draw = FALSE, startX, startY, endX, endY, posX, posY;
switch(message){ //handle the messages
case WM_PAINT:
printf("WM_PAINT \n");
if( hwnd == window && draw == TRUE ){
HPEN OldPen, redPen;
redPen = CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
hdc = BeginPaint(hwnd, &ps);
OldPen = (HPEN)SelectObject(hdc, redPen);
MoveToEx(hdc, startX, startY, NULL);
LineTo(hdc, endX, endY);
SelectObject(hdc, OldPen);
EndPaint(hwnd, &ps);
DeleteObject(redPen);
return 0;
}
break;
case WM_MOUSEMOVE:
//printf("WM_MOUSEMOVE \n");
if( hwnd == windowClone && draw == TRUE ){
startX = posX;
startY = posY;
endX = GET_X_LPARAM(lParam);
endY = GET_Y_LPARAM(lParam);
posX = endX;
posY = endY;
InvalidateRect(window, NULL, FALSE);
}
break;
case WM_LBUTTONDOWN:
printf("WM_LBUTTONDOWN \n");
if( hwnd == windowClone ){
posX = GET_X_LPARAM(lParam);
posY = GET_Y_LPARAM(lParam);
draw = TRUE;
}
break;
case WM_LBUTTONUP:
printf("WM_LBUTTONUP \n");
if( hwnd == windowClone && draw == TRUE ){
draw = FALSE;
}
break;
case WM_CAPTURECHANGED:
printf("WM_CAPTURECHANGED \n");
if( hwnd == windowClone && draw == TRUE ){
draw = FALSE;
}
break;
default: //for messages that we don't deal with
return DefWindowProc(hwnd, message, wParam, lParam);
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
i'm writing a program with the Win32 API just to get a hang of it and have come across this annoying problem. After a little while of resizing the window minimizes and after bringing it up again it's visible but non-clickable and clicking on it just activates what's on the window below it. The only solution being exiting the program.
At first i thought it was because of my custom-made function for sizing the window (i use a custom gui and don't want the default windows sizebox border) but then i reactivated the default sizebox and the problem still persisted. It's hard to tell but the problem seems to consistently occur after roughly the same amount of time/ticks being resized.
I had a similar problem before where the window would minimize and then becoming completely white when brought up again, which for some reason was caused by a for-loop in the resizing function.
I have excluded as many potential problems as possible in the code but still haven't found a solution. Below is the source file where all the code handling how the window is drawn lies (with the default sizebox).
I appreciate any help i can get.
Ps. I apologize for any bad language (non-native speaker), incorrectly used terms or bad syntax (first time venturing this far into the API).
//WinMain.cpp
#include <Windows.h>
#include "Utility.h"
#include "Mouse.h"
#include "AppInfo.h"
#include "Buttons.h"
//Function prototypes
ATOM MainRegister();
bool MainInit(HWND &hWnd, int nCmdShow);
void MatchRectToWnd(RECT &rect);
//Variables define in AppInfo.h
HRGN rgnMain, rgnCaptionbar;
bool _APPRUNNING = true;
const char _APPTITLE[] = "Dark";
//Variables
bool WIREFRAME = false;
//Pointers to buttons (singelton design)
btnCloseClass * btnCloseClass::s_Instance = 0;
btnMaximizeClass * btnMaximizeClass::s_Instance = 0;
btnMinimizeClass * btnMinimizeClass::s_Instance = 0;
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_LBUTTONDBLCLK: //Left mouse button double-clicked
Mouse.CheckDblClick(hWnd, wParam);
Mouse.m_Pressed = false;
break;
case WM_LBUTTONDOWN: //Left mouse button clicked
//Update the mouse position variables
GetCursorPos(&Mouse.prevPt);
GetCursorPos(&Mouse.m_LastClick);
Mouse.m_Pressed = true;
Mouse.CheckClickDown(hWnd);
break;
case WM_LBUTTONUP: //Left mouse button released
{
GetCursorPos(&Mouse.prevPt);
Mouse.CheckClickUp(hWnd);
Mouse.m_Pressed = false;
}
break;
case WM_SIZE: //Check if the window has been resized
{
//Update the buttons
btnClose->Update(hWnd);
btnMaximize->Update(hWnd);
btnMinimize->Update(hWnd);
//Update the regions
RECT rect; GetWindowRect(hWnd, &rect);
rgnMain = CreateRectRgn(0, 0, rect.right - rect.left, rect.bottom- rect.top);
rgnCaptionbar = CreateRectRgn(0, 0, rect.right - rect.left, CAPTIONBAR_HEIGHT);
}
break;
case WM_PAINT: //Draw the window
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
hdc = BeginPaint(hWnd, &ps);
//Color the mainregion
hBrush = CreateSolidBrush(COLOR_MAIN);
FillRgn(hdc, rgnMain, hBrush);
//Color the captionbarregion
hBrush = CreateSolidBrush(COLOR_CAPTIONBAR);
FillRgn(hdc, rgnCaptionbar, hBrush);
//Color the button backgrounds
hBrush = CreateSolidBrush(COLOR_BUTTON_BACKGROUND);
FillRgn(hdc, btnClose->GetRegion(), hBrush);
FillRgn(hdc, btnMinimize->GetRegion(), hBrush);
FillRgn(hdc, btnMaximize->GetRegion(), hBrush);
//Color the button icons
hBrush = CreateSolidBrush(COLOR_BUTTON_ICON);
FillRgn(hdc, btnClose->GetIcon(), hBrush);
FillRgn(hdc, btnMinimize->GetIcon(), hBrush);
FillRgn(hdc, btnMaximize->GetIcon(), hBrush);
//Paint the wireframe
if (WIREFRAME)
{
hBrush = CreateSolidBrush(COLOR_WIREFRAME);
FrameRgn(hdc, rgnMain, hBrush, 1, 1);
FrameRgn(hdc, rgnCaptionbar, hBrush, 1, 1);
FrameRgn(hdc, btnClose->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnMaximize->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnMinimize->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnClose->GetIcon(), hBrush, 1, 1);
FrameRgn(hdc, btnMaximize->GetIcon(), hBrush, 1, 1);
FrameRgn(hdc, btnMinimize->GetIcon(), hBrush, 1, 1);
}
//Free up memomry
DeleteObject(hBrush);
EndPaint(hWnd, &ps);
}
break;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_TAB: //If TAB is pressed
{
if (WIREFRAME) //Activate the wireframe
WIREFRAME = false;
else
WIREFRAME = true;
InvalidateRgn(hWnd, NULL, true);
}
break;
case VK_ESCAPE: //If the ESCAPE is pressed
PostMessage(hWnd, WM_DESTROY, 0, 0);
break;
}
}
break;
case WM_DESTROY: //Free up memory and exit the program
_APPRUNNING = false;
DeleteObject(rgnMain);
DeleteObject(rgnCaptionbar);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg;
_APPRUNNING = true;
//Register the main window
if (!MainRegister())
{
MessageBox(hWnd, "Error registering main window!", "Error", MB_ICONERROR);
return false;
}
//Initialize the main window
MainInit(hWnd, nCmdShow);
//App-loop
while (_APPRUNNING)
{
if ((GetKeyState(VK_LBUTTON) & 0x80) == 0) //Make sure the mouse's status gets updated
{
Mouse.m_Pressed = false;
}
Mouse.Update(hWnd);
//Message-loop
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
bool MainInit(HWND &hWnd, int nCmdShow)
{
//Create the window
hWnd = CreateWindowEx(
WS_EX_LAYERED,
_APPTITLE,
_APPTITLE,
WS_OVERLAPPEDWINDOW,
0, 0, //Starting positons (x,y)
START_WIDTH, START_HEIGHT, //Width and height
NULL, //Parent-handle
NULL, //Menu-handle
GetModuleHandle(NULL),
NULL);
//Make sure the window was created properly
if (hWnd == NULL)
{
MessageBox(hWnd, "Error initializing main window!", "Error!", MB_ICONERROR);
return false;
}
SetLayeredWindowAttributes(hWnd, NULL, NULL, NULL);
//Get and set the window's style
DWORD wndStyle = GetWindowLong(hWnd, GWL_STYLE);
wndStyle &= ~(WS_CAPTION);
SetWindowLong(hWnd, GWL_STYLE, wndStyle);
//Create regions
rgnMain = CreateRectRgn(0, 0, START_WIDTH, START_HEIGHT);
rgnCaptionbar = CreateRectRgn(0, 0, START_WIDTH, CAPTIONBAR_HEIGHT);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return true;
}
ATOM MainRegister()
{
//Create the window's classEx (extended class)
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = MainWndProc;
wc.lpszClassName = _APPTITLE;
wc.lpszMenuName = NULL;
wc.style = CS_DBLCLKS;
//Register the classEx
return RegisterClassEx(&wc);
}
As #CodyGray suggested here's the working code aswell as the class and the functions dealing with freeing up memory for the region and now brushes too.
Edit: The problem was caused by a memory leak in WM_PAINT when creating, and not deleting, brushes (HBRUSH).
//WinMain.cpp
#include <Windows.h>
#include "Utility.h"
#include "Mouse.h"
#include "AppInfo.h"
#include "Buttons.h"
//Function prototypes
ATOM MainRegister();
bool MainInit(HWND &hWnd, int nCmdShow);
void MatchRectToWnd(RECT &rect);
//Variables define in AppInfo.h
HRGN rgnMain, rgnCaptionbar;
bool _APPRUNNING = true;
const char _APPTITLE[] = "Dark";
//Variables
bool WIREFRAME = false;
//Pointers to buttons (singelton design)
btnCloseClass * btnCloseClass::s_Instance = 0;
btnMaximizeClass * btnMaximizeClass::s_Instance = 0;
btnMinimizeClass * btnMinimizeClass::s_Instance = 0;
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_LBUTTONDBLCLK: //Left mouse button double-clicked
Mouse.CheckDblClick(hWnd, wParam);
Mouse.m_Pressed = false;
break;
case WM_LBUTTONDOWN: //Left mouse button clicked
//Update the mouse position variables
GetCursorPos(&Mouse.prevPt);
GetCursorPos(&Mouse.m_LastClick);
Mouse.m_Pressed = true;
Mouse.CheckClickDown(hWnd);
break;
case WM_LBUTTONUP: //Left mouse button released
{
GetCursorPos(&Mouse.prevPt);
Mouse.CheckClickUp(hWnd);
Mouse.m_Pressed = false;
}
break;
case WM_SIZE: //Check if the window has been resized
{
DeleteObject(rgnMain);
DeleteObject(rgnCaptionbar);
//Update the buttons
btnClose->Update(hWnd);
btnMaximize->Update(hWnd);
btnMinimize->Update(hWnd);
//Update the regions
RECT rect; GetWindowRect(hWnd, &rect);
rgnMain = CreateRectRgn(0, 0, rect.right - rect.left, rect.bottom- rect.top);
rgnCaptionbar = CreateRectRgn(0, 0, rect.right - rect.left, CAPTIONBAR_HEIGHT);
}
break;
case WM_PAINT: //Draw the window
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
hdc = BeginPaint(hWnd, &ps);
//Color the mainregion
hBrush = CreateSolidBrush(COLOR_MAIN);
FillRgn(hdc, rgnMain, hBrush);
DeleteObject(hBrush);
//Color the captionbarregion
hBrush = CreateSolidBrush(COLOR_CAPTIONBAR);
FillRgn(hdc, rgnCaptionbar, hBrush);
DeleteObject(hBrush);
//Color the button backgrounds
FillRgn(hdc, btnClose->GetRegion(), btnClose->GetBrush());
FillRgn(hdc, btnMinimize->GetRegion(), btnMinimize->GetBrush());
FillRgn(hdc, btnMaximize->GetRegion(), btnMaximize->GetBrush());
//Color the button icons
FillRgn(hdc, btnClose->GetIcon(), btnClose->GetBrushIcon());
FillRgn(hdc, btnMinimize->GetIcon(), btnMinimize->GetBrushIcon());
FillRgn(hdc, btnMaximize->GetIcon(), btnMaximize->GetBrushIcon());
//Paint the wireframe
if (WIREFRAME)
{
hBrush = CreateSolidBrush(COLOR_WIREFRAME);
FrameRgn(hdc, rgnMain, hBrush, 1, 1);
FrameRgn(hdc, rgnCaptionbar, hBrush, 1, 1);
FrameRgn(hdc, btnClose->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnMaximize->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnMinimize->GetRegion(), hBrush, 1, 1);
FrameRgn(hdc, btnClose->GetIcon(), hBrush, 1, 1);
FrameRgn(hdc, btnMaximize->GetIcon(), hBrush, 1, 1);
FrameRgn(hdc, btnMinimize->GetIcon(), hBrush, 1, 1);
DeleteObject(hBrush);
}
//Free up memomry
DeleteObject(hBrush);
EndPaint(hWnd, &ps);
}
break;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_TAB: //If TAB is pressed
{
if (WIREFRAME) //Activate the wireframe
WIREFRAME = false;
else
WIREFRAME = true;
InvalidateRgn(hWnd, NULL, true);
}
break;
case VK_ESCAPE: //If the ESCAPE is pressed
PostMessage(hWnd, WM_DESTROY, 0, 0);
break;
}
}
break;
case WM_DESTROY: //Free up memory and exit the program
_APPRUNNING = false;
DeleteObject(rgnMain);
DeleteObject(rgnCaptionbar);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg;
_APPRUNNING = true;
//Register the main window
if (!MainRegister())
{
MessageBox(hWnd, "Error registering main window!", "Error", MB_ICONERROR);
return false;
}
//Initialize the main window
MainInit(hWnd, nCmdShow);
//App-loop
while (_APPRUNNING)
{
if ((GetKeyState(VK_LBUTTON) & 0x80) == 0) //Make sure the mouse's status gets updated
{
Mouse.m_Pressed = false;
}
Mouse.Update(hWnd);
//Message-loop
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
bool MainInit(HWND &hWnd, int nCmdShow)
{
//Create the window
hWnd = CreateWindowEx(
WS_EX_LAYERED,
_APPTITLE,
_APPTITLE,
WS_OVERLAPPEDWINDOW,
0, 0, //Starting positons (x,y)
START_WIDTH, START_HEIGHT, //Width and height
NULL, //Parent-handle
NULL, //Menu-handle
GetModuleHandle(NULL),
NULL);
//Make sure the window was created properly
if (hWnd == NULL)
{
MessageBox(hWnd, "Error initializing main window!", "Error!", MB_ICONERROR);
return false;
}
SetLayeredWindowAttributes(hWnd, NULL, NULL, NULL);
//Get and set the window's style
DWORD wndStyle = GetWindowLong(hWnd, GWL_STYLE);
wndStyle &= ~(WS_CAPTION | WS_SIZEBOX);
SetWindowLong(hWnd, GWL_STYLE, wndStyle);
//Create regions
rgnMain = CreateRectRgn(0, 0, START_WIDTH, START_HEIGHT);
rgnCaptionbar = CreateRectRgn(0, 0, START_WIDTH, CAPTIONBAR_HEIGHT);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return true;
}
ATOM MainRegister()
{
//Create the window's classEx (extended class)
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = MainWndProc;
wc.lpszClassName = _APPTITLE;
wc.lpszMenuName = NULL;
wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
//Register the classEx
return RegisterClassEx(&wc);
}
//Button.h
//Header file for the class responsible for handling the memory of the
//regions and brushes
class Button
{
protected:
//Protected default constructor
Button();
//Protected default destructor
~Button();
//Protected variables
HRGN m_Rgn;
HRGN m_Icon;
HBRUSH m_hBrush;
HBRUSH m_hBrush_Icon;
bool m_Hovered;
public:
//Public functions
virtual void CreateIcon() = 0;
virtual void Update(HWND hWnd) = 0;
bool Clicked(POINT pt);
bool CheckIfHovered(HWND hWnd);
HRGN GetRegion() { return m_Rgn; }
HRGN GetIcon() { return m_Icon; }
HBRUSH GetBrush() { return m_hBrush; }
HBRUSH GetBrushIcon() { return m_hBrush_Icon; }
};
//Button.cpp
//Default constructor
Button::Button()
{
m_hBrush = CreateSolidBrush(COLOR_BUTTON_BACKGROUND);
m_hBrush_Icon = CreateSolidBrush(COLOR_BUTTON_ICON);
m_Hovered = false;
}
//Default destructor
Button::~Button()
{
//Free up memory
DeleteObject(m_Rgn);
DeleteObject(m_Icon);
DeleteObject(m_hBrush);
DeleteObject(m_hBrush_Icon);
}
Again, thanks to #CodyGray #theB and #AdrianMcCarthy !
I have the "simple" goal of drawing a bitmap with some transparency around it on the screen. That bit wasn't so hard:
#include <windows.h>
#include "BBKG.h"
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static int wH = 156;
static int wW = 166;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC mem0, mem1;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
mem0 = CreateCompatibleDC(0);
mem1 = CreateCompatibleDC(0);
SelectObject(mem0, hbmColour);
SelectObject(mem1, hbmMask);
SetBkColor(mem0, crTransparent);
BitBlt(mem1, 0, 0, bm.bmWidth, bm.bmHeight, mem0, 0, 0, SRCCOPY);
BitBlt(mem0, 0, 0, bm.bmWidth, bm.bmHeight, mem1, 0, 0, SRCINVERT);
DeleteDC(mem0);
DeleteDC(mem1);
return hbmMask;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
hInst = hInstance;
MSG msg;
HWND hwnd;
WNDCLASSW wc;
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = L"nope";
wc.hInstance = hInst;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
hwnd = CreateWindowW(wc.lpszClassName, L"",
WS_VISIBLE | WS_POPUP| WS_EX_TRANSPARENT,
100, 100, wW, wH, NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
//Workaround for focusables stealing my Esc key
if (msg.message == WM_KEYDOWN){
if (msg.wParam == VK_ESCAPE) {
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static int px;
static int py;
static HBITMAP bhbm;
static RECT nRect = { 0, 0, wW, wH };
switch (msg)
{
case WM_CREATE:
{
HWND bb = CreateWindowW(L"STATIC", L"",
WS_VISIBLE | WS_CHILD ,
0, 0, wW, wH,
hwnd, (HMENU)11, hInst, NULL);
//SetTimer(hwnd, 1, 80, NULL);
return 0;
}
case WM_PAINT: {
//Vars
RECT wRect;
if (GetUpdateRect(hwnd, &wRect, FALSE) == 0) {
return 0; //Nothing to paint
}
PAINTSTRUCT gps;
PAINTSTRUCT ps;
BeginPaint(hwnd, &gps);
HWND bb = GetDlgItem(hwnd, 11);
HDC bbhdc = BeginPaint(bb, &ps);
HDC mdc = CreateCompatibleDC(bbhdc);
//Load Image
BITMAP pBM;
HBITMAP pHBM = (HBITMAP)LoadImage(NULL, L"twi00.bmp", 0, 0, 0, LR_LOADFROMFILE);
HBITMAP pMBM = CreateBitmapMask((HBITMAP)pHBM, 0x00000000);
GetObject(pHBM, sizeof(pBM), &pBM);
//Paint
HBITMAP oldBM = (HBITMAP)SelectObject(mdc, pMBM);
BitBlt(bbhdc, 0, 0, pBM.bmWidth, pBM.bmHeight, mdc, 0, 0, SRCAND);
SelectObject(mdc, pHBM);
BitBlt(bbhdc, 0, 0, pBM.bmWidth, pBM.bmHeight, mdc, 0, 0, SRCPAINT);
//Cleanup
SelectObject(mdc, oldBM);
DeleteObject(pHBM);
DeleteObject(pMBM);
DeleteDC(mdc);
EndPaint(bb, &ps);
EndPaint(hwnd, &gps);
return 1;
}
case WM_ERASEBKGND: {
return 0;
}
case WM_DESTROY:
{
DeleteObject(bhbm);
PostQuitMessage(0);
return 0;
}
case WM_LBUTTONDOWN:
SetCapture(hwnd);
px = LOWORD(lParam);
py = HIWORD(lParam);
return 1;
case WM_LBUTTONUP:
{
ReleaseCapture();
return 1;
}
case WM_MOUSEMOVE:
{
if (GetCapture() == hwnd)
{
RECT rcWindow;
GetWindowRect(hwnd, &rcWindow);
SetWindowPos(hwnd, NULL, rcWindow.left + LOWORD(lParam) - px, rcWindow.top + HIWORD(lParam) - py, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
break;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
Use any generic bmp with a black border will do, I used this:
Now the question is, how can I make it so that when I move the window (click/drag) the background updates? I was hoping for something like putting the bitmap into a transparent window so that it's overlayed on top of things but it seems to just grab the pixels of what ever is behind it.
I'm attempting to do this without GDI+ or other libraries, if possible.
CreateWindow() does not accept extended window styles, such as WS_EX_TRANSPARENT (which is why it has EX in its name). You have to use CreateWindowEx() instead:
hwnd = CreateWindowExW(WS_EX_TRANSPARENT,
wc.lpszClassName, L"",
WS_VISIBLE | WS_POPUP,
100, 100, wW, wH, NULL, NULL, hInst, NULL);
A better option is to create a layered window (see also this) by using the WS_EX_LAYERED extended style). Then you can use the UpdateLayeredWindow() function to provide the window with the bitmap and the transparent color (you can also specify alpha as well). Let the window manage all of the hard work of drawing the bitmap transparently for you.
Your WndProc() can also respond to the WM_NCHITTEST message to tell the OS that all clicks on the window should be treated as if the user were clicking on the window's titlebar. Let the window handle the mouse tracking and auto-positioning for you.
Try something more like this:
#include <windows.h>
HINSTANCE hInst;
static int wH = 156;
static int wW = 166;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
hInst = hInstance;
WNDCLASSW wc = {0};
wc.lpszClassName = L"nope";
wc.hInstance = hInst;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
HWND hwnd = CreateWindowEx(WS_EX_LAYERED,
wc.lpszClassName, L"",
WS_POPUP, 100, 100, wW, wH, NULL, NULL,
hInst, NULL);
HBITMAP hBmp = (HBITMAP) LoadImage(NULL, L"twi00.bmp", 0, 0, 0, LR_LOADFROMFILE);
HDC hdcScreen = GetDC(0);
HDC hdcBmp = CreateCompatibleDC(hdcScreen);
HBITMAP oldBM = (HBITMAP) SelectObject(hdcBmp, hBmp);
POINT pt = {0};
UpdateLayeredWindow(hwnd,
hdcScreen,
NULL, NULL,
hdcBmp, &pt,
RGB(0, 0, 0), // black
NULL, ULW_COLORKEY
);
SelectObject(hdcBmp, oldBM);
DeleteDC(hdcBmp);
ReleaseDC(0, hdcScreen);
DeleteObject(hBmp);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
//Workaround for focusables stealing my Esc key
if ((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE) {
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_NCHITTEST:
{
return HTCAPTION;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}