Recent

Author Topic: KOL CE - Paint Box - AlphaBlend  (Read 8068 times)

Nibbler

  • New Member
  • *
  • Posts: 11
KOL CE - Paint Box - AlphaBlend
« on: November 09, 2008, 01:56:15 am »
Is it possible to use AlphaBlend function (http://msdn.microsoft.com/en-us/library/aa920812.aspx) with KOL CE inside a PaintBox ?
What are the steps to translate/import a function from its msdn description, then use it in lazarus with KOL CE ?

Thanks in advance  8)

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 158
RE: KOL CE - Paint Box - AlphaBlend
« Reply #1 on: November 10, 2008, 12:31:41 pm »
Just import this function in your code like it done in Windows unit. Then you csn use it in OnPaint event handler.

Nibbler

  • New Member
  • *
  • Posts: 11
KOL CE - Paint Box - AlphaBlend
« Reply #2 on: November 11, 2008, 12:34:52 am »
I tried to use the function with ImageDecompress image ...

I imported the function like this :

Code: [Select]
function AlphaBlend(hdcDest:HDC; nXOriginDest, nYOriginDest, nWidthDest, nHeightDest: Integer; hdcSrc: HDC; nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc: Integer; blendFunction: TBlendFunction): BOOL; external 'coredll.dll' name 'AlphaBlend';

declared

Code: [Select]
blend : TBlendFunction;
...
blend.SourceConstantAlpha:=128;


then I load an image :

Code: [Select]
image:= NewImageDecompress;
image.LoadFromFile(extractFilePath(paramstr(0))+'test.png');


then I try to paint alphablended in the OnPaint handler

Code: [Select]
procedure TmainForm.PaintBoxPaint(Sender: PControl; DC: HDC);
var aRect: tRect;
begin
  aRect.Left:= 0;
  aRect.Top := 0;
  aRect.Right  := aRect.left + sender^.width;
  aRect.Bottom := aRect.top +  sender^.height;

  with pcontrol(sender).canvas^ do
  begin
    randomize;
    brush.color := clBlue;
    brush.BrushStyle:=bsSolid;
    FillRect(aRect);
  end;

  if image.Bitmap<>nil then
  begin
    AlphaBlend(DC,0,0,16,16,image.Bitmap.Handle,0,0,16,16,blend);
  end;
end;



but it doesn't work :(

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 158
KOL CE - Paint Box - AlphaBlend
« Reply #3 on: November 11, 2008, 10:51:35 am »
First try to use simple BitBlt instead of AlphaBlend. If it will work, then you need to dig for some AlphaBlend usage examples in the Internet.

You imported AlphaBlend properly. Just one note. All wince APIs have cdecl calling convention. It is not important for ARM, but very important for i386...

Nibbler

  • New Member
  • *
  • Posts: 11
KOL CE - Paint Box - AlphaBlend
« Reply #4 on: November 11, 2008, 07:48:04 pm »
Hi Yury, following your advice I succeeded using AlphaBlend function  8)

Yet unfortunatelly, using AlphaBlend with ImageDecompress unit seems a no way as the Alpha channel is lost when loading the image  :cry:

Furthermore it seems that DecompressImageIndirect used in ImageDecompress is going to be deprecated and starting from WM5 and above it is advised to use Image Factory :



Code: [Select]
HBITMAP LoadImageWithImagingApi(const CString &strFileName)
{
    IImagingFactory *pImgFactory = NULL;
    IImage *pImage = NULL;
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
   HBITMAP hResult = 0;
    if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory,
                                    NULL,
                                    CLSCTX_INPROC_SERVER,
                                    IID_IImagingFactory,
                                    (void **)&pImgFactory)))
    {
      ImageInfo imageInfo;
        if (SUCCEEDED(pImgFactory->CreateImageFromFile(strFileName, &pImage))
         && SUCCEEDED(pImage->GetImageInfo(&imageInfo)))
        {
         CWindowDC dc(0);
         CDC dcBitmap;
         dcBitmap.CreateCompatibleDC(&dc);
         hResult = CreateCompatibleBitmap(dc.GetSafeHdc(), imageInfo.Width, imageInfo.Height);
         if (hResult) {
            HGDIOBJ hOldBitmap = dcBitmap.SelectObject(hResult);
              pImage->Draw(dcBitmap.GetSafeHdc(), CRect(0, 0, imageInfo.Width, imageInfo.Height), NULL);
            dcBitmap.SelectObject(hOldBitmap);
         }
         pImage->Release();
        }
        pImgFactory->Release();
    }
    CoUninitialize();

   return hResult;
}


is it possible to translate this code to pascal / Lazarus / Kol CE ?

Thank you for your help !

 

TinyPortal © 2005-2018