Win32 - Change background color under ENTIRE 'Static' control area? - winapi

VS2019 c++
This is how I'm declaring a 'Static' label in my humble Win32 application:
hLabel = CreateWindowW(L"Static", L"My Application Text", WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 20, 50, 700, 50, hWnd, NULL, NULL, NULL);
Each line of text uses about 25 pixel worth vertically. So I effectively have room for two lines worth of text in this control since it is declared as 50 pixels high.
I would like to override the default colors used to paint the control, so I experiment with this code:
case WM_CTLCOLORSTATIC:
{
HDC hEdit = (HDC)wParam;
SetTextColor(hEdit, RGB(0, 255, 0));
SetBkColor(hEdit, RGB(0, 0, 255));
return 0;
}
This works "great" except it does not paint the entire area of the control?
As you can see above, the Text color is applied to the characters and the 'Background' color is applied not to the entire area of the control, enclosed by the WS_BORDER, but only to the extent of the characters. I start by filling the control with 100 characters. That's why you only see "one" line of text. My question is "how should I go about filling the entire are of the control"? Which is declared as having an area of 700 pixel wide by 50 pixel high.
I confess I searched around quite a bit but I feel I'm going in circle.

The answer is "42" -- kidding!
This is how I changed my code :
case WM_CTLCOLOREDIT:
{
HDC hEdit = (HDC)wParam;
SetTextColor(hEdit, RGB(0, 0, 255));
SetBkColor(hEdit, RGB(255, 255, 0));
SetBkMode(hEdit, TRANSPARENT);
BackEDIT = CreateSolidBrush(RGB(255, 255, 0));
return (INT_PTR)BackEDIT;
}
And then I added :
case WM_DESTROY:
DeleteObject(BackEDIT);
PostQuitMessage(0);
return 0;
So, adding a BRUSH and deleting the BRUSH. Works perfect!

Related

How to draw raster console fonts in GDI?

How do I draw the raster fonts used by the Windows console in a GDI application? For instance, the 8x8 fixed font as shown in this screenshot.
Can these fonts be used via the CreateFont() API, or is there some special way that Windows loads these fonts?
The console uses "fixed width fonts", such as "Courier New" (available in all Windows version) or "Consolas" (available since Vista).
Fixed width fonts are not necessarily raster. To use raster fonts, enumerate fonts to find a raster font such as "Terminal" or "Fixedsys". You have to use the right size (example, 18 for "Terminal" font) otherwise Windows may substitute a different font and resize. There are also issues with DPI settings. If program is not DPI aware then magnification will occur if work station has high DPI settings.
case WM_PAINT:
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
auto hfont = CreateFont(-18, 0, 0, 0, 0, 0, 0, 0,
ANSI_CHARSET,
OUT_DEVICE_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
L"Terminal");
auto oldfont = SelectObject(hdc, hfont);
RECT rc = { 0,0,100,300 };
DrawText(hdc, L"Test 123", -1, &rc, DT_LEFT | DT_TOP);
SelectObject(hdc, oldfont);
DeleteObject(hfont);
EndPaint(hwnd, &ps);
return 0;
}
The answer was similar to Barmak's answer, with the difference that both width and height are specified, so to create a font for the 8x8 raster font I use the following code:
hfont = CreateFont(-8, -8, 0, 0, 0, 0, 0, 0, OEM_CHARSET, OUT_DEVICE_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("Terminal"));
Specifically, both the height and width must be specified, and the OEM_CHARSET charset must be specified, in order to select one of the Raster Fonts.
My intent is to render to a DirectDraw surface (IDirectDrawSurface7::GetDC()) then subsequently paint that surface to the primary, as shown here:
With a bit of trickery involving multiple passes I added a bit of a shadow effect to the text, however that is outside the scope of my original question.

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.

win32 - How to draw a rectangle around a text string?

