updatelayeredwindow not working - c++

i tried to use UpdateLayeredWindow for all day, but it failed to work :(, i put the code in OnCreate and load a png file (created by photoshop) with CImage.
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
SetWindowLong(this->m_hWnd, GWL_STYLE, 0);
SetWindowLong(this->m_hWnd, GWL_EXSTYLE, 0);
SetWindowLong(this->m_hWnd, GWL_EXSTYLE, GetWindowLong(this->m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
this->SetMenu(NULL);
::SetWindowPos(this->m_hWnd, HWND_TOP, 50, 50, 652, 492, SWP_SHOWWINDOW);
CBitmap imageBitmap, *oldBitmap;
CDC *dcWindow, dcMem;
CRect wRect;
CPoint wPos;
CSize wSize;
HBITMAP hbmp;
BLENDFUNCTION bFunc;
m_Image.Load(_T("Img/BG_Blue.png"));
GetWindowRect(&wRect);
dcWindow = GetWindowDC();
imageBitmap.CreateCompatibleBitmap(dcWindow, wRect.Width(), wRect.Height());
dcMem.CreateCompatibleDC(dcWindow);
oldBitmap = dcMem.SelectObject(&imageBitmap);
m_Image.Draw(dcMem.m_hDC, 0, 0, wRect.Width(), wRect.Height(), 0, 0, wRect.Width(), wRect.Height());
wPos.x = 0; wPos.y = 0;
wSize.cx = wRect.left; wSize.cy = wRect.bottom;
bFunc.SourceConstantAlpha = 125;
bFunc.BlendFlags = 0;
bFunc.BlendOp = AC_SRC_OVER;
bFunc.AlphaFormat = AC_SRC_ALPHA;
UpdateLayeredWindow(dcWindow, &wPos, &wSize, &dcMem, &wPos, 0, &bFunc, ULW_ALPHA);
//BitBlt(dcWindow->m_hDC, 0, 0, wRect.Width(), wRect.Height(), dcMem.m_hDC, 0, 0, SRCCOPY);
DWORD error = GetLastError();
dcMem.SelectObject(oldBitmap);
return 0;
}
the function returned with 1 , but nothing appeared on the screen, only an icon on the starup menu indicating the program is running. I am wondering if there is something wrong with the png file ... can anyone help ?

dcWindow should be the Device Context of the screen not the window. So initialize it like so: dcWindow->Attach(::GetDC(NULL)) instead of dcWindow = GetWindowDC();

Related

An Aero caption title bar issue using DWM API on the windows 10

In order to draw the icon on the caption title bar, I have refereed this MSDN article and used DWM API to create my customize client area by calling DwmExtendFrameIntoClientArea.
my code:
CMainFrame::CMainFrame()
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
BOOL fDwmEnabled = FALSE;
if (SUCCEEDED(DwmIsCompositionEnabled(&fDwmEnabled)))
TRACE0("DWM is enabled\n");
TCHAR szLogoPath[MAX_PATH];
GetModuleFileName ( GetModuleHandle(NULL), szLogoPath, _countof(szLogoPath) );
PathRemoveFileSpec ( szLogoPath );
PathAppend ( szLogoPath, _T("lena.bmp") );
m_pLogoImage = m_pLogoImage->FromFile ( CT2CW(szLogoPath) );
if(NULL == m_pLogoImage)
TRACE0("load image fail\n");
}
void CMainFrame::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
int xFrame = 2;
int yFrame = 2;
int nTHight = 30;
NCCALCSIZE_PARAMS * p;
RECT * rc;
RECT aRect;
RECT bRect;
RECT acRect;
p = (NCCALCSIZE_PARAMS *)lpncsp;
CopyRect(&bRect,&p->rgrc[1]);
CopyRect(&aRect,&p->rgrc[0]);
acRect.left = aRect.left + xFrame;
acRect.top = aRect.top - nTHight;
acRect.right = aRect.right - xFrame;
acRect.bottom = aRect.bottom - yFrame;
CopyRect(&p->rgrc[0],&acRect);
CopyRect(&p->rgrc[1],&aRect);
CopyRect(&p->rgrc[2],&bRect);
CFrameWnd::OnNcCalcSize(TRUE, lpncsp);
}
LRESULT CMainFrame::OnNcHitTest(CPoint p)
{
BOOL dwm_enabled = FALSE;
if (SUCCEEDED(DwmIsCompositionEnabled(&dwm_enabled)))
{
LRESULT result = 0;
if (!DwmDefWindowProc(m_hWnd, WM_NCHITTEST, 0, MAKELPARAM(p.x, p.y), &result))
result = HitTestNCA(m_hWnd, p);
if (result == HTNOWHERE && GetForegroundWindow() != this)
{
return HTCAPTION;
}
return result;
}
return CWnd::OnNcHitTest(p);
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(cs.hMenu!=NULL)
{
::DestroyMenu(cs.hMenu);
cs.hMenu = NULL ;
}
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style = WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_OVERLAPPED| WS_SYSMENU | WS_THICKFRAME;
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
void CMainFrame::OnActivate(UINT nState,CWnd* pWndOther,BOOL bMinimized )
{
CFrameWnd::OnActivate(nState,pWndOther,bMinimized);
BOOL fDwmEnabled = FALSE;
if (SUCCEEDED(DwmIsCompositionEnabled(&fDwmEnabled)))
{
if(nState == WA_ACTIVE )
{
MARGINS margins = {-1};
/*margins.cyTopHeight = 30;
margins.cxLeftWidth = 0;
margins.cxRightWidth = 0;
margins.cyBottomHeight = 0;*/
HRESULT hr = DwmExtendFrameIntoClientArea(m_hWnd, &margins);
if (!SUCCEEDED(hr))
TRACE0("Failed in DwmExtendFrameIntoClientArea\n");
}
}
}
void CMainFrame::OnNcPaint()
{
CFrameWnd::OnPaint();
CDC* dc = GetWindowDC();
RECT rcClient;
GetWindowRect(&rcClient);
dc->FillSolidRect(0,0,RECTWIDTH(rcClient),RECTHEIGHT(rcClient),RGB(255,0,0));
CPaintDC gdc(this); // device context for painting
Graphics gr(gdc.m_hDC);
gr.DrawImage ( m_pLogoImage, 0, 0 );
ReleaseDC(dc);
}
The result under Windows 7 is fine.
However, my window appears another unknown caption title bar under Windows 10.
I found out the unknown caption is caused by WS_THICKFRAME in the cs.style.
If I remove WS_THICKFRAME, the unknown cation bar will disappear, but I cannot resizing the border of my window. Furthermore, my program cannot capture the minimum, maximum and the close button message on my custom caption bar anymore.
I want to remove the unknown title bar without any side effect.
Does anyone could provide me a good solution or suggestion?
Best Regards,
When using DwmExtendFrameIntoClientArea, it means frame is extended in to client area. It is no longer in non-client area. So there is no need to override OnNcPaint, you can do all of the painting in OnPaint
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
//paint titlebar area (this used to be the non-client area)
CRect rc;
GetClientRect(&rc);
rc.bottom = titlebar_height;
CDC memdc;
memdc.CreateCompatibleDC(&dc);
BITMAPINFOHEADER bmpInfoHeader = {
sizeof(BITMAPINFOHEADER), rc.Width(), -rc.Height(), 1, 32 };
HBITMAP hbitmap = CreateDIBSection(
dc, (BITMAPINFO*)(&bmpInfoHeader), DIB_RGB_COLORS, NULL, NULL, 0);
auto oldbitmap = memdc.SelectObject(hbitmap);
dc.BitBlt(0, 0, rc.Width(), rc.Height(), &memdc, 0, 0, SRCCOPY);
memdc.SelectObject(oldbitmap);
DeleteObject(hbitmap);
//begin normal paint
//The new client area begins below titlebar_height which we define earlier
GetClientRect(&rc);
rc.top = titlebar_height;
dc.FillSolidRect(&rc, RGB(0, 0, 255));
Gdiplus::Image *image = Gdiplus::Image::FromFile(L"file.jpg");
Gdiplus::Graphics gr(dc);
gr.DrawImage(image, 0, 0);
delete image;
}
Use a member variable CRect m_border to keep track of border's thickness. You can use AdjustWindowRectEx to find the thickness of the borders.
void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
titlebar_height = 100;
//find border thickness
if (GetWindowLongPtr(m_hWnd, GWL_STYLE) & WS_THICKFRAME)
{
m_border = { 0,0,0,0 };
AdjustWindowRectEx(&m_border, GetWindowLongPtr(m_hWnd,
GWL_STYLE) & ~WS_CAPTION, FALSE, NULL);
m_border.left = abs(m_border.left);
m_border.top = abs(m_border.top);
}
else if (GetWindowLongPtr(m_hWnd, GWL_STYLE) & WS_BORDER)
{
m_border = { 1,1,1,1 };
}
else
{
m_border = { 0,0,0,0 };
}
//Extend frame in to client area
MARGINS margins = { 0 };
margins.cyTopHeight = titlebar_height; //<<=== *** edited
DwmExtendFrameIntoClientArea(m_hWnd, &margins);
SetWindowPos(NULL, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
}
m_border will be for example {7,7,7,7};
Allow Windows to do the painting on left, right, bottom border. The top border is the only one changed
void CMainFrame::OnNcCalcSize(BOOL validate, NCCALCSIZE_PARAMS FAR* lpncsp)
{
if (validate)
{
lpncsp->rgrc[0].left += m_border.left;
lpncsp->rgrc[0].right -= m_border.right;
lpncsp->rgrc[0].bottom -= m_border.bottom;
}
else
{
CFrameWnd::OnNcCalcSize(validate, lpncsp);
}
}
see also How to glow the minimum. maximum and close button?

