First, I have a picture control with a bitmap1 loaded in a dialog box:
SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadImage(NULL, sbitmap1.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
Second, at some point I draw a bitmap2 in the picture control using StretchBlt.
hdcImage = GetDC(hWnd)
hMemDC = CreateCompatibleDC( hdcImage );
hOldbm = (HBITMAP)SelectObject( hMemDC, hbitmap2 );
SetStretchBltMode( hdcImage, COLORONCOLOR);
StretchBlt( hdcImage, left, top, width, height, hMemDC, 0, 0, w, h, SRCCOPY );
SelectObject( hMemDC, hOldbm );
The bitmap2 is successfully painted but in certain occasions (for example when I minimize the dialog) the picture control no longer shows bitmap2 but bitmap1 instead.
I think the problem is the repaint event. Is there a way to stop the repaint event or change the bitmap that this event is going to paint?
Edit:
Thanks #Mark and #Edward for your answers.
The problem was this:
after using StretchBlt you need to do SendMessage STM_SETIMAGE
for SendMessage STM_SETIMAGE use a global HBITMAP (preferably)
Something like this:
hbitmapglobal = (HBITMAP)CopyImage(hbitmap2, IMAGE_BITMAP, abs(width), abs(height), LR_COPYRETURNORG);
SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbitmapglobal);
CopyImage is the real simple way to make a copy of a HBITMAP. Take in consideration that this is a sample and hbitmapglobal must be freed at some point.
Normally all the painting occurs in the WM_PAINT handler.
I would suggest setting up (global) variables when you need to paint the second bitmap and invalidate the rectangle of the picture control.
Check for the variable in the WM_PAINT handler and do the painting there.
Related
I'm creating a context menu for a window and I'd like to have in a submenu filled ellipses, each in a different colour, instead of texts - this submenu is responsible for choosing a colour. I don't know how to do it... Does anybody know any concrete example? I've read on MSDN pages about owner-drawn menu items, but there were no example concerning this specific task - so, I don't know how to do it. Later I tried to change checked icon for my menu item - but it turned out my Dev-C++ (under Windows 7) knows neither SetDCBrushColor nor DC_BRUSH, and I still don't know how to change checked icon without loading an image from a file. Then I added at the beginning of my program the following lines:
#define _WIN32_IE 0x0600
#define WINVER 0x0700
#define _WIN32_WINNT 0x0700
Then the compiler doesn't protest, however, the icon is always black, when I'm trying the following code and whatever colour I'm choosing:
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC( hwnd );
HDC hdcMem = CreateCompatibleDC( hdc );
SIZE size = { GetSystemMetrics( SM_CXMENUCHECK ), GetSystemMetrics( SM_CYMENUCHECK ) };
HBITMAP bitmap = CreateCompatibleBitmap( hdcMem, size.cx, size.cy );
HBITMAP bitmapOld = (HBITMAP) SelectObject( hdcMem, bitmap );
PatBlt( hdcMem, 0, 0, size.cx, size.cy, WHITENESS );
HBRUSH brushOld = (HBRUSH) SelectObject( hdcMem, GetStockObject( NULL_BRUSH ) );
Ellipse( hdcMem, 0, 0, size.cx, size.cy);
SetDCBrushColor( hdcMem, RGB(0,0,255) );
SelectObject( hdcMem, GetStockObject( DC_BRUSH ) );
Ellipse( hdcMem, 2, 2, size.cx-2, size.cy-2 );
SelectObject( hdcMem, brushOld );
SelectObject( hdcMem, bitmapOld );
DeleteDC( hdcMem );
ReleaseDC( hwnd, hdc );
SetMenuItemBitmaps( mnu_t, (30*M_MENU_T+25), MF_BYCOMMAND, bitmap, bitmap);// mnu_t and M_MENU_T are my variables
Can anyone help me?
The problem is here:
HBITMAP bitmap = CreateCompatibleBitmap( hdcMem, size.cx, size.cy );
This is a perfectly natural and rational statement to write, but it doesn't do what most people expect it to do. It's a common mistake and it can take forever to figure out that the source of the problem is here. I know this from making this very mistake more than once myself.
CreateCompatibleBitmap does not create a bitmap that's compatible with the specified device context (DC). Well, it does--sort of--but it's actually more nuanced that: It creates a new bitmap that's compatible with the bitmap that's selected into the specified DC.
When creating a memory DC with CreateCompatibleDC, the new DC gets a default bitmap that is 1 pixel wide by 1 pixel high and with a color depth of 1 bit. This is not intuitive at all, since you asked for the DC to be compatible with the screen DC, and the screen (almost certainly) has more than 1 bit of color depth.
So when you call CreateCompatibleBitmap you get a new bitmap of the specified size that uses 1 bit per pixel. You can draw to it, and, if the stars align, you'll see a crude outline of what you drew. But if the stars aren't aligned, you'll end up with all the pixels being a single color.
When you blit a 1-bit per pixel bitmap to another DC, the current text foreground and background colors are used. Since the foreground text color is often black, you end up with a black rectangle and virtually no clue as to where you went wrong.
The solution is to specify the screen or window DC instead of the memory DC in the call the CreateCompatibleBitmap. This will create a new bitmap that has the same color depth as the bitmap used by the screen, which is far more useful.
INTRODUCTION AND RELEVANT INFORMATION:
Recently, I have asked, here in SO, a question about scaling a bitmap properly, so it can keep the quality of the picture:
Bitmap loses quality when stretched/shrinked on buttons background.
I have tried to employ a suggestion made in a comment, to use `StretchBlt, so I have made a small demo program.
It did improve the bitmaps sharpness, after I have set stretch mode to BLACKONWHITE.
I would like to try to make the portion of the bitmap, with the certain color-say black for example, transparent.
I have used TransparentBlt before, but I don't know how to do it now.
PROBLEM:
In order to preserve the sharpness of the picture, I need to StretchBlt it in the memory DC, with stretch mode being BLACKONWHITE.
The problem is that I do not know how to Blt it transparently into main window's DC.
Here is a code snippet from the demo app:
case WM_PAINT:
{
// main window's DC
hdc = BeginPaint(hWnd, &ps);
// main window's client rectangle
RECT r;
GetClientRect( hWnd, &r );
// memory DC for double buffering
HDC MemDC = CreateCompatibleDC( hdc );
// fill it with test brush
FillRect( MemDC, &r, (HBRUSH)GetStockObject( GRAY_BRUSH ) );
// select loaded bitmap into memory DC
HBITMAP old = (HBITMAP)SelectObject( MemDC, bmp );
// get bitmaps dimensions
BITMAP b;
GetObject( bmp, sizeof(BITMAP), &b );
// needed to preserve bitmap's sharpness
SetStretchBltMode( hdc, BLACKONWHITE );
StretchBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top,
MemDC, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY );
/* TransparentBlt( ... ); call should go here,
so I can make portion of the bitmap transparent,
in order for the gray brush can be seen */
// cleanup
SelectObject( MemDC, old );
DeleteDC(MemDC);
EndPaint(hWnd, &ps);
}
return 0L;
break;
QUESTION:
How to modify the above code, so a bitmap can be transparent, in order for test brush to be seen ?
The original image is bellow.
I just need to use TransparentBlt( ..., RGB( 0, 0, 0 ) ); to make it transparent in black areas.
The example picture that shows result:
MY EFFORTS:
Browsing through Internet, I have found only simple tutorials, regarding double buffering.
I haven't found anything like this, but to be honest, I am inexperienced in WIN32 API, so I don't know how to phrase the question properly, in order to get better search results.
If further information is required, ask for it and I will supply it.
It is omitted to keep the question short.
You Need to create a mask use specific raster operations to copy only the Pixels were the mask is defined.
http://www.winprog.org/tutorial/transparency.html
The next code is MFC, but you can easily extract and convert the MFC objects into the Standard GDI operations.
http://www.codeproject.com/Articles/703/Drawing-Transparent-Bitmap-with-ease-with-on-the-f
I register the window class like this:
WNDCLASSEX wctt;
wctt.cbSize = sizeof(WNDCLASSEX);
wctt.style = CS_DBLCLKS;
wctt.lpfnWndProc = WndProcTooltip;
wctt.cbClsExtra = 0;
wctt.cbWndExtra = 0;
wctt.hInstance = m_hAppInstance;
wctt.hIcon = NULL;
wctt.hCursor = LoadCursor(NULL, IDC_SIZE);
wctt.hbrBackground = NULL;
wctt.lpszMenuName = NULL;
wctt.lpszClassName = _T("myWindow");
wctt.hIconSm = NULL;
RegisterClassEx(&wctt)
As you can see I use wctt.hbrBackground = NULL; so it will have no background.
The window is created like this:
::CreateWindowEx(WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
_T("myWindow"),
NULL,
WS_VISIBLE | WS_POPUP,
50,
50,
150,
100,
NULL,
NULL,
m_hAppInstance,
NULL);
In the paint section I draw icon on the window:
PAINTSTRUCT ps;
HDC hdc;
BITMAP bitmap;
ICONINFO iconinfo;
hdc = ::BeginPaint(hWnd, &ps);
::SetBkMode(hdc,TRANSPARENT);
::GetIconInfo(localIcon, &iconinfo);
::GetObject(iconinfo.hbmColor, sizeof(bitmap), &bitmap);
::DeleteObject(iconinfo.hbmColor);
::DeleteObject(iconinfo.hbmMask);
::DrawIconEx(hdc, 0,0, localIcon, bitmap.bmWidth, bitmap.bmHeight, 0, NULL, DI_NORMAL);
The icon size is smaller than the window size and I get on the background the current view on the window below the popup.
But now when I move the window (or minimize the window below the popup) the background is not changing.
I was trying to make a timer that each time do the flowing:
RECT rcClient;
GetClientRect(hWnd, &rcClient);
InvalidateRect(hWnd,&rcClient,TRUE);
This makes the print function run again but the background of the icon is not changing.
Should I do anything in WM_ERASEBKGND?
Does Anyone have any idea how to make it work?
thanks,
guy
It's not enough to just let the background stay unpainted; you also need to get the window below yours to repaint itself when necessary.
If the windows are part of the same hierarchy, created by the same thread, it is sufficient to give your window the WS_EX_TRANSPARENT extended style. This causes the window underneath to paint itself first so the background is always up-to-date.
Otherwise you need to use SetWindowRgn so that your window actually doesn't exist outside of the borders you wish to paint.
Look at Layered Window. This feature allows creating semi-transparent windows of different shapes.
Add WS_EX_LAYERED extended attribute in your window class.
You can control the transparency of your window with these two functions:
SetLayeredWindowAttributes:
bAlpha controls the opacity of the entire window, if you pass LWA_ALPHA in dwFlags.
When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
crKey sets the color that would transparent.
All pixels painted by the window in this color will be transparent.
UpdateLayeredWindow gives you precise control over window transparency, you can give different parts of window different levels of transparency.
If you're trying to create a non-rectangular window, this is not sufficient. Setting "no background" simply means the background will not be drawn, and you'll see whatever happens to be in memory at that location.
To create a non-rectangular window, have a look at the SetWindowRgn function.
I'm creating what should be a very simple Win32 C++ app whose sole purpose it to ONLY display a semi-transparent PNG. The window shouldn't have any chrome, and all the opacity should be controlled in the PNG itself.
My problem is that the window doesn't repaint when the content under the window changes, so the transparent areas of the PNG are "stuck" with what was under the window when the application was initially started.
Here's the line where I setup the new window:
hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_POPUP, 0, height/2 - 20, 40, 102, NULL, NULL, hInstance, 0);
For the call to RegisterClassEx, I have this set for the background:
wcex.hbrBackground = (HBRUSH)0;
Here is my handler for WM_PAINT message:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
Gdiplus::Graphics graphics(hdc);
graphics.DrawImage(*m_pBitmap, 0, 0);
EndPaint(hWnd, &ps);
break;
}
One thing to note is that the application is always docked to the left of the screen and doesn't move. But, what's underneath the application may change as the user opens, closes or moves windows under it.
When the application first starts, it looks perfect. The transparent (and simi-transparent) parts of the PNG show through perfectly. BUT, when the background underneath the application changes, the background DOESN'T update, it just stays the same from when the application first started. In fact, WM_PAINT (or WM_ERASEBKGND does not get called when the background changes).
I've been playing with this for quite a while and have gotten close to getting 100% right, but not quite there. For instance, I've tried setting the background to (HBRUSH) NULL_BRUSH and I've tried handling WM_ERASEBKGND.
What can be done to get the window to repaint when the contents under it changes?
I was able to do exactly what I wanted by using the code from Part 1 and Part 2 of this series:
Displaying a Splash Screen with C++
Part 1: Creating a HBITMAP archive
Part 2: Displaying the window archive
Those blog posts are talking about displaying a splash screen in Win32 C++, but it was almost identical to what I needed to do. I believe the part that I was missing was that instead of just painting the PNG to the window using GDI+, I needed to use the UpdateLayeredWindow function with the proper BLENDFUNCTION parameter. I'll paste the SetSplashImage method below, which can be found in Part 2 in the link above:
void SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash)
{
// get the size of the bitmap
BITMAP bm;
GetObject(hbmpSplash, sizeof(bm), &bm);
SIZE sizeSplash = { bm.bmWidth, bm.bmHeight };
// get the primary monitor's info
POINT ptZero = { 0 };
HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorinfo = { 0 };
monitorinfo.cbSize = sizeof(monitorinfo);
GetMonitorInfo(hmonPrimary, &monitorinfo);
// center the splash screen in the middle of the primary work area
const RECT & rcWork = monitorinfo.rcWork;
POINT ptOrigin;
ptOrigin.x = 0;
ptOrigin.y = rcWork.top + (rcWork.bottom - rcWork.top - sizeSplash.cy) / 2;
// create a memory DC holding the splash bitmap
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcMem, hbmpSplash);
// use the source image's alpha channel for blending
BLENDFUNCTION blend = { 0 };
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
// paint the window (in the right location) with the alpha-blended bitmap
UpdateLayeredWindow(hwndSplash, hdcScreen, &ptOrigin, &sizeSplash,
hdcMem, &ptZero, RGB(0, 0, 0), &blend, ULW_ALPHA);
// delete temporary objects
SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
}
Use the SetLayeredWindowAttributesarchive function, this allows you to set a mask color that will become transparent, thus allowing the background to show through.
You will also need to configure your window with the layered flag, e.g.:
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
After that it's fairly simple:
// Make red pixels transparent:
SetLayeredWindowAttributes(hwnd, RGB(255,0,0), 0, LWA_COLORKEY);
When your PNG contains semi-transparent pixels that you want to blend with the background, this becomes more complicated. You could try looking at the approach in this CodeProject article:
Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above
I have a Window (win32 API) Application in visual c++. I am not using MFC. I have to add a picutre box to my application and Change the image of this picture box periodically. Can any one help me out in achieving the above task? Thanks in advance.
This is quite a complex task to post full code here, but I will try to give a few guidelines on how to do it:
First method is to load the image and paint it
Load your image (unfortunately the plain Win32 API has support for quite a few image formats BMP, ICO ...).
HBITMAP hImage = (HBITMAP)LoadImage(NULL, (LPCSTR)file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT);
Store the handle above somewhere in your application where you can access it from your WindowProcedure
In the WinProc on the WM_PAINT message you will need to paint the image. The code is something like:
HDC hdcMem = CreateCompatibleDC(hDC); // hDC is a DC structure supplied by Win32API
SelectObject(hdcMem, hImage);
StretchBlt(
hDC, // destination DC
left, // x upper left
top, // y upper left
width, // destination width
height, // destination height
hdcMem, // you just created this above
0,
0, // x and y upper left
w, // source bitmap width
h, // source bitmap height
SRCCOPY); // raster operation
Should work.
Now, the second way of doing it is to create a static control, with type being SS_BITMAP and set its image as:
hImage = LoadImage(NULL, file, IMAGE_BITMAP, w, h, LR_LOADFROMFILE);
SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
where hwnd is the handle of your static control.