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)
Related
I'm working on a C++ ATL COM thumbnail/preview/search project, but its bitmap displaying code behavior is monochrome during the Thumbnail process instead of the colored. The Preview process is colored as expected, using the same function.
I used the ATL Wizard to create the IThumbnailProvider and his friends. My small changes are: I replaced the color from black to pink in the document::OnDrawThumbnail and I wrote the document::OnDrawThumbnail into CPreviewCtrl::DoPaint. I've read the "new DC always monochrome" thing in the MS spec but I could not get colored DC even if a changed the original ATL code OnDrawThumbnail(GetDC(NULL), &rcBounds);. The CreateCompatibleDC(NULL) and CreateCompatibleDC(hDrawDC) were dead-end too.
document.cpp (changed)
// Almost the default sample code, but hDrawBrush is changed to pink
void document::OnDrawThumbnail(HDC hDrawDC, LPRECT lprcBounds)
{
HBRUSH hDrawBrush = CreateSolidBrush(RGB(255, 0, 255)); // pink
FillRect(hDrawDC, lprcBounds, hDrawBrush);
HFONT hStockFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
LOGFONT lf;
GetObject(hStockFont, sizeof(LOGFONT), &lf);
lf.lfHeight = 34;
HFONT hDrawFont = CreateFontIndirect(&lf);
HFONT hOldFont = (HFONT) SelectObject(hDrawDC, hDrawFont);
CString strText = _T("TODO: implement thumbnail drawing here");
DrawText(hDrawDC, strText, strText.GetLength(), lprcBounds, DT_CENTER | DT_WORDBREAK);
SelectObject(hDrawDC, hDrawFont);
SelectObject(hDrawDC, hOldFont);
DeleteObject(hDrawBrush);
DeleteObject(hDrawFont);
}
PreviewHandler.h (changed, it is called by the Preview)
// CPreviewCtrl implementation
class CPreviewCtrl : public CAtlPreviewCtrlImpl
{
protected:
virtual void DoPaint(HDC hdc)
{
// you can obtain a pointer to IDocument as follows
// CMyDoc* pDoc = (CMyDoc*)m_pDocument;
/*
CString strData = _T("Draw Rich Preview content here.");
TextOut(hdc, 10, 20, strData, strData.GetLength());
*/
RECT rc{};
rc.right = 290;
rc.bottom = 290;
dynamic_cast<document*>(m_pDocument)->OnDrawThumbnail(hdc, &rc);
}
};
atlhandlerimpl.h (unchanged, from VS SDK \atlmfc\include\ which is called be the thumbnail provider)
ATLPREFAST_SUPPRESS(6101)
_Success_(return != FALSE) BOOL GetThumbnail(
_In_ UINT cx,
_Out_ HBITMAP* phbmp,
_Out_ WTS_ALPHATYPE* /* pdwAlpha */)
{
HDC hdc = ::GetDC(NULL);
RECT rcBounds;
SetRect(&rcBounds, 0, 0, cx, cx);
HDC hDrawDC = CreateCompatibleDC(hdc);
if (hDrawDC == NULL)
{
ReleaseDC(NULL, hdc);
return FALSE;
}
HBITMAP hBmp = CreateCompatibleBitmap(hDrawDC, cx, cx);
if (hBmp == NULL)
{
ReleaseDC(NULL, hdc);
DeleteDC(hDrawDC);
return FALSE;
}
HBITMAP hOldBitmap = (HBITMAP) SelectObject(hDrawDC, hBmp);
// Here you need to draw the document's data
OnDrawThumbnail(hDrawDC, &rcBounds);
SelectObject(hDrawDC, hOldBitmap);
DeleteDC(hDrawDC);
ReleaseDC(NULL, hdc);
*phbmp = hBmp;
return TRUE;
}
ATLPREFAST_UNSUPPRESS()
Sample thumbnail in the File Explorer
Github helped me. It is definitely an ATL SDK bug.
BUG report on the VS developer community
Solution on the www.patthoyts.tk
And the github repo which helped me: abhimanyusirohi/ThumbFish
In the atlhandlerimpl.h provided GetThumbnail must be override:
BOOL document::GetThumbnail(
_In_ UINT cx,
_Out_ HBITMAP * phbmp,
_Out_ WTS_ALPHATYPE* /* pdwAlpha */)
{
BOOL br = FALSE;
HDC hdc = ::GetDC(NULL);
HDC hDrawDC = CreateCompatibleDC(hdc);
if (hDrawDC != NULL)
{
void* bits = 0;
RECT rcBounds;
SetRect(&rcBounds, 0, 0, cx, cx);
BITMAPINFO bi = { 0 };
bi.bmiHeader.biWidth = cx;
bi.bmiHeader.biHeight = cx;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biSizeImage = 0;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
if (hBmp != NULL)
{
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hDrawDC, hBmp);
OnDrawThumbnail(hDrawDC, &rcBounds);
SelectObject(hDrawDC, hOldBitmap);
*phbmp = hBmp;
br = TRUE;
}
DeleteDC(hDrawDC);
}
ReleaseDC(NULL, hdc);
return br;
}
I am using Visual Studio 2017, I wrote code which should create a folder, capture a screenshot when the mouse button is pressed and save the screenshot to a .bmp file. But I don't know why that script does not work. Visual Studio compile it without errors / warnings.
Here is the code:
// variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
// or you will get problems since every function uses this variable.
HHOOK _hook;
// This struct contains the data received by the hook callback. As you see in the callback function
// it contains the thing you will need: vkCode = virtual key code.
KBDLLHOOKSTRUCT kbdStruct;
int filenum = 1;
// This is the callback function. Consider it the event that is raised when, in this case,
// a key is pressed.
void TakeScreenShot(const char* filename)
{
//keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
//keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
HBITMAP h;
POINT a, b;
a.x = 0;
a.y = 0;
b.x = GetSystemMetrics(SM_CXSCREEN);
b.y = GetSystemMetrics(SM_CYSCREEN);
HDC hScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, abs(b.x - a.x), abs(b.y - a.y));
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BOOL bRet = BitBlt(hDC, 0, 0, abs(b.x - a.x), abs(b.y - a.y), hScreen, a.x, a.y, SRCCOPY);
// save bitmap to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
// clean up
SelectObject(hDC, old_obj);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
OpenClipboard(NULL);
h = (HBITMAP)GetClipboardData(CF_BITMAP);
CloseClipboard();
HDC hdc = NULL;
FILE*fp = NULL;
LPVOID pBuf = NULL;
BITMAPINFO bmpInfo;
BITMAPFILEHEADER bmpFileHeader;
do
{
hdc = GetDC(NULL);
ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(hdc, h, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS);
if (bmpInfo.bmiHeader.biSizeImage <= 0)
bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount + 7) / 8;
if ((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage)) == NULL)
{
MessageBox(NULL, TEXT("Unable to Allocate Bitmap Memory"), TEXT("Error"), MB_OK | MB_ICONERROR);
break;
}
bmpInfo.bmiHeader.biCompression = BI_RGB;
GetDIBits(hdc, h, 0, bmpInfo.bmiHeader.biHeight, pBuf, &bmpInfo, DIB_RGB_COLORS);
if ((fp = fopen(filename, "wb")) == NULL)
{
MessageBox(NULL, TEXT("Unable to Create Bitmap File"), TEXT("Error"), MB_OK | MB_ICONERROR);
break;
}
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmpInfo.bmiHeader.biSizeImage;
bmpFileHeader.bfType = 'MB';
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fwrite(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(pBuf, bmpInfo.bmiHeader.biSizeImage, 1, fp);
}
while (false);
if (hdc)ReleaseDC(NULL, hdc);
if (pBuf) free(pBuf);
if (fp)fclose(fp);
}
LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
// the action is valid: HC_ACTION.
if (wParam == WM_LBUTTONDOWN)
{
std::string OutputFolder = "C:\\temp";
std::string filename = "ss";
if (CreateDirectory(OutputFolder.c_str(), NULL) ||
ERROR_ALREADY_EXISTS == GetLastError())
{
}
else
{
// Failed to create directory.
}
auto numfile = std::to_string(filenum);
TakeScreenShot((OutputFolder + "\\" + filename + std::to_string(filenum) + ".bmp").c_str());
filenum++;
}
}
// call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
void ReleaseHook()
{
UnhookWindowsHookEx(_hook);
}
int main()
{
// Don't mind this, it is a meaningless loop to keep a console application running.
// I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
}
}
If click with it does not make the directory (if it does not exist) and does not create the .bmp file.
As others have said, you never install your hook! Also, it appears from my tests that you need to be dispatching messages to a window of some sort in order for a WH_MOUSE hook to get called.
Here's a minimal version of main () that works for me:
int main()
{
_hook = SetWindowsHookEx (WH_MOUSE, HookCallback, NULL, GetCurrentThreadId ());
if (_hook == NULL)
...
MessageBox (NULL, "Click OK to quit", "Screen Grabber", MB_OK);
UnhookWindowsHookEx (_hook);
}
Then the rest of your code works fine, albeit is a little bit messy as others have said.
However, this will only catch mouse-clicks within the message box itself, and I don't think that's what you want.
If you want to catch these globally, you will need to install a "low level" mouse hook. This needs to be a global hook but otherwise the code looks much the same. The code to install and run the hook is:
int main()
{
_hook = SetWindowsHookEx (WH_MOUSE_LL, HookCallback, NULL, 0);
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
DispatchMessage (&msg);
UnhookWindowsHookEx(_hook);
}
I am attempting to load in a selected .bmp file and display it in WM_PAINT. The function is being ran by the code so visual studio does see it.
here is the code that seems to be not working:
case WM_PAINT:
{
_hWindowDC = BeginPaint(_hwnd, &ps);
if (g_bIsFileLoaded)
{
std::wstring wsFileName = g_vecImageFileNames.back();
g_vecImageFileNames.pop_back();
std::string File(wsFileName.length(), ' ');
std::copy(wsFileName.begin(), wsFileName.end(), File.begin());
HBITMAP h_BitMap = (HBITMAP)LoadImageA(g_hInstance, File.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC h_HomeDC = CreateCompatibleDC(_hWindowDC);
BITMAP BitMap;
int i_Final = GetObject(reinterpret_cast<HGDIOBJ>(h_BitMap), sizeof(BITMAP), reinterpret_cast<LPVOID>(&BitMap));
HBITMAP h_OldBit = (HBITMAP)::SelectObject(h_HomeDC, h_BitMap);
SetStretchBltMode(_hWindowDC, HALFTONE);
BOOL PaintBit = StretchBlt(_hWindowDC, 1, 1, 50, 50, h_HomeDC, 0, 0, BitMap.bmWidth, BitMap.bmHeight, SRCCOPY);
::SelectObject(h_HomeDC, h_OldBit);
::DeleteDC(h_HomeDC);
::DeleteObject(h_BitMap);
InvalidateRect(_hwnd, NULL, NULL);
g_bIsFileLoaded = false;
}
EndPaint(_hwnd, &ps);
return (0);
}
break;
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
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();