Double buffering for my Menu

i have implemented double buffering in my application, using the code below.
After wards, i found that my child windows (ie, buttoms), was not animating like normal, so i added WS_EX_COMPOSITED to my parent window, and the child windows are now animating correctly. However, after adding WS_EX_COMPOSITED, my Menu that is created from my resource, is black, and not displaying properly.
So, how to add double buffering to my top Menu?
MainWinHwnd = CreateWindowEx(WS_EX_COMPOSITED, MainWinClassName, MainWinTitle,
WS_VISIBLE|WS_BORDER|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SIZEBOX|WS_CLIPCHILDREN,
NULL, NULL, 609, 440, NULL, NULL, hInstance, NULL);
if (!MainWinHwnd) return FALSE;
case WM_PAINT:
{
RECT WinRt;
RECT TxtRt;
HFONT TxtFont = NULL;
SIZE TxtSize;
HDC WinDC = GetDC(hWnd);
GetClientRect(hWnd, &WinRt);
HDC WinBuffer = CreateCompatibleDC(WinDC);
HBITMAP BitBuffer = CreateCompatibleBitmap(WinDC, WinRt.right, WinRt.bottom);
SelectObject(WinBuffer, BitBuffer);
FillRect(WinBuffer, &WinRt, (HBRUSH) (COLOR_BTNFACE + 1));
SetBkColor(WinBuffer, GetSysColor((COLOR_BTNFACE)));
DrawThemeBackground(ButtonThemeData, WinBuffer, BP_GROUPBOX, GBS_NORMAL, &GroupOptionBox, NULL);
DrawThemeBackground(DragDropThemeData, WinBuffer, RP_GRIPPER, 0, &DragDropItem, NULL);
for (DWORD I = 0; I < sizeof(MainWinLabels) / sizeof(CustomLabel); I++)
{
if (MainWinLabels[I].Font == NULL)
{
TxtFont = CreateFontA(MainWinLabels[I].cFontHeight, NULL, NULL, NULL, FW_DONTCARE,
MainWinLabels[I].cFontItalic, MainWinLabels[I].cFontUnderline,
MainWinLabels[I].cFontStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
MainWinLabels[I].cFontQuality, DEFAULT_PITCH, MainWinLabels[I].cFontFaceName);
if (TxtFont == NULL) continue;
SelectObject(WinBuffer, TxtFont);
}
else SelectObject(WinBuffer, *MainWinLabels[I].Font);
SetTextColor(WinBuffer, MainWinLabels[I].Color);
TxtRt.left = MainWinLabels[I].X;
TxtRt.top = MainWinLabels[I].Y;
DrawTextA(WinBuffer, MainWinLabels[I].Text, strlen(MainWinLabels[I].Text), &TxtRt,
DT_LEFT|DT_SINGLELINE|DT_NOCLIP);
GetTextExtentPoint32A(WinBuffer, MainWinLabels[I].Text, strlen(MainWinLabels[I].Text), &TxtSize);
MainWinLabels[I].Width = TxtSize.cx;
MainWinLabels[I].Height = TxtSize.cy;
if (TxtFont != NULL) DeleteObject(TxtFont);
}
BitBlt(WinDC, 0, 0, WinRt.right, WinRt.bottom, WinBuffer, 0, 0, SRCCOPY);
ReleaseDC(hWnd, WinDC);
DeleteObject(BitBuffer);
DeleteDC(WinBuffer);
break;
}
The solution:
Do not use WS_EX_COMPOSITED.
Instead of using GetDC(hWnd) use BeginPaint(hwnd, &ps) and EndPaint(hwnd, &ps)

