I have different screens serving different purposes in my win32 app, each screen having a different background. The main window have its own static background.
I am drawing screen background on a custom window using following code.
panelBackground = LoadBitmap ( hInstance, MAKEINTRESOURCE ( bitmap ) );
WNDCLASSEX wincl;
wincl.hInstance = hInstance;
wincl.lpszClassName = "jPanel";
wincl.lpfnWndProc = WndProc;
wincl.style = CS_BYTEALIGNWINDOW;// | CS_HREDRAW | CS_VREDRAW;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = 0;
wincl.hIconSm = 0;
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 4;
wincl.hbrBackground = ( HBRUSH ) GetStockObject( BLACK_BRUSH );
RegisterClassEx (&wincl);
hwnd = CreateWindowEx ( 0, "jPanel", txt.c_str(), WS_CLIPSIBLINGS | WS_CHILD, x, y, width, height, parent, 0, hInstance, 0) ;
SetWindowLong( hwnd , 0 , ( LONG ) this ) ;
SetWindowPos ( hwnd , HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE ) ;
Then in the WndProc :
view::jPanel* panel = ( view::jPanel* ) GetWindowLong( hwnd , 0 ) ;
case WM_PAINT:
{
if ( ! panel )
return 0 ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
hdc = BeginPaint( hwnd , &ps ) ;
HDC dcSkin = CreateCompatibleDC ( hdc ); // memory dc for skin
HDC hMemDc = CreateCompatibleDC ( hdc ); // memory dc for painting
HBITMAP hmemBmp = CreateCompatibleBitmap ( hdc, rect.right - rect.left, rect.bottom - rect.top ); // Create bitmap to draw on
HBITMAP hOldMemBmp = (HBITMAP)SelectObject ( hMemDc, hmemBmp ); // select memory bitmap in memory dc
HBITMAP hOldSkinBmp = (HBITMAP)SelectObject ( dcSkin, panel->panelBackground ); //select skin bitmap in skin memory dc
BitBlt ( hMemDc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, dcSkin, 0, 0, SRCCOPY ); // Paint Skin on Memory DC
BitBlt ( hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, hMemDc, 0, 0, SRCCOPY ); // Paint Skin on Window DC
//<<<... DeleteDC will leak memory if it holds a resource, so lets select the old bitmap back in the memory DCs
SelectObject ( hMemDc, hOldMemBmp ); // select old bitmaps back to their respective DCs before deleting
SelectObject ( dcSkin, hOldSkinBmp ); // select old bitmaps back to their respective DCs before deleting
DeleteObject ( hOldSkinBmp );
DeleteObject ( hOldMemBmp );
DeleteObject( hmemBmp );
DeleteDC ( hMemDc );
DeleteDC ( dcSkin );
EndPaint( hwnd , &ps ) ;
}
break ;
Problem : The child windows(buttons, edit controls) belonging to that screen don't show up, I am guessing
that they are below the screen background window. Moving the controls up the z-order with WS_EX_TOPMOST style doesn't work neither moving the screen background window at the bottom of z-order with SetWindowPos ( hwnd , HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE ) ; works.
And yes buttons are BS_BITMAP styled.
Add the WS_CLIPCHILDREN style when you create the parent window.
Related
I'm need to draw border on top another application's window (the main purpose is to highlight window user chooses from running applications list). I'm trying to draw border on top of native window border, but the border isn't drawn. Here is the code:
HPEN framePen = ::CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
HWND handle = FindWindow(L"ConsoleWindowClass", L"C:\\WINDOWS\\system32\\cmd.exe");
WINDOWPLACEMENT winPlacement;
GetWindowPlacement(handle, &winPlacement);
if (winPlacement.showCmd == SW_SHOWMINIMIZED)
{
ShowWindow(handle, SW_RESTORE);
}
SetWindowPos(handle, HWND_TOP, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
SetForegroundWindow(handle);
PAINTSTRUCT ps;
RECT rect = {};
::GetClientRect(handle, &rect);
HDC hdc = ::BeginPaint(handle, &ps);
::SelectObject(hdc, framePen);
::Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
::EndPaint(handle, &ps);
In this example used handle of cmd window, but in fact it doesn't matter.
Could you please tell why border isn't drawn and how to draw it?
Thanks.
You can't draw directly on another window, because the system may refresh the window at any time, overwriting your drawing.
To make your drawing persistent, create a layered window, positioned on top of the other window.
Create the window with WS_EX_LAYERED flag.
Pass a color key to SetLayeredWindowAttributes().
In your WM_PAINT handler, draw the inside of the rectangle with the color key (by using it for the brush). Everything you draw with the color key will become transparent. Draw the border of the rectangle with the desired color (by using it for the pen).
Here is a minimal example to get you started. The frame can be moved around by drag-n-drop.
Note there is no error handling to keep the sample code concise. You should check the return value of each Windows API call.
#include <windows.h>
const COLORREF MY_COLOR_KEY = RGB( 255, 0, 255 );
int APIENTRY wWinMain(
HINSTANCE hInstance, HINSTANCE /*hPrevInst*/, LPWSTR /*lpCmdLine*/, int nCmdShow )
{
WNDCLASSW wc{ sizeof( wc ) };
wc.hInstance = hInstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hCursor = LoadCursor( nullptr, IDC_ARROW );
wc.hbrBackground = reinterpret_cast<HBRUSH>( COLOR_BTNFACE + 1 );
wc.lpszClassName = L"MyTransparentFrame";
wc.lpfnWndProc = []( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp ) -> LRESULT
{
switch( msg )
{
case WM_PAINT:
{
PAINTSTRUCT ps{};
HDC hdc = BeginPaint( hwnd, &ps );
RECT rc{}; GetClientRect( hwnd, &rc );
HPEN hPen = CreatePen( PS_SOLID, 20, GetSysColor( COLOR_HIGHLIGHT ) );
HBRUSH hBrush = CreateSolidBrush( MY_COLOR_KEY );
HGDIOBJ hOldPen = SelectObject( hdc, hPen );
HGDIOBJ hOldBrush = SelectObject( hdc, hBrush );
Rectangle( hdc, rc.left, rc.top, rc.right, rc.bottom );
if( hOldPen )
SelectObject( hdc, hOldPen );
if( hOldBrush )
SelectObject( hdc, hOldBrush );
if( hPen )
DeleteObject( hPen );
if( hBrush )
DeleteObject( hBrush );
EndPaint( hwnd, &ps );
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_NCHITTEST:
return HTCAPTION; // to be able to drag the window around
break;
default:
return DefWindowProcW( hwnd, msg, wp, lp );
}
return 0;
};
RegisterClassW( &wc );
HWND hwnd = CreateWindowExW( WS_EX_LAYERED, wc.lpszClassName, L"", WS_POPUP,
200, 200, 800, 600, nullptr, nullptr, hInstance, nullptr );
SetLayeredWindowAttributes( hwnd, MY_COLOR_KEY, 255, LWA_COLORKEY );
ShowWindow( hwnd, nCmdShow );
MSG msg;
while( GetMessage( &msg, nullptr, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int) msg.wParam;
}
Finally I managed to solve the problem with the following code:
const COLORREF MY_COLOR_KEY = RGB(255, 128, 0);
HWND cmdHanlde = NULL;
constexpr unsigned int timerIdWindowUpdate = 1;
constexpr unsigned int timerIdFrameColor = 2;
bool tick = false;
bool minimized = false;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = L"MyTransparentFrame";
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpfnWndProc = [](HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) -> LRESULT
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps{};
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc{}; GetClientRect(hwnd, &rc);
HPEN hPen = CreatePen(PS_SOLID, 5, tick ? RGB(255, 128, 1) : RGB(255, 201, 14));
HBRUSH hBrush = CreateSolidBrush(MY_COLOR_KEY);
HGDIOBJ hOldPen = SelectObject(hdc, hPen);
HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
if (hOldPen)
SelectObject(hdc, hOldPen);
if (hOldBrush)
SelectObject(hdc, hOldBrush);
if (hPen)
DeleteObject(hPen);
if (hBrush)
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER:
{
if (wp == timerIdWindowUpdate)
{
WINDOWPLACEMENT windowPlacement = { sizeof(WINDOWPLACEMENT), };
if (::GetWindowPlacement(cmdHanlde, &windowPlacement))
{
if (windowPlacement.showCmd == SW_SHOWMINIMIZED
|| !IsWindowVisible(cmdHanlde))
{
ShowWindow(hwnd, SW_HIDE);
minimized = true;
}
else
{
RECT rect = {};
DwmGetWindowAttribute(cmdHanlde, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(rect));
MONITORINFO monInfo;
monInfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfoW(MonitorFromWindow(cmdHanlde, MONITOR_DEFAULTTONEAREST), &monInfo);
if (cmdHanlde != NULL && ::IsZoomed(cmdHanlde))
{
rect.left = monInfo.rcWork.left;
rect.top = monInfo.rcWork.top;
rect.bottom = monInfo.rcWork.bottom > rect.bottom ? rect.bottom : monInfo.rcWork.bottom;
rect.right = monInfo.rcWork.right > rect.right ? rect.right : monInfo.rcWork.right;
}
if (minimized)
{
::SetWindowPos(hwnd, cmdHanlde, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
minimized = false;
}
else
{
::SetWindowPos(cmdHanlde, hwnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
::SetWindowPos(hwnd, 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
SWP_SHOWWINDOW);
}
}
}
}
else if (wp == timerIdFrameColor)
{
tick = !tick;
::RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, msg, wp, lp);
}
return 0;
};
RegisterClassEx(&wc);
HWND hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_LAYERED | WS_EX_TRANSPARENT, wc.lpszClassName, L"", WS_POPUP | WS_VISIBLE | WS_DISABLED,
0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr);
::SetTimer(hwnd, timerIdWindowUpdate, 50, NULL);
::SetTimer(hwnd, timerIdFrameColor, 500, NULL);
SetLayeredWindowAttributes(hwnd, MY_COLOR_KEY, 255, LWA_COLORKEY);
ShowWindow(hwnd, SW_SHOW);
cmdHanlde = FindWindow(L"ConsoleWindowClass", L"C:\\WINDOWS\\system32\\cmd.exe");
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
May be it is not the best solution, but it works for me. Could you please take a look on it and tell if there something to improve?
I'm creating several EDIT elements with a for loop. When I move the window outside the screen and then back inside, the EDIT controls disappear, along with a line in the WM_PAINT event.
The "create" event:
case WM_CREATE:
for( int i =0; i < 4; i++ ){
text = CreateWindow("EDIT","text",WS_VISIBLE | WS_CHILD , 0,i*40+100,100,10, hwnd , NULL, NULL, NULL);
}
// Other elements
break;
The "paint" event:
case WM_PAINT:
{
hdc = GetDC(hwnd);
SetRect(&secondbackground, 0, 70, 800, 500);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,200,200)) );
ReleaseDC(hwnd, hdc);
hdc = GetDC(hwnd);
SetRect(&secondbackground, 0, 0, 800, 20);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,100,200)) );
ReleaseDC(hwnd, hdc);
hdc = BeginPaint( hwnd, &ps );
MoveToEx(hdc,1,52,NULL);
LineTo(hdc,100,200);
EndPaint( hwnd, &ps );
hdc = GetDC(hwnd);
SetPixel(hdc,300,300,RGB(255,255,255));
EndPaint( hwnd, &ps );
}
break;
Use BeginPaint/EndPaint when handling WM_PAINT, that will force background drawing which GetDC does not.
I am using ::TransparentBlt to paint a bmp with transparent pixels marked by Magenta RGB(255, 0, 255), but ::TransparentBlt is not behaving properly, some pixels which shouldn't be transparent are transparent in the result.
What am I doing wrong ?
Original Image :
Result from ::TransparentBlt
The grey area in the result image is background image on top of which the original bmp is ::TransparentBlt-ed
Code:
LRESULT jItems::paint ( )
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
::GetClientRect ( hwnd , &rect ) ;
hdc = ::BeginPaint ( hwnd , &ps ) ;
HDC dcSkin = ::CreateCompatibleDC ( hdc ); // memory dc for skin
HDC hMemDc = ::CreateCompatibleDC ( hdc ); // memory dc for painting
HBITMAP hmemBmp = ::CreateCompatibleBitmap ( hdc, rect.right - rect.left, rect.bottom - rect.top ); // Create bitmap to draw on
HBITMAP hOldMemBmp = ( HBITMAP ) ::SelectObject ( hMemDc, hmemBmp ); // select memory bitmap in memory dc
HBITMAP hOldSkinBmp = ( HBITMAP ) ::SelectObject ( dcSkin, this->hbitmap ); //select skin bitmap in skin memory dc
::FillRect ( hMemDc, &rect, ::CreateSolidBrush ( backgroundColor ) );
::BitBlt ( hMemDc, 0, 0, rect.right - rect.left,
rect.bottom - rect.top, dcSkin, 0, 0, SRCCOPY ); // Paint Skin on Memory DC
::SelectObject ( dcSkin, bottomEdge ); // select edge bitmap in skin memory dc
::TransparentBlt ( hMemDc, 0, 0, rect.right - rect.left,
rect.bottom - rect.top, dcSkin,
0, 0, 237, 10, RGB ( 255, 0, 255 ) ); // Paint edge on Memory DC
::BitBlt ( hdc, 0, 0, rect.right - rect.left,
rect.bottom - rect.top, hMemDc, 0, 0, SRCCOPY ); // Paint Skin on Window DC
//<<<... DeleteDC will leak memory if it holds a resource, so lets select the old bitmap back in the memory DCs
::SelectObject ( hMemDc, hOldMemBmp ); // select old bitmaps back to their respective DCs before deleting
::SelectObject ( dcSkin, hOldSkinBmp ); // select old bitmaps back to their respective DCs before deleting
::DeleteObject ( hOldSkinBmp );
::DeleteObject ( hOldMemBmp );
::DeleteObject( hmemBmp );
::DeleteDC ( hMemDc );
::DeleteDC ( dcSkin );
::EndPaint ( hwnd , &ps ) ;
return 0;
};
From the TransparentBlt documentation.
If the source and destination rectangles are not the same size, the source bitmap is stretched to match the destination rectangle.
In your call to TransparentBlt the destination rectangle has size (rect.right - rect.left, rect.bottom - rect.top) and the source rectangle has size (237, 10). So the bitmap is stretched and you don't get the result you expected.
I guess both sizes should be (237, 10).
I have a question similar to this, How to display text in system tray icon with win32 API?
I tried his solution but it's not working for me. I get a small 4x16 white image as the system icon instead of text and I can't understand why.
I'm not using MFC/.NET just win32 api.
void UpdateIcon(HWND hWnd){
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
nid.hIcon = CreateSmallIcon(hWnd);
nid.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
HICON CreateSmallIcon( HWND hWnd )
{
static TCHAR *szText = TEXT ( "100" );
HDC hdc, hdcMem;
HBITMAP hBitmap = NULL;
HBITMAP hOldBitMap = NULL;
HBITMAP hBitmapMask = NULL;
ICONINFO iconInfo;
HFONT hFont;
HICON hIcon;
hdc = GetDC ( hWnd );
hdcMem = CreateCompatibleDC ( hdc );
hBitmap = CreateCompatibleBitmap ( hdc, 16, 16 );
hBitmapMask = CreateCompatibleBitmap ( hdc, 16, 16 );
ReleaseDC ( hWnd, hdc );
hOldBitMap = (HBITMAP) SelectObject ( hdcMem, hBitmap );
PatBlt ( hdcMem, 0, 0, 16, 16, WHITENESS );
// Draw percentage
hFont = CreateFont (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TEXT ("Arial"));
hFont = (HFONT) SelectObject ( hdcMem, hFont );
TextOut ( hdcMem, 0, 0, szText, lstrlen (szText) );
SelectObject ( hdc, hOldBitMap );
hOldBitMap = NULL;
iconInfo.fIcon = TRUE;
iconInfo.xHotspot = 0;
iconInfo.yHotspot = 0;
iconInfo.hbmMask = hBitmapMask;
iconInfo.hbmColor = hBitmap;
hIcon = CreateIconIndirect ( &iconInfo );
DeleteObject ( SelectObject ( hdcMem, hFont ) );
DeleteDC ( hdcMem );
DeleteDC ( hdc );
DeleteObject ( hBitmap );
DeleteObject ( hBitmapMask );
return hIcon;
}
I don't have windows installed currently so i cannot check if this will work better, but i found potential problem - from MSDN documentation of CreateIconIndirect function:
The application must continue to manage the original bitmaps and delete them when they are no longer necessary.
Seems like you are deleting bitmaps too soon.
You need to set background and possibly foreground colors:
SetTextColor( hdcMem, 0x00FF0000 ); // 0x00bbggrr, not rrggbb !!
SetBkMode( hdcMem, TRANSPARENT ); // VERY IMPORTANT
I think DeleteDC ( hdc ); is not needed here as you used GetDC().
I have created a windows application using core WIN32 and VC++. In my parent window I have a child window and two buttons "save" and "send".
When user clicks the "save" button I want the savefileDialog to be opened and user should be able to save the image as a bitmap file.
The same file should be sent to a remote user using WinSock API.... My problem is, I don't know how to save the screen shot of the window to a bitmap file...
please help me out of this ... I have not used MFC, ATL or WTL....
thanks in advance,
RECT rect = {0};
GetWindowRect( hwnd, &rect );
ATL::CImage* image_ = new CImage();
image_ -> Create( rect.right - rect.left, rect.bottom - rect.top, 32 );
HDC device_context_handle = image_ -> GetDC();
PrintWindow( hwnd, device_context_handle, PW_CLIENTONLY );
image_ -> Save( filename );
image_ -> ReleaseDC();
delete image_;
PrintWindow() should do the trick.
To save as HBITMAP:
HDC hDC = GetDC( hwnd );
HDC hTargetDC = CreateCompatibleDC( hDC );
RECT rect = {0};
GetWindowRect( hwnd, &rect );
HBITMAP hBitmap = CreateCompatibleBitmap( hDC, rect.right - rect.left,
rect.bottom - rect.top );
SelectObject( hTargetDC, hBitmap );
PrintWindow( hwnd, hTargetDC, PW_CLIENTONLY );
SaveBMPFile( filename, hBitmap, hTargetDC, rect.right - rect.left,
rect.bottom - rect.top );
DeleteObject( hBitmap );
ReleaseDC( hwnd, hDC );
DeleteDC( hTargetDC );
I will leave the implementation of SaveBMPFile up to you ; )