child window blinking - winapi

My application is dll which is injected into 3rd party GUI application. There dll creates child window containing few controls on the main window of the application. The problem is that my window and its controls blink when the main window is redrawn. I've already read many articles about similar problems but could not fix it. Here is some code
hwndContainer = CreateWindowEx( WS_EX_TOPMOST, PANEL_CLASS_NAME, "", WS_CHILD | WS_VISIBLE, 0, 0, width, height, hwnd, 0, g_hInstance, cd );
CreateWindowEx( WS_EX_TOPMOST, "button", "Click me", WS_CHILD | WS_VISIBLE | WS_TABSTOP, x, y, w, h, hwndContainer, 0, 0, 0 );
...
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwndPanel, &ps );
RECT r;
GetClientRect( hwndPanel, &r );
SelectObject( hdc, hpenBorder );
SelectObject( hdc, GetStockObject( NULL_BRUSH ) );
RoundRect( hdc, 0, 0, r.right, r.bottom, 5, 5 );
EndPaint( hwndPanel, &ps );
return 0;
I tried adding WS_CLIPCHILDREN to the main window style, processed WM_PAINT of the main window etc. Any thoughts?

You may give WS_CLIPSIBLINGS a try... It's been a long time since I've done anything with window styles directly but I seem to remember this style required to help reduce redraw flicker in non-top level windows. Good luck.

Related

ClientRect mysteriously smaller than WindowRect?

I started a new Windows Desktop C++ project in Visual Studio. The window creation looks like this:
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
Immediately I check the size of the window and client rects, and get the difference between them:
RECT windowRect;
RECT clientRect;
GetWindowRect(hWnd, &windowRect);
GetClientRect(hWnd, &clientRect);
int xExtra = windowRect.right - windowRect.left - clientRect.right;
xExtra is 16 (26 if my process is per monitor DPI aware).
What is accounting for this difference? The client area appears to be the same width as the window!
(It seems like the border may account for a few pixels, but not 26!)
What am I overlooking?

How to set size and position of a MDI Client?

It seems like both size (x, y) and position (nWidth, nHeight) arguments are ignored when using CreateWindow. For example:
CreateWindow(L"MDICLIENT", L"", WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
150, 10, 400, 300, hWnd, NULL, hInst, (LPVOID)&ccs);
It's always aligned to the top-left corner and takes the parent's size, as shown below.
(We could see the difference since the window background is COLOR_WINDOW).
The coordinates for MDICLIENT have no effect on start up. Instead you have to handle client-size in WM_SIZE as follows:
LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndClient;
switch (message)
{
case WM_CREATE:
{
hwndClient = CreateWindow(L"MDICLIENT", L"", WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
0, 0, 0, 0, hWnd, NULL, hInst, (LPVOID)&ccs);
...
return 0;
}
case WM_SIZE:
{
RECT rc;
GetClientRect(hwnd, &rc);
SetWindowPos(hwndToolbar, 0, 0, 0, rc.right, 30, SWP_SHOWWINDOW);
int x = 50; //right-edge of another toolbar...
int y = 30;
int w = rc.right - x;
int h = rc.bottom - y;
MoveWindow(hwndClient, x, y, w, h, 0);
return 0;
}
...
}
By the way, you won't really see any difference on the screen unless you add MDI child. The MDI child will limit its movements to the new area, it won't go over the toolbar.

Win32 can't get SetBkMode Transparency to work