how to stretch a background image in win32 using visual studio c

I'm trying to create an application in Win32 api with c++ and I want to make it FullScreen without any bar , i succeeded but i still have a problem in the background image. The image is repeated but i want it to be stretched. Have you any idea?
below part from the code :
int WINAPI WinMain (HINSTANCE cetteInstance, HINSTANCE precedenteInstance,
LPSTR lignesDeCommande, int modeDAffichage)
{
HWND fenetrePrincipale;
MSG message;
WNDCLASS classeFenetre;
instance = cetteInstance;
classeFenetre.style = 0;
classeFenetre.lpfnWndProc = procedureFenetrePrincipale;
classeFenetre.cbClsExtra = 0;
classeFenetre.cbWndExtra = 0;
classeFenetre.hInstance = NULL;
classeFenetre.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
classeFenetre.hCursor = LoadCursor(NULL, IDC_ARROW);
// classeFenetre.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
//classeFenetre.hbrBackground = CreatePatternBrush(LoadBitmap( instance, MAKEINTRESOURCE("images\Image1.bmp" ) ) );
HBITMAP hbmp = LoadBitmap(instance,MAKEINTRESOURCE(IDB_BITMAP1));
if(NULL == hbmp)
{
MessageBox(NULL,L"BitMap Loading Failed.",L"Error",MB_ICONEXCLAMATION | MB_OK);
}
else
{
HBRUSH hbr = CreatePatternBrush(hbmp);
if(NULL == hbr)
{
MessageBox(NULL,L"Brush Creation Failed.",L"Error",MB_ICONEXCLAMATION | MB_OK);
}
else
{
//StretchBlt();
HDC hdcMem = GetDC (NULL) ;
HDC wndHDC = GetDC (fenetrePrincipale) ;
StretchBlt(hdcMem, 0, 0, 800, 600, wndHDC, 0, 0, 1280, 1024, SRCCOPY);
classeFenetre.hbrBackground = hbr ;
}
}
classeFenetre.lpszMenuName = NULL;
classeFenetre.lpszClassName = L"classeF";
//fullscreen mode and delete minimize and max buttons
// On prévoit quand même le cas où ça échoue
if(!RegisterClass(&classeFenetre)) return FALSE;
//WS_OVERLAPPEDWINDOW
fenetrePrincipale = CreateWindow(L"classeF", L"Ma premiere fenetre winAPI !",WS_MAXIMIZE|WS_POPUP ,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 630,
NULL,
NULL,//LoadMenu(instance, L"ID_MENU"),
cetteInstance,
NULL);
if (!fenetrePrincipale) return FALSE;
//ShowWindow(fenetrePrincipale, modeDAffichage);
ShowWindow(fenetrePrincipale,SW_MAXIMIZE);
UpdateWindow(fenetrePrincipale);
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
thanks
You haven't shown the exact code, but it appears that you load a bitmap, create a brush from it, and then set that brush as the brush for your window. Brushes would indeed lead to the repeating-image behavior you report. To get a stretched bitmap, you may skip any brush-related code. Instead, handle the WM_ERASEBKGND message sent to your window. In it, call StretchBlt to paint your bitmap onto the client area of your window. The HDC to paint to is given in the message's wParam argument.
Steps
1, CreateWindowEx to create the window
2, SetWindowPos to place your window on top of all windows and Fullscreen
3, On your windows's WindowProce handle WM_PAINT message
4, Load your bitmap
5, Create a memory dc using CreateCompatibleDC
6, Selet your bitmap into memory dc by calling SelectObject
7, Do the StretchBlt to your actual dc, using the prepared memory dc as the source, you should know the actual width and height of the bitmap for proper stretching

DrawIconEx leaving mask artifacts

I'm extracting jumbo icons for any given path using IImageList and SHGetFileInfo. Once I have that, I then render the HICON into a HBITMAP using DrawIconEx for eventual rendering with GDI+ Bitmap and Graphics objects.
Now, this all works great, except that when I do the final rendering of the bitmap, the very left edge always has a black artifact on it. This is true for pretty much any icon I get, and is always the left edge.
Any ideas where the dark line could be coming from?
The code I'm using is roughly:
1. Extract Icon:
// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;
// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;
// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_SCALE|ILD_TRANSPARENT, &hicon);
return hicon;
2. Generate Bitmap
HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, x, y);
HBITMAP hResultBmp = NULL;
HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);
HBRUSH hbr = CreateSolidBrush(bg);
RECT rr = { 0, 0, 256, 256 }; // jumbo icons
FillRect(hMemDC, &rr, hbr);
DeleteBrush(hbr);
DrawIconEx(hMemDC, 0, 0, hicon, size, size, 0, NULL, DI_NORMAL);
hResultBmp = hMemBmp;
hMemBmp = NULL;
SelectObject(hMemDC, hOrgBMP);
return hResultBitmap;
3. Render GDI+ Bitmap to "window bitmap":
Bitmap *b = ::New Bitmap(hResultBitmap, NULL);
Graphics graphics(hdc);
graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
SolidBrush bgbrush(Color(255, 255, 255, 255));
Rect r(0, 0, hwnd_w, hwnd_h);
graphics.FillRectangle(&bgbrush, r);
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Rect r(5, 5, 128, 128);
graphics.DrawImage(dpd->image_to_draw, r);
Wow, I spent another while last night playing with it. It's the ILD_SCALE in IImageList::GetIcon.
Get rid of that and it all works perfectly fine again. Go figure …
1. Extract Icon:
// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;
// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;
// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hicon);
return hicon;

