Einzelnen Beitrag anzeigen

venice2
(Gast)

n/a Beiträge
 
#8

AW: Wecker - ELEGANTE Lösung mit Abbruch via Button gesucht

  Alt 24. Mai 2022, 18:56
Den Sound kann man Async abspielen, somit hat der Hauptthread garnichts zu tun und ein Thread ist garnicht nötig.

Fazit: Die eleganzeste Lösung ist ein Timer.
Du weist doch gar nicht was alles noch innerhalb des Threads behandelt werden soll.
Abfrage der Zeiten und..und..und
Fazit: Daher ist die eleganteste Lösung diese mit der Sie am besten zurecht kommt Code bedingt.

Ich Render zum Beispiel noch einige Bilder innerhalb des Thread deshalb habe ich ihn umgeschrieben und hier angeboten.
Glaube es oder nicht der hat genug zu tun. (In meiner Anwendung)
Delphi-Quellcode:
procedure Animate(Delay: Integer); stdcall;
var
  Brush: HBrush;
  StepX: Integer;
  DesktopDC: HDC;
  lpSize: TSize;
  graphics: LONG_PTR;
  rc: TRect;
  rw: TRect;
  bf: BLENDFUNCTION;
  lp, ptSrc: TPoint;
// label DoLoop;
begin

  GetWindowRect(gP.Handle, rc);
  lpSize.cx := rc.right - rc.left;
  lpSize.cy := rc.bottom - rc.top;
  SetRect(rc, 0, 0, lpSize.cx, lpSize.cy);

  if (gP.img = 0) then
    exit;

  bf.BlendOp := AC_SRC_OVER;
  bf.BlendFlags := 0;
  bf.AlphaFormat := AC_SRC_ALPHA;
  bf.SourceConstantAlpha := 255;

// DoLoop:
  Repeat
    if (gP.bitmap = 0) then
    begin
      DesktopDC := GetDC(0);
      gP.DC := CreateCompatibleDC(DesktopDC);
      gP.bitmap := CreateDIBSection32(gP.DC, lpSize.cx, lpSize.cy);
      SelectObject(gP.DC, gP.bitmap);
      ReleaseDC(0, DesktopDC);
    end;

    if (gP.bitmap <> 0) then
    begin
      GetWindowRect(gP.Handle, rw);

      StepX := 1;
      lp.y := rw.top;

      if (rw.right > GetSystemMetrics(0)) and (HideIt = 0) then
      begin
        lp.x := rw.left - StepX;
      end else
      if (Sound = 0) then
      begin
        Sound := -1;

        inc(PlayThis);
        if (PlayThis = 2) then
          PlaySound('SOUND2', gP.instance, SND_RESOURCE or SND_ASYNC)
        else if (PlayThis < 2) then
          PlaySound('SOUND1', gP.instance, SND_RESOURCE or SND_ASYNC);
      end;

      if (PlayThis > 2) then
        Playthis := 3;

      if (HideIt = 3) then
      begin
        if rw.left < GetSystemMetrics(0) - 25 then
          lp.x := rw.left + StepX
        else
        begin
          HideIt := 0;
          Sound := 0;
        end
      end;

      Brush := CreateSolidBrush(0);
      SelectObject(gP.DC, Brush);
      FillRect(gP.DC, rc, Brush);
      DeleteObject(Brush);

      if (GDIP_CreateFromHDC(gP.DC, graphics) = OK) then
      begin
        inc(gP.frametouse);
        if (gP.frametouse > gP.framecount) then
        begin
          gP.frametouse := 1;
          if rw.right < GetSystemMetrics(0) + 1 then
          inc(HideIt);
        end;

        if (gP.img <> 0) then
          GDIP_DrawImageRectRectI(graphics, gP.img, 0, 0, lpSize.cx, lpSize.cy,
            lpSize.cx * gP.frametouse - lpSize.cx, 0, lpSize.cx, lpSize.cy, 2, nil, nil, nil);

        GDIP_DeleteGraphics(graphics);
      end;

      UpdateLayeredWindow(gP.Handle, 0, @lp, @lpSize, gP.DC, @ptSrc, 0, @bf, ULW_ALPHA);
      Sleep(Delay);
    end;

  Until (EndAnimate = True);
// Goto DoLOOP;

end;
Wie gesagt kann nicht riechen was ihr da so vorschwebt!
Aber eine allgemeine Aussage was richtig ist oder falsch muß sie selbst entscheiden.
So hat Sie auf jedenfalls einen anderen Ansatz.

Geändert von venice2 (24. Mai 2022 um 19:06 Uhr)
  Mit Zitat antworten Zitat