I am writing a simple program, that prints some text on the screen, overlaying the other windows.
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
HWND hwnd = GetDesktopWindow();
HDC hdc;
RECT rect;
//LPRECT rect = new RECT;
wchar_t text[] = L"test";
GetClientRect(hwnd, &rect);
do{
hdc = GetWindowDC(hwnd);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(100, 100, 100));
DrawText(hdc, text, -1, &rect, DT_NOCLIP);
ReleaseDC(hwnd, hdc);
Sleep(15);
} while (1);
return 0;
}
The problem is that I would like the background of the printed text to be transparent, but SetBkMode does not seem to work (it actually makes no difference if I set it to OPAQUE or TRANSPARENT) so I get a solid background. Any ideas? What am I missing?
edit: Changed LPRECT to RECT, as suggested.
edit: using transparent window:
creating window:
CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED, // extended style
(LPCWSTR)WINDOW_CLASS_NAME, // class
L"test", // title
NULL,
0, 0, // initial x,y
400, 300, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL)
globals:
wchar_t tst_Str[] = L"TEST";
WM_PAINT:
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetTextColor(hdc, RGB(255, 0, 0));
DrawText(hdc, tst_Str, -1, &rc,NULL);
EndPaint(hwnd, &ps);
return 0;
Windows are responsible for painting themselves. The system is not designed to allow external parties to draw on other windows in the manner you are attempting. You will have to recalibrate your expectations.
If you wish to draw something you should create a window and draw on it. You are at liberty to make the window a transparent layered window and position your drawing so that it appears on top of another window. And you also gain the benefit that you don't need to run a busy loop. You can paint in the standard way, in response to WM_PAINT messages.
To make a layered window appear on the desktop, you have to call SetLayeredWindowAttributes function, for example
SetLayeredWindowAttributes(hWnd,
RGB(255, 255, 255), // The transparent color
128, // Opacity of the window
LWA_ALPHA | LWA_COLORKEY); // Enable both features
This example will make portions of the window with white color completely transparent. Parameter bAlpha specifies the opacity of the window (the pixels not affected with crKey).
Here's a screen shot of such window with Test text floating over browser window with this question:
If you create your window without border, using these window styles: WS_POPUP | WS_SYSMENU, then it will look like on the screen shot. I included WS_SYSMENU to have the standard window menu in the taskbar. To force the taskbar to display a button for popup window, include WS_EX_APPWINDOW extended style.

How can I make integer to display in window?

I want to have in window something like:
int a;
cout<<a;
but I don't know how I can do it. At the beginning, I want to have number displayed on screen and have a button which adds +1 and another button which adds -1 to this number. I want this number to be updated without next compilation. Do you know how I can do it?
I want it to be a prototype of simple calculator.
You can do your calculation in your corresponding button handler and set the "screen's" text with SetWindowText message.
The idea is following:
You have 2 buttons-one to add and one to subtract. You can create them in your WM_CREATE handler like this:
case WM_CREATE:
{
HWND btnAdd = CreateWindowEx( 0, L"Button",
L"+1", //this is the text for your adding button
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
50, 150, 150, 25, hWnd, (HMENU)8004, hInst, 0 );
HWND btnSubtract = CreateWindowEx( 0, L"Button",
L"-1", //this is the text for your adding button
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
50, 250, 150, 25, hWnd, (HMENU)8005, hInst, 0 );
// since you want "calculator type" application
// here is your result window-edit control
HWND input = CreateWindowEx( 0, L"Edit",
L"", // no need for text
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL,
50, 450, 150, 25, hWnd, (HMENU)8006, hInst, 0 );
HWND result = CreateWindowEx( 0, L"Edit",
L"", // no need for text
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY | ES_AUTOHSCROLL,
50, 450, 150, 25, hWnd, (HMENU)8007, hInst, 0 );
// other stuff
}
return 0L;
After user clicks on your buttons you set text of the result edit control with SetWindowText like this in your WM_COMMAND handler:
case 8004: // add 1 to the number
{
// get the number from input edit control
wchar_t temp[10];
GetWindowText( GetDlgItem( hWnd, 8006 ), temp, 10 );
//convert text to nubmer
int InputedNumber = wtoi( temp );
// show the result in the result edit control
memset( temp, L'\0', sizeof(temp) ); //reuse variable to save memory
swprintf_s( temp, 10, L"%d", InputNumber+1 ); //convert result to text
SetWindowText( GetDlgItem( hWnd, 8007 ), temp ); //show the result
}
case 8005: // subtract 1 to the number
{
// get the number from input edit control
wchar_t temp[10];
GetWindowText( GetDlgItem( hWnd, 8006 ), temp, 10 );
//convert text to number
int InputedNumber = wtoi( temp );
// show the result in the result edit control
memset( temp, L'\0', sizeof(temp) ); //reuse variable to save memory
swprintf_s( temp, 10, L"%d", InputNumber-1 ); //convert result to text
SetWindowText( GetDlgItem( hWnd, 8007 ), temp ); //show the result
}
Above were relevant code snippets for C++.
This might be a big bite for you to handle, so I recommend you to go through this beginner tutorial.
Good luck and best regards!

Drawing semitransparent child window with image on parent window

