Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a transparent bitmap and UpdateLayeredWindow
    primarykey
    data
    text
    <p>I'm trying to create a mask window with "loading" text, to alert the user about a busy state of my application.</p> <p>For this, I first created a single form with:</p> <ul> <li>BorderStyle = bsNone</li> <li>Color = clBlack</li> <li>AlphaBlend = True</li> <li>AlphaBlendValue = 180;</li> </ul> <p>As the second step, I want to create another Form, but this one will has dynamic content. I need to create a transparent bitmap with some status text and use the UpdateLayeredWindow to draw the window as the text.</p> <p>Take a look and my desired result:</p> <p><img src="https://i.stack.imgur.com/l69Eq.jpg" alt="enter image description here"></p> <p>Remember: the text will be different in some cases, like:</p> <ul> <li>Recalculating</li> <li>Loading resources</li> <li>Loading report</li> </ul> <p>That's the reason what I need a dynamic bitmap generation.</p> <p><strong>QUESTION</strong></p> <p>How can I create a transparent bitmap with text and use it on a form with UpdateLayeredWindow?</p> <p>I'm trying this, but without success ( to try the code put a Button5 and Label2 on a form):</p> <pre><code> procedure Inc(var p: pointer); begin p := Pointer(Integer(p) + 1); end; var s: string; frm: TForm; f: HFont; tx: HDC; bmp, old: HBITMAP; rc: TRect; h: BITMAPINFOHEADER; pvBits: Pointer; t: tagBITMAPINFO; x,y: integer; a, r, g, b: byte; sz: TSize; p: tpoint; BlendFunction: TBlendFunction; begin tx := CreateCompatibleDC(0); s := label2.Caption; f := SelectObject(tx, label2.Font.Handle); fillchar(rc, SizeOf(rc), 0); DrawText(tx, PChar(s), length(s), rc, DT_CALCRECT); fillchar(h, SizeOf(h), 0); pvBits := nil; h.biSize := SizeOf(h); h.biWidth := rc.Right - rc.Left; h.biHeight := rc.Bottom - rc.Top; h.biPlanes := 1; h.biBitCount := 32; h.biCompression := BI_RGB; FillChar(t, SizeOf(t), 0); t.bmiHeader := h; bmp := CreateDIBSection(tx, t, 0, pvBits, 0, 0); old := SelectObject(tx, bmp); if old &gt; 0 then begin SetTextColor(tx, $00FFFFFF); SetBkColor(tx, $00000000); SetBkMode(tx, TRANSPARENT); DrawText(tx, PChar(s), length(s), rc, DT_NOCLIP); r := GetRValue($FF); g := GetGValue($FF); b := GetBValue($FF); for x := 0 to h.biWidth-1 do for y := 0 to h.biHeight-1 do begin a := Byte(pvBits^); Inc(pvBits); Byte(pvBits^) := (b * a) shr 8; Inc(pvBits); Byte(pvBits^) := (g * a) shr 8; Inc(pvBits); Byte(pvBits^) := (r * a) shr 8; Inc(pvBits); Byte(pvBits^) := a; end; SelectObject(tx, old); end; SelectObject(tx, f); deleteDC(tx); sz.cx := h.biWidth; sz.cy := h.biHeight; p := Point(0,0); BlendFunction.BlendOp := AC_SRC_OVER; BlendFunction.BlendFlags := 0; BlendFunction.SourceConstantAlpha := 255; BlendFunction.AlphaFormat := AC_SRC_ALPHA; frm := TForm.CreateNew(self); frm.BorderStyle := bsNone; frm.Position := poOwnerFormCenter; frm.Show; UpdateLayeredWindow(frm.Handle, 0, nil, @sz, bmp, @p, 0, @BlendFunction, ULW_ALPHA); end; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload