GDI / GDI+
-
hi im trying to code below, but is not working.
void update(HWND hwnd){ HBITMAP pBitmap; pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); // using this it works //Image* pBitmap = new Image(L"clockbg.bmp"); // this is the same image as above, it doesn't work when i use it no errors it compiles fine just no window // i want to use GDI+ to load the image because i want to use a png just trying to get it to work first HDC pDC = GetDC(hwnd); HDC dcMem; dcMem = CreateCompatibleDC(pDC); COLORREF colour; colour = RGB(255,255,255); SelectObject(dcMem,pBitmap); BLENDFUNCTION blend; blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; POINT ptSrc = {0, 0 }; SIZE sz = { 270, 270 }; //int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, ULW_COLORKEY ); int p = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); }
thank you -
hi im trying to code below, but is not working.
void update(HWND hwnd){ HBITMAP pBitmap; pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); // using this it works //Image* pBitmap = new Image(L"clockbg.bmp"); // this is the same image as above, it doesn't work when i use it no errors it compiles fine just no window // i want to use GDI+ to load the image because i want to use a png just trying to get it to work first HDC pDC = GetDC(hwnd); HDC dcMem; dcMem = CreateCompatibleDC(pDC); COLORREF colour; colour = RGB(255,255,255); SelectObject(dcMem,pBitmap); BLENDFUNCTION blend; blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; POINT ptSrc = {0, 0 }; SIZE sz = { 270, 270 }; //int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, ULW_COLORKEY ); int p = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); }
thank youWhats the problem?
WhiteSky
-
Whats the problem?
WhiteSky
-
pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); // ^ this shows the window Image* pBitmap = new Image(L"clockbg.bmp"); ^ this does not they are the same image loaded differently. thank you
Lamefif wrote:
Image* pBitmap = new Image(L"clockbg.bmp"); ^ this does not
Maybe you need the full pathname of the file? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); // ^ this shows the window Image* pBitmap = new Image(L"clockbg.bmp"); ^ this does not they are the same image loaded differently. thank you
And where's clockbg.bmp(path of file)?
WhiteSky
-
And where's clockbg.bmp(path of file)?
WhiteSky
-
the image is in my project folder and i know it loads because i can display it like this: Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); and it shows :confused: thanks
Bitmap is derived from Image. If Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); works, then Image* pBitmap = new Image(L"clockbg.bmp"); graphics.DrawImage(pBitmap, 0, 0); should work. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Bitmap is derived from Image. If Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 0, 0); works, then Image* pBitmap = new Image(L"clockbg.bmp"); graphics.DrawImage(pBitmap, 0, 0); should work. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
i know Mark thats not my problem, but when i use GDI+ to load the image: Image* pBitmap = new Image(L"clockbg.bmp"); or Bitmap. as appose to: HBITMAP pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); and i use it for my: UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); the window doesn't show, as i've posted in the code above is the same image and i now it loads, i dont now.. thank you
-
i know Mark thats not my problem, but when i use GDI+ to load the image: Image* pBitmap = new Image(L"clockbg.bmp"); or Bitmap. as appose to: HBITMAP pBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1)); and i use it for my: UpdateLayeredWindow(hwnd, NULL, NULL, &sz, dcMem, &ptSrc, colour, &blend, LWA_ALPHA ); the window doesn't show, as i've posted in the code above is the same image and i now it loads, i dont now.. thank you
Sorry about that - I should have read the first post more closely :) Maybe this will work better...
Image* pBitmap = new Image(L"clockbg.bmp");
HDC pDC = GetDC(hwnd);
HDC dcMem;
dcMem = CreateCompatibleDC(pDC);
COLORREF colour;
colour = RGB(255,255,255);// Assuming white is the transparent color
HBITMAP hBitmap; pBitmap->GetHBITMAP(Color(0xFF,0xFF,0xFF), &hBitmap);
SelectObject(dcMem, hBitmap);
..."Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Sorry about that - I should have read the first post more closely :) Maybe this will work better...
Image* pBitmap = new Image(L"clockbg.bmp");
HDC pDC = GetDC(hwnd);
HDC dcMem;
dcMem = CreateCompatibleDC(pDC);
COLORREF colour;
colour = RGB(255,255,255);// Assuming white is the transparent color
HBITMAP hBitmap; pBitmap->GetHBITMAP(Color(0xFF,0xFF,0xFF), &hBitmap);
SelectObject(dcMem, hBitmap);
..."Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Thanks Mark, but my aim is to use the transparency of the png and Alpha channel to define the transparency, perpixelvlue as it were. am goind all wrong about it? thanks
I "think" you may be able to use a 32-bit ARGB DIBsection - I put together this test just to make sure the alpha channel stuff was working. (My "hdc" would be your "dcMem" and you wouldn't want to delete it or un-select your alpha bitmap like I have if you pass it to UpdateLayeredWindow())
Image TransparentSrcBitmap(L"clockbg.bmp");
LONG lImageWidth = TransparentSrcBitmap.GetWidth();
LONG lImageHeight = TransparentSrcBitmap.GetHeight();
WORD wBitsPerPixel = 32;//LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
LONG lBytesPerRow = lImageWidth * 4;BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = lImageWidth;
bmi.bmiHeader.biHeight = lImageHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = wBitsPerPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
//bmi.bmiHeader.biXPelsPerMeter = 0;
//bmi.bmiHeader.biYPelsPerMeter = 0;
//bmi.bmiHeader.biClrUsed = 0;
//bmi.bmiHeader.biClrImportant = 0;HDC hdc = ::CreateCompatibleDC(0);
BYTE* pBitmapBits;
HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);if (hBitmap)
{
memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);
Graphics DstGraphics(hdc);
DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());//** Test - blt the memory dc to the screen to make sure alpha channel data carried over
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0x7F; // Alpha multiplier applied overall - use 0xFF - 0x7F is a test
bf.AlphaFormat = AC_SRC_ALPHA;HDC hClientDC = ::GetDC(*this);
::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(),
hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf);
::ReleaseDC(*this, hClientDC);
//** End Test::SelectObject(hdc, hOldBitmap);
//<-- Don't do this when using with UpdateLayeredWindow()
::DeleteObject(hBitmap);
//<-- Don't do this when using with UpdateLayeredWindow()
}::DeleteDC(hdc);
//<-- Don't do this when using with UpdateLayeredWindow()
Sorry about all the strange extra variables - I cut and paste from assorted worki
-
I "think" you may be able to use a 32-bit ARGB DIBsection - I put together this test just to make sure the alpha channel stuff was working. (My "hdc" would be your "dcMem" and you wouldn't want to delete it or un-select your alpha bitmap like I have if you pass it to UpdateLayeredWindow())
Image TransparentSrcBitmap(L"clockbg.bmp");
LONG lImageWidth = TransparentSrcBitmap.GetWidth();
LONG lImageHeight = TransparentSrcBitmap.GetHeight();
WORD wBitsPerPixel = 32;//LONG lBytesPerRow = (((lImageWidth * (long)wBitsPerPixel + 31L) & (~31L)) / 8L);
LONG lBytesPerRow = lImageWidth * 4;BITMAPINFO bmi;
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = lImageWidth;
bmi.bmiHeader.biHeight = lImageHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = wBitsPerPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = lBytesPerRow * lImageHeight;
//bmi.bmiHeader.biXPelsPerMeter = 0;
//bmi.bmiHeader.biYPelsPerMeter = 0;
//bmi.bmiHeader.biClrUsed = 0;
//bmi.bmiHeader.biClrImportant = 0;HDC hdc = ::CreateCompatibleDC(0);
BYTE* pBitmapBits;
HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, NULL, 0);if (hBitmap)
{
memset(pBitmapBits, 0, bmi.bmiHeader.biSizeImage);HGDIOBJ hOldBitmap = ::SelectObject(hdc, hBitmap);
Graphics DstGraphics(hdc);
DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());//** Test - blt the memory dc to the screen to make sure alpha channel data carried over
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0x7F; // Alpha multiplier applied overall - use 0xFF - 0x7F is a test
bf.AlphaFormat = AC_SRC_ALPHA;HDC hClientDC = ::GetDC(*this);
::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(),
hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf);
::ReleaseDC(*this, hClientDC);
//** End Test::SelectObject(hdc, hOldBitmap);
//<-- Don't do this when using with UpdateLayeredWindow()
::DeleteObject(hBitmap);
//<-- Don't do this when using with UpdateLayeredWindow()
}::DeleteDC(hdc);
//<-- Don't do this when using with UpdateLayeredWindow()
Sorry about all the strange extra variables - I cut and paste from assorted worki
thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D
-
thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D
No problem! Can you show your latest code? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
thank you Mark very much for taking the time, it mean as lot me. but is not quite there is coming out pixalated, the alpha value is being replace by a black colour. program running image: http://img118.imageshack.us/img118/8953/testwindowhu9.png . . . Code: the main method. http://rafb.net/p/1Dfouz58.html thank you so much ....:-D
What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
What's "image" and what's "program window" in your screenshot? Never mind I get it LOL I'd still like to see your updated code if you can :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here
-
Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here
Try replacing int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_COLORKEY ); with int t = UpdateLayeredWindow(hwnd, NULL, NULL, &sz, hdc, &ptSrc, NULL, &bf, ULW_ALPHA ); You probably want to set bf.SourceConstantAlpha = 0xFF; (instead of 0x7F) too. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Code: the main method. http://rafb.net/p/1Dfouz58.html ^i posted here
And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
And comment out these lines - they were there for testing only (leave in the BLENDFUNCTION since you're using that)... // comment these out HDC hClientDC = ::GetDC(hwnd); ::AlphaBlend(hClientDC, 50, 50, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), hdc, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight(), bf); ::ReleaseDC(hwnd, hClientDC);
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol
-
thanks a million one more thing since i've got you here. will i be able to draw on this window? i.e: Graphics graphics(GetDC(hwnd)); Bitmap bitmap(L"clockbg.bmp"); graphics.DrawImage(&bitmap, 100,100); i did a quick test it dont seem to be working. my goal would be to be as great as you one day lol
Lamefif wrote:
will i be able to draw on this window?
Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...
// redraw the image background
DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());// draw some stuff on top of the background
Bitmap bitmap(L"clockbg.bmp");
DstGraphics.DrawImage(&bitmap, 100,100);You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Lamefif wrote:
will i be able to draw on this window?
Absolutely not! Just kidding. The DIBSection bitmap is already in a DC which is associated with a Graphics object (DstGraphics). That's where you should draw. Example...
// redraw the image background
DstGraphics.DrawImage(&TransparentSrcBitmap, 0, 0, TransparentSrcBitmap.GetWidth(), TransparentSrcBitmap.GetHeight());// draw some stuff on top of the background
Bitmap bitmap(L"clockbg.bmp");
DstGraphics.DrawImage(&bitmap, 100,100);You have full access to the bitmap that IS your window. You can write directly to the pixel bytes, use GDI+ to draw, use GDI to draw, or any combination of those :) You may need to call UpdateLayeredWindow() again to see changes (i think the hdcSrc param can be NULL for updates). Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder