程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 修正XPMenu的兩個Bug

修正XPMenu的兩個Bug

編輯:Delphi
XPMenu是大名鼎鼎的Dephi第三方界面控件,最近在使用中發現了幾個Bug,並對其中的兩個進行修正。
  
  1、首先,是繪制菜單和工具欄圖標時,會將圖像白色部分作為透明色,導致圖像缺損非常難看,如下圖所示:
  
  查看XPMenu的源代碼,圖標是通過TXPMenu.DrawIcon函數繪制的,函數內計算了圖標顯示的位置、調用GrayBitmap、DimBitmap、DrawBitmapShadow等函數對圖像進行了處理,並將圖像的Transparent設為true,再查看GrayBitmap、DimBitmap、DrawBitmapShadow函數並沒有發現會導致透明色計算錯誤的代碼。再往回找,終於在TXPMenu.MenueDrawItem和TXPMenu.ToolBarDrawButton裡發現了問題,先來看看TXPMenu.MenueDrawItem:
  

   procedure TXPMenu.MenueDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
    Selected: Boolean);
  beign
  .....
    //-------
    if HasBitmap then
      begin
        B.Width := FMenuItem.Bitmap.Width;
        B.Height := FMenuItem.Bitmap.Height;
    // +jt
       //B.Canvas.Brush.Color := FTransparentColor; // ACanvas.Brush.Color;
       B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <[email protected]>
       B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
       FMenuItem.Bitmap.Transparent := true;
       FMenuItem.Bitmap.TransparentMode := tmAuto;
        B.Canvas.Draw(0,0,FMenuItem.Bitmap);
    // +jt
      end;

  

  
    if HasImgLstBitmap then
    begin
    {$IFDEF VER5U}
      if FMenuItem.Parent.SubMenuImages <> nil then
      begin
        ImgListHandle := FMenuItem.Parent.SubMenuImages.Handle;
        ImgIndex := FMenuItem.ImageIndex;

        B.Width := FMenuItem.Parent.SubMenuImages.Width;
        B.Height := FMenuItem.Parent.SubMenuImages.Height;
       // B.Canvas.Brush.Color := FTransparentColor; // ACanvas.Brush.Color; // +jt
        B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <[email protected]>
  
      B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
        ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_Transparent);
  
      end
      else

    {$ENDIF}
      if FMenuItem.Parent.GetParentMenu.Images <> nil then
      begin
        ImgListHandle := FMenuItem.Parent.GetParentMenu.Images.Handle;
        ImgIndex := FMenuItem.ImageIndex;

        B.Width := FMenuItem.Parent.GetParentMenu.Images.Width;
        B.Height := FMenuItem.Parent.GetParentMenu.Images.Height;
        //B.Canvas.Brush.Color := FTransparentColor; //ACanvas.Pixels[2,2]; // +jt
        B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <[email protected]>
        B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
        ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_Transparent);
  

      end;

    end;
  ......
    if