saving to bitmap with win32 API or GDI+

Question is rather simple but I couldn't find nice & clean solution to my problem on the Internet.
What I got are some drawings on my window. Now, I can save those using BitBlt function from window device context to image device context, and also from there to bitmap handle:
HDC bitmapDC = CreateCompatibleDC(dc);
HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
SelectObject(bitmapDC,bitmap);
BitBlt(bitmapDC, 0, 0, 200, 200, dc, 200, 200, SRCCOPY);
But from there I'm lost. I had a look at GDI+ Bitmap class which got save function, and I found how to implement code for retrieving CLSID of picture encoding. However I don't know if I use loading to that class correctly. There's overloaded constructor for HBITMAP, but it's also asking for some palette, which I set to NULL:
Bitmap image(bitmap,NULL);
I tried to save png file but it resulted in black quare without those drawings I was expecting. If you'd like, full code for my painting procedure:
void GetCLSID(const WCHAR* format, CLSID* pClsid){
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
}
}
}
void OnPaint(HDC dc){
RECT rect; rect.bottom = 0; rect.top = 20; rect.left = 0; rect.right = 100;
HBRUSH blueBrush = CreateSolidBrush(RGB(0,0,200));
FillRect(dc, &rect, blueBrush);
Graphics graphics(dc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 200, 100);
SolidBrush greenBrush(Color(0,200,0));
Rect ellipseRect(20,20,20,20);
graphics.FillEllipse(&greenBrush, ellipseRect);
SolidBrush redBrush(Color(200,0,0));
Rect boxRectangle(0,40,20,100);
graphics.FillRectangle(&redBrush, boxRectangle);
pen.SetColor(Color(200,0,200));
pen.SetWidth(20);
graphics.DrawBezier(&pen, 100, 20, 130, 40, 200, 10, 230, 20);
HDC bitmapDC = CreateCompatibleDC(dc);
HBITMAP bitmap = CreateCompatibleBitmap(bitmapDC, 200, 200);
SelectObject(bitmapDC,bitmap);
BitBlt(bitmapDC, 0, 0, 500, 500, dc, 500, 500, SRCCOPY);
Bitmap image(bitmap,NULL);
CLSID clsID;
GetCLSID(L"image/png", &clsID);
image.Save(L"pic.png", &clsID);
}
I couldn't even imagine that simple saving will be such problem, so I'll be glad for any help, thanks!
I gave code here which does pretty much what you want:
How to save the client area of a child Window to a Bitmap file?
It is very verbose in C. It's a lot better in C++ because of CImage