I am new to Win32 and trying to get a GDI based code in C++ ( for technical reasons don't want to use GDI+)
Edit: Simplied the question:
I need to draw a rectangle around the text that is drawn in the middle of the window.
- How can I populate the rectangle co-ordinates?
- Can any one help with the line - Rectangle(x1,y1,x2,y2)? - How to calculate these (x1,y1) & (x2,y2) values?
Thank you..
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rcClient);
SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
SetTextColor(hdc, RGB(255, 0, 0));
DrawText(hdc, wstring(s.begin(),s.end()).c_str(), -1, &rectResult, DT_SINGLELINE | DT_CALCRECT);
DrawText(hdc, wstring(s.begin(),s.end()).c_str(), -1, &rcClient, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
// Here I need help - How to I place the rectangle around the Text - which is drawn in the middle of the window?
// It looks like need to use - rectResult.bottom/top/left/right - but don't know how..
Rectangle(hdc, 0,0,100,100);
You don't actually have to center the text yourself. The GDI text output functions can do that for you if you pass the appropriate flags.
For example, if you call DrawText and pass the DT_CENTER flag, it will automatically draw the text in the middle of the specified rectangle (horizontally centered).
Assuming you have only a single line of text (which it sounds like you do), you can get it to automatically vertically center the text by passing the DT_SINGLELINE and DT_VCENTER flags.
So all you really have to do is write code to divide your window's client area up into 4 equal parts, and then pass those rectangles to the DrawText function. That's not terribly difficult. Put a pencil and paper to it if you can't visualize it in your head.
void PaintWindow(HWND hWnd)
{
// Set up the device context for drawing.
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
HPEN hpenOld = static_cast<HPEN>(SelectObject(hDC, GetStockObject(DC_PEN)));
HBRUSH hbrushOld = static_cast<HBRUSH>(SelectObject(hDC, GetStockObject(NULL_BRUSH)));
// Calculate the dimensions of the 4 equal rectangles.
RECT rcWindow;
GetClientRect(hWnd, &rcWindow);
RECT rc1, rc2, rc3, rc4;
rc1 = rc2 = rc3 = rc4 = rcWindow;
rc1.right -= (rcWindow.right - rcWindow.left) / 2;
rc1.bottom -= (rcWindow.bottom - rcWindow.top) / 2;
rc2.left = rc1.right;
rc2.bottom = rc1.bottom;
rc3.top = rc1.bottom;
rc3.right = rc1.right;
rc4.top = rc1.bottom;
rc4.left = rc1.right;
// Optionally, deflate each of the rectangles by an arbitrary amount so that
// they don't butt up right next to each other and we can distinguish them.
InflateRect(&rc1, -5, -5);
InflateRect(&rc2, -5, -5);
InflateRect(&rc3, -5, -5);
InflateRect(&rc4, -5, -5);
// Draw (differently-colored) borders around these rectangles.
SetDCPenColor(hDC, RGB(255, 0, 0)); // red
Rectangle(hDC, rc1.left, rc1.top, rc1.right, rc1.bottom);
SetDCPenColor(hDC, RGB(0, 255, 0)); // green
Rectangle(hDC, rc2.left, rc2.top, rc2.right, rc2.bottom);
SetDCPenColor(hDC, RGB(0, 0, 255)); // blue
Rectangle(hDC, rc3.left, rc3.top, rc3.right, rc3.bottom);
SetDCPenColor(hDC, RGB(255, 128, 0)); // orange
Rectangle(hDC, rc4.left, rc4.top, rc4.right, rc4.bottom);
// Draw the text into the center of each of the rectangles.
SetBkMode(hDC, TRANSPARENT);
SetBkColor(hDC, RGB(0, 0, 0)); // black
// TODO: Optionally, set a nicer font than the default.
DrawText(hDC, TEXT("Hello World!"), -1, &rc1, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
DrawText(hDC, TEXT("Hello World!"), -1, &rc2, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
DrawText(hDC, TEXT("Hello World!"), -1, &rc3, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
DrawText(hDC, TEXT("Hello World!"), -1, &rc4, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
// Clean up after ourselves.
SelectObject(hDC, hpenOld);
SelectObject(hDC, hbrushOld);
EndPaint(hWnd, &ps);
}
RECT rect={0,0,0,0};
const char *str="Test Text";
DrawText(hDC, str, strlen(str), &rect, DT_CALCRECT | DT_NOCLIP);
Rectangle(hDC,rect.left,rect.top,rect.right,rect.bottom);
DrawText(hDC, str, strlen(str), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE|DT_NOCLIP);
Got it finally :) Thanks a lot Cody Gray for pointing me in right direction about this :)
GetTextExtentPoint32(hDC, str, strlen(str), &sz2);
rect2.top=rect2.bottom+sz2.cy;
rect2.right=rect2.top+sz2.cx;
Rectangle(hDC,rect2.left,rect2.top,rect2.right,rect2.bottom);
DrawText(hDC, str, -1, &rect2, DT_CENTER | DT_VCENTER | DT_SINGLELINE|DT_NOCLIP);

Draw Shapes in WinAPI C++

I'm studying Drawing Shapes by WinAPI C++
I tried to draw 2 ellipse with some codes on WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HPEN hPen = CreatePen(PS_DASHDOTDOT, 2, NULL);SelectObject(hdc, hPen);
Ellipse(hdc, 100, 200, 400, 400);
Ellipse(hdc, 300, 300, 500, 510);
DeleteObject(hPen);
EndPaint(hWnd, &ps);
But the output is:
The result I expect is neither shapes is front of the other. And the border is dash dot dot.
Can anyone show me my mistake? I appreciate for your help.
Ellipse() (like all GDI shape functions) fills the shape using the current brush, which is why your output looks like that. For details on that see setting pen and brush colors.
If you want just the ellipse with no fill, first select a null brush:
SelectObject( hdc, GetStockObject( NULL_BRUSH ) );
One appears on top of the other because you're not just drawing the outline, but filling it as well. To stop filling it, you can select a "hollow brush", sometimes called a "null brush".
HBRUSH hbrOld = SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
// draw your ellipses here
You can only create dotted or dashed pens with widths of 1 or 0. You used 2, so the command failed.
Also, you should select the pen back out of the DC before you delete it.

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