(B <> nil) and (B.Width > 0) then  // X
  
    DrawIcon(FMenuItem, ACanvas, B, IconRect,
        Selected or DrawTopMenuBorder, False, FMenuItem.Enabled, FMenuItem.Checked,
        FTopMenu, FMenu.IsRightToLeft);
  ......
  end;

  很明顯,除了XPMenu的作者之外,+jt、Asher和X均對代碼進行過修改,問題就出在+jk的修改的這段代碼裡,它先用B(TBitmap).Canvas.Pixels[0, B.Height - 1]處的顏色填充了整個B,然後再把要顯示的圖標畫到B裡去,後面會調用DrawIcon把圖標顯示出來,可惜+jt計算錯誤,當時的B(TBitmap).Canvas.Pixels[0, B.Height - 1]值正是$FFFFFF(白色),以ILD_Transparent參數調用ImageList_DrawEx時會把原圖標底色(比如上圖的粉紅色)去掉,那麼DrawIcon繪制的圖標底色就是白色的,最後圖標所有白色部分被挖空了。+jt真是好心做壞事了。
  原因找到了,現在直接把+jt那部分代碼去掉即可。原程序變為:
  

   procedure TXPMenu.MenueDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
    Selected: Boolean);
  beign
  .....
    //-------
    if HasBitmap then
      begin
        B.Width := FMenuItem.Bitmap.Width;
        B.Height := FMenuItem.Bitmap.Height;
  {Modify: Conch 2004-12-16 畫出的圖標透明顏色有錯
    // +jt
       //B.Canvas.Brush.Color := FTransparentColor; // ACanvas.Brush.Color;
       B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <
[email protected]>
       B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
       FMenuItem.Bitmap.Transparent := true;
       FMenuItem.Bitmap.TransparentMode := tmAuto;
  }
  
      B.Canvas.Draw(0,0,FMenuItem.Bitmap);
    // +jt
      end;

  

  
    if HasImgLstBitmap then
    begin
    {$IFDEF VER5U}
      if FMenuItem.Parent.SubMenuImages <> nil then
      begin
        ImgListHandle := FMenuItem.Parent.SubMenuImages.Handle;
        ImgIndex := FMenuItem.ImageIndex;

        B.Width := FMenuItem.Parent.SubMenuImages.Width;
        B.Height := FMenuItem.Parent.SubMenuImages.Height;
  {Modify: Conch 2004-12-16 畫出的圖標透明顏色有錯
       // B.Canvas.Brush.Color := FTransparentColor; // ACanvas.Brush.Color; // +jt
        B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <
[email protected]>
        B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
        ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_Transparent);
  }
  
      ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_NORMAL);
  //Conch
  
  
      end
      else

    {$ENDIF}
      if FMenuItem.Parent.GetParentMenu.Images <> nil then
      begin
        ImgListHandle := FMenuItem.Parent.GetParentMenu.Images.Handle;
        ImgIndex := FMenuItem.ImageIndex;

        B.Width := FMenuItem.Parent.GetParentMenu.Images.Width;
        B.Height := FMenuItem.Parent.GetParentMenu.Images.Height;
  {Modify: Conch 2004-12-16 畫出的圖標透明顏色有錯
        //B.Canvas.Brush.Color := FTransparentColor; //ACanvas.Pixels[2,2]; // +jt
        B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <
[email protected]>
        B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
        ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_Transparent);
  }
  
      ImageList_DrawEx(ImgListHandle, ImgIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_NORMAL);
  //Conch
  
  

      end;

    end;
  ......
    if
(B <> nil) and (B.Width > 0) then  // X
  
    DrawIcon(FMenuItem, ACanvas, B, IconRect,
        Selected or DrawTopMenuBorder, False, FMenuItem.Enabled, FMenuItem.Checked,
        FTopMenu, FMenu.IsRightToLeft);
  ......
  end;

  TXPMenu.ToolBarDrawButton的原因也是一樣。
  

   procedure TXPMenu.ToolBarDrawButton(Sender: TToolBar;
    Button: TToolButton; State: TCustomDrawState; var DefaultDraw: Boolean);
  begin
        if CanDraw then
        begin {CanDraw}
  
{Modify: Conch 2004-12-16  畫出的圖標透明顏色有錯
         // B.Canvas.Brush.Color := TransparentColor; // ACanvas.Brush.Color; // +jt
          B.Canvas.Brush.Color := B.Canvas.Pixels[0, B.Height - 1];//"Todd Asher" <
[email protected]>
          B.Canvas.FillRect(Rect(0, 0, B.Width, B.Height));
          ImageList_DrawEx(ImglstHand, Button.ImageIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_Transparent);
  }

          ImageList_DrawEx(ImglstHand, Button.ImageIndex,
          B.Canvas.Handle, 0, 0, 0, 0, clNone, clNone, ILD_NORMAL);
  //Conch
  ......
  end;

  
  2、第二個問題是菜單的陰影太呆板,是固定的一層灰色塊(當然是指WinXP以前的系統),如圖:
  
  
  菜單陰影是由TXPMenu.DrawWindowBorder函數繪制的。這裡可以看到+jt進行過修改,不過+jt的注釋方式有點特別,看不出那些在作者的原代碼,那些是+jt改過的,不過可以肯定+jt下了不少苦功。這個函數把Windows來的那個3D邊框去掉,變成平面的OfficeXP風格菜單。若果要改為像WinXP菜單的那種通過與背景像素混合的得到的陰影效果,必須對源碼進行大改,但使用第三方控件目的是為了方便省事,如果那樣做的話就太有違初衷了。因此我這裡用了一種折衷的辦法,把灰色面積減少,這樣看起來就順眼多了。最終效果如下圖:
  
  修改後的TXPMenu.DrawWindowBorder函數:
   procedure TXPMenu.DrawWindowBorder(hWnd: HWND; IsRightToLeft: boolean);
  var
    WRect: TRect;
    dCanvas: TCanvas;
    wDC: HDC; // +jt

  

   regiontype: integer; // +jt
  
 r1,r2,wr,region: HRGN; // +jt
  
 rgnr: TRect; // +jt
  
