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;