I need to make bird animation in WS_OVERLAPPED window (as shown below). Animation is represented by 8 images:
The blue color in the image (which is RGB(0, 255, 255)) has to be transparent (see screenshot below).
I wanted to do this using CreateWindowEx() (bird would be represented by layered window) with WS_EX_LAYERED argument. Unfortunately bird must be WS_CHILD. Mixing WS_EX_LAYERED | WS_CHILD is not legal in Windows 7:
Windows 8: The WS_EX_LAYERED style is supported for top-level windows and child windows. Previous Windows versions support WS_EX_LAYERED only for top-level windows.
Final effect should look like this (I've already painted window's background - the only problem is the bird):
How can I achieve this effect? How to animate bird in parent window?
If you have any idea how to implement bird animation with transparent background color please share.
Since animation is done even when there's no interaction with the window, we'll need a timer:
case WM_CREATE:
// load resources
SetTimer(hwnd, 0, 250, NULL); // set timer to 250 ms
return 0;
...
case WM_DESTROY:
KillTimer(hwnd, 0);
// release the resources
return 0;
We can invalidate the whole window each timer tick, but it would be better to redraw only needed part. We'll also update the current frame number here:
case WM_TIMER:
frame_number++;
if (frame_number >= 8)
frame_number = 0;
RECT rc = { 30, 30, 80, 80 }; // a rectangle from (30,30) to (80,80)
InvalidateRect(hwnd, &rc, FALSE);
return 0;
Then, we draw the current frame in the WM_PAINT handler:
case WM_PAINT:
// draw the sky
SelectObject(hDCMem, hBird);
TransparentBlt(hDC, 30, 30, 50, 50, hDCMem, frame_number * 51, 0, 50, 50, RGB(0, 255, 255)); // 51 is 50 (side of a bird frame) + 1 (gap between the frames)
// draw the rest
return 0;
I've finally find out how to do it. It's quite tricky.
The full description of the solution is available here - winprog.org/tutorial/transparency.html. For polish readers here is great translation.
Simple idea in brief:
Giving bitmaps the appearance of having transparent sections is quite simple, and involves the use of a black and white Mask image in addition to the colour image that we want to look transparent.
The following conditions need to be met for the effect to work correctly: First off, the colour image must be black in all areas that we want to display as transparent. And second, the mask image must be white in the areas we want transparent, and black elsewhere. The colour and mask images are displayed as the two left most images in the example picture on this page.
Simple solution in brief:
#define TRANSPARENCY_COLOR RGB(0, 255, 255)
birdBmp = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
hbmpMask = CreateBitmapMask(birdBmp, TRANSPARENCY_COLOR);
Painting:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
HDC birdMaskHdc = CreateCompatibleDC(hdc);
BITMAP bmInfo;
GetObject(birdBmp, sizeof(bmInfo), &bmInfo);
HBITMAP hbmpOld = (HBITMAP) SelectObject(birdMaskHdc, hbmpMask);
BitBlt(hdc, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, birdMaskHdc, 0, 0, SRCAND);
SelectObject(birdMaskHdc, birdBmp);
BitBlt(hdc, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, birdMaskHdc, 0, 0, SRCPAINT);
SelectObject(birdMaskHdc, hbmpOld);
DeleteDC(birdMaskHdc);
EndPaint(hWnd, &ps);
break;
}
Cleaning:
case WM_DESTROY:
{
DeleteObject(hbmpMask);
DeleteObject(birdBmp);
PostQuitMessage(0);
break;
}
Function which is responsible for creating bitmap mask:
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask, hbmOld, hbmOld2;
BITMAP bm;
GetObject( hbmColour, sizeof( BITMAP ), & bm );
hbmMask = CreateBitmap( bm.bmWidth, bm.bmHeight, 1, 1, NULL );
hdcMem = CreateCompatibleDC( NULL );
hdcMem2 = CreateCompatibleDC( NULL );
hbmOld =( HBITMAP ) SelectObject( hdcMem, hbmColour );
hbmOld2 =( HBITMAP ) SelectObject( hdcMem2, hbmMask );
SetBkColor( hdcMem, crTransparent );
BitBlt( hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY );
BitBlt( hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT );
SelectObject( hdcMem, hbmOld );
SelectObject( hdcMem2, hbmOld2 );
DeleteDC( hdcMem );
DeleteDC( hdcMem2 );
return hbmMask;
}

Resources