begin

    if (hWnd <= 0) or (FSettingWindowRng) then
   begin
    exit;
   end;
  // +jt
   wDC := GetWindowDC(hWnd); //GetDesktopWindow
  
 if wDC=0 then exit;
  // +jt
    FSettingWindowRng :=true; // +jt
   dCanvas := TCanvas.Create;
   try
     dCanvas.Handle := wDC; // +jt
      GetWindowRect(hWnd, WRect);
    // +jt
     WRect.Right := WRect.Right-WRect.Left;
     WRect.Bottom := WRect.Bottom-WRect.Top;
     WRect.Top:=0;
     WRect.Left:=0;
      if GetWindowLong(hWnd,GWL_WNDPROC)<>integer(@MenuWindowProc) then
     begin
       SetWindowLong(hWnd,GWL_USERDATA,GetWindowLong(hWnd,GWL_WNDPROC));
       SetWindowLong(hWnd,GWL_WNDPROC,integer(@MenuWindowProc));
     end;
  {Modify: Conch 2004-11-04 畫出的陰影太難看了
      if not IsWXP then
     begin
        wr:= CreateRectRgn(0,0,0,0);
       regiontype := GetWindowRgn(hWnd, wr);
       GetRgnBox(wr,rgnr);
       DeleteObject(wr);
        if (regionType = ERROR) or (abs(rgnr.Right-WRect.Right)>5) or (abs(rgnr.Bottom-WRect.Bottom)>5) then
       begin
         region:= CreateRectRgn(0,0,0,0);
         r1:=CreateRectRgn(WRect.Left,WRect.Top,WRect.Right-2,WRect.Bottom-2);
         r2:=CreateRectRgn(WRect.Left+2,WRect.Top+2,WRect.Right,WRect.Bottom);
         CombineRgn(region,r1,r2,RGN_OR);
         DeleteObject(r1);
         DeleteObject(r2);
          SetWindowRgn(hWnd,region,true);
        end;
   // +jt
        Dec(WRect.Right, 2);
       Dec(WRect.Bottom, 2);
     end; // +jt
  }

      dCanvas.Brush.Style := bsClear;
      dCanvas.Pen.Color := FMenuBorderColor;
     dCanvas.Rectangle(WRect.Left, WRect.Top, WRect.Right, WRect.Bottom);
      if IsRightToLeft then
     begin
        dCanvas.Pen.Color := FFIconBackColor;
       dCanvas.MoveTo(WRect.Right - 3, WRect.Top + 2);
       dCanvas.LineTo(WRect.Right - 2, WRect.Bottom - 1);
      end
     else
     begin
  
     dCanvas.Pen.Color := FFIconBackColor;
       dCanvas.Rectangle(WRect.Left + 1, WRect.Top + 2, WRect.Left + 3, WRect.Bottom - 1);
     end;
  // +jt
         StretchBlt(dCanvas.Handle,WRect.Left + 1,WRect.Top + 1,WRect.Right - WRect.Left-1,2,
                    dCanvas.Handle,WRect.Left + 1,WRect.Top + 3,WRect.Right - WRect.Left-1,1,SRCCOPY);
          if IsWXP then
         begin
           StretchBlt(dCanvas.Handle,WRect.Left + 1,WRect.Bottom - 3,WRect.Right - WRect.Left-1,2,
                      dCanvas.Handle,WRect.Left + 1,WRect.Top + 3,WRect.Right - WRect.Left-1,1, SRCCOPY);
           dCanvas.Pen.Color := FFColor;
           dCanvas.Rectangle(WRect.Right - 3, WRect.Top+1, WRect.Right - 1, WRect.Bottom-1);
         end;
  // +jt
  {Modify: Conch 2004-11-04 畫出的陰影太難看了
      Inc(WRect.Right, 2);
     Inc(WRect.Bottom, 2);
      if not IsWXP then // +jt
     begin // +jt
       dCanvas.Pen.Color := FMenuShadowColor;
       dCanvas.Rectangle(WRect.Left + 2, WRect.Bottom, WRect.Right, WRect.Bottom - 2);
       dCanvas.Rectangle(WRect.Right - 2, WRect.Bottom, WRect.Right, WRect.Top + 2);
     end; // +jt
  }

   finally
     ReleaseDC(hWnd, wDC); // +jt
     dCanvas.Free;
   FSettingWindowRng :=false;
   end;

  end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved