全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2815
推到 Plurk!
推到 Facebook!

請問 要如何 繪製 半透明 bmp

尚未結案
bearlin
一般會員


發表:3
回覆:3
積分:1
註冊:2004-07-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-08 03:03:47 IP:59.113.xxx.xxx 未訂閱
請問 要繪製 半透明 bmp ,兩張重疊時要怎麼寫阿?? 我有搜尋 透明的,不過都是 說明 form 視窗如何全部透明,不然就是元件如何全部透明,我只想單純先會一張bmp1 然後再繪製 另一張bmp2 蓋在原來bmp1 上 還有半透明的效果BMP有像 alpha 這種東西參數ㄇ? 請高手 知道的 幫我一下 拜託~~~
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-08 08:49:09 IP:202.39.xxx.xxx 未訂閱
試試 Graphics32 這套元件: http://www.g32.org/graphics32/index.html#Downloads    與參考 "make a Form semi-/ transparent" http://www.swissdelphicenter.ch/torry/printcode.php?id=924
TMyForm = class (TForm);    TrackBar1: TTrackBar;
//..
private
FColorKey: TCOLOR;    end;    const
  // Use crKey as the transparency color.
  LWA_COLORKEY = 1;
  // Use bAlpha to determine the opacity of the layered window..
  LWA_ALPHA     = 2;
  WS_EX_LAYERED = $80000;    implementation    {$R *.DFM}    // The SetLayeredWindowAttributes function sets the opacity and transparency
// color key of a layered window.
// Note : This function is only available with Windows 2000 and XP!
function SetLayeredWindowAttributes(
  // Handle to the layered window.
  Wnd: hwnd;
  // Pointer to a COLORREF value that specifies the transparency
  // color key to be used when composing the layered window
  crKey: ColorRef;
  // Alpha value used to describe the opacity of the layered window
  Alpha: Byte;
  // Specifies an action to take
  // LWA_COLORKEY or LWA_ALPHA
  // This parameter can be one or more of the following values:
  dwFlags: DWORD): Boolean;
  stdcall; external 'user32.dll';    procedure TMyForm.TrackBar1Change(Sender: TObject);
  // Trackbar.Position must run at range 1-255...
begin
  SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
  SetLayeredWindowAttributes(Handle, ColorToRGB(FColorKey), TrackBar1.Position, LWA_ALPHA);
end;    {******************************************************************}    { To load the SetLayeredWindowAttributes() function dynamically, use this function:}    function MakeWindowTransparent(Wnd: HWND; nAlpha: Integer = 10): Boolean;
type
  TSetLayeredWindowAttributes = function(hwnd: HWND; crKey: COLORREF; bAlpha: Byte;
    dwFlags: Longint): Longint; 
  stdcall;
const
  // Use crKey as the transparency color.
  LWA_COLORKEY = 1;
  // Use bAlpha to determine the opacity of the layered window..
  LWA_ALPHA     = 2;
  WS_EX_LAYERED = $80000;
var
  hUser32: HMODULE;
  SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
  i: Integer;
begin
  Result := False;
  // Here we import the function from USER32.DLL
  hUser32 := GetModuleHandle('USER32.DLL');
  if hUser32 <> 0 then
  begin @SetLayeredWindowAttributes := GetProcAddress(hUser32, 'SetLayeredWindowAttributes');
    // If the import did not succeed, make sure your app can handle it!
    if @SetLayeredWindowAttributes <> nil then
    begin
      // Check the current state of the dialog, and then add the WS_EX_LAYERED attribute
      SetWindowLong(Wnd, GWL_EXSTYLE, GetWindowLong(Wnd, GWL_EXSTYLE) or WS_EX_LAYERED);
      // The SetLayeredWindowAttributes function sets the opacity and
      // transparency color key of a layered window
      SetLayeredWindowAttributes(Wnd, 0, Trunc((255 / 100) * (100 - nAlpha)), LWA_ALPHA);
      Result := True;
    end;
  end;
end;
-- 分擔可以輕省, 分享帶來喜樂!
8866
中階會員


發表:27
回覆:147
積分:69
註冊:2002-10-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-08 09:58:11 IP:59.120.xxx.xxx 未訂閱
請參考拙著 '【發表】半透明不規則按鈕之原理與實作 '    http://delphi.ktop.com.tw/topic.php?topic_id=64346    你就知道如何作了 !    如果有幫助請給分喔 ! 發表人 -
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-08 13:10:06 IP:61.222.xxx.xxx 未訂閱
{ 取得物件背後的圖片 }
Procedure GetBackground(DestBMP:TBitmap;PS:TPoint;CltWidth,CltHeight:Integr);
var
  hHdc : HDC;
begin
  if Assigned(DestBMP) then
  begin
    DestBMP.PixelFormat := pf24bit;
    DestBMP.Height := CltHeight;
    DestBMP.Width  := CltWidth;
    try
      hHdc := GetDC(0);
      BitBlt(DestBMP.Canvas.Handle
            ,0,0,CltWidth,CltHeight
            ,hHdc,PS.X,PS.Y,SRCCOPY);
    finally
      ReleaseDC(0, hHdc);
    end;
  end;
end;
{ 半透明圖片 會改變到 ForeBMP 。 ALPHA 透明度 }
{ 沒什麼時間(懶)!請改用 ScanLine 效率會比較好! }
procedure TranslucentBMP(BKBMP,ForeBMP:TBitmap;ALPHA:WORD);
var
  BkColor,ForeColor:COLORREF;
  R,G,B:Int64;
  i,j:Integer;
begin
  with ForeBmp,ForeBmp.Canvas do
    For i:=0 to Height-1 do
      For J:=0 to Width-1 do
      begin
        BkColor   :=GetPixel(BKBMP.Canvas.Handle,j,i);
        ForeColor :=GetPixel(Handle,j,i);
        R:=(GetRValue(ForeColor)*ALPHA getRValue(BkColor)*(256-ALPHA)) shr 8;
        G:=(GetGValue(ForeColor)*ALPHA getGValue(BkColor)*(256-ALPHA)) shr 8;
        B:=(GetBValue(ForeColor)*ALPHA getBValue(BkColor)*(256-ALPHA)) shr 8;
        SetPixelV(Handle,j,i,RGB(R,G,B));
      end;
end;    {示範}
1. 將 Image1(TImage) 元件,放入圖形。為底圖。
2. 將 Image2(TImage) 元件,放入圖形。次序為最上層。    procedure TForm1.Button1Click(Sender: TObject);
var
  Background:TBitmap;
begin
  Background := TBitmap.Create;
  Try
    Image2.Visible := False;
    GetBackground(BackGround
                 ,ClientToScreen(Point(image2.Left,Image2.Top))
                 ,Image2.Width,Image2.Height);
    Image2.visible:=True;
    TranslucentBmp(Background,Image2.Picture.Bitmap,60);
  Finally
    Background.Free;
  end;
end;    在此範例是有將底圖比前圖大,考慮進去。
若有現成的兩張同大小的圖,可直接用 TranslucentBmp 轉換。
參考參考! 發表人 - wameng 於 2005/04/08 22:04:40
bearlin
一般會員


發表:3
回覆:3
積分:1
註冊:2004-07-03

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-09 02:53:05 IP:59.113.xxx.xxx 未訂閱
wameng 高手你好,我有照你的方法 試試看,可是還是跑步出來,我把procedure 都直接貼過來,也可以run 可是就是沒有效果... 不知道為什麼 會這樣?  我是用 delphi 6.0 win2000  的...用這個版本限制ㄇ? 之前也有參考過其他半透明 Form 半透明的 Form我會了 但我想弄bmp,bmp的做法有看過類似的 也都一樣按照步驟貼程式碼過來,可是也都沒有反應....     我把 整個貼過來的程式 存檔上傳 放在        <> 希望能好心幫我到底.... 為什麼會沒有反應... 最後謝謝大家....
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-09 10:53:09 IP:219.81.xxx.xxx 未訂閱
Sorry 忘了加 Image1.Repaint;
procedure TForm1.Button1Click(Sender: TObject);
var
  Background:TBitmap;
begin
  Background := TBitmap.Create;
  Try
    Image2.Visible := False;
    Image1.Repaint;        GetBackground(BackGround
                 ,ClientToScreen(Point(image2.Left,Image2.Top))
                 ,Image2.Width,Image2.Height);
    Image2.visible:=True;
    TranslucentBmp(Background,Image2.Picture.Bitmap,50);
  Finally
    Background.Free;
  end;
end;
最後,承讓!我不是高手。 寫程式是屬於年輕人的天下。 {我哭!~~ 想當年我還是二十初頭的翩翩少年郎。} 發表人 - wameng 於 2005/04/09 10:58:35
jest0024
高階會員


發表:11
回覆:310
積分:224
註冊:2002-11-24

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-04-09 12:18:16 IP:211.74.xxx.xxx 未訂閱
procedure TranslucentBmp(BKBMP,ForeBMP:TBitmap;ALPHA:WORD);
var
  i,j:Integer;
  p,q:PByteArray;
begin
  BKBmp.PixelFormat:=pf24bit;
  ForeBMP.PixelFormat:=pf24bit;
  for i:=0 to Min(BKBmp.Height,ForeBMP.Height)-1 do begin
    p:=BKBmp.ScanLine[i];
    q:=ForeBMP.ScanLine[i];
    for j:=0 to Min(BKBmp.Width,ForeBMP.Width)-1 do begin
      p[j*3]  := Trunc((q[j*3]  *ALPHA/100) (p[j*3]  *(100-ALPHA)/100));
      p[j*3 1]:= Trunc((q[j*3 1]*ALPHA/100) (p[j*3 1]*(100-ALPHA)/100));
      p[j*3 2]:= Trunc((q[j*3 2]*ALPHA/100) (p[j*3 2]*(100-ALPHA)/100));
    end;
  end;
end;
系統時間:2024-06-08 2:12:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!