C# CommandBarButton图标获得黑色背景
我正在尝试制作outlook 2007 addIn。将图标设置为CommandBarButton时,图标的背景变为黑色 我使用的代码是:C# CommandBarButton图标获得黑色背景,c#,.net,outlook-addin,outlook-2007,C#,.net,Outlook Addin,Outlook 2007,我正在尝试制作outlook 2007 addIn。将图标设置为CommandBarButton时,图标的背景变为黑色 我使用的代码是: private CommandBar _menuBar; private void AddScheduleMeetingButton() { var scheduleButton = (CommandBarButton)_menuBar .Controls.Add(MsoControlType.msoControlB
private CommandBar _menuBar;
private void AddScheduleMeetingButton()
{
var scheduleButton = (CommandBarButton)_menuBar
.Controls.Add(MsoControlType.msoControlButton, missing, missing, 1, true);
scheduleButton.BeginGroup = true;
scheduleButton.Caption = ScheduleButtonCaption;
scheduleButton.Picture = GetImage(Resources.schedule_meeting16);
scheduleButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
scheduleButton.Click += UnirowScheduleButtonClick;
scheduleButton.Visible = true;
}
使用Microsoft提供的AxHost方法的GetImage()函数:
private IPictureDisp GetImage(Icon newIcon)
{
IPictureDisp tempImage = null;
try
{
var newImageList = new ImageList();
newImageList.Images.Add(newIcon);
tempImage = MyHost.GetIPictureDispFromPicture(newImageList.Images[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return tempImage;
}
我犯了什么错误?Office中有一个特殊的解决方案,可以通过
CommandBarButton
支持自定义图标透明度。有关如何利用Win32 API为Office CommandBar按钮创建透明图片的信息,请参见
复制透明的Office工具栏图片
C:\MyTestPic.bmp
单击事件中:
选项显式
公共类型BitMapInfo标头“40字节”
一分为二
宽度与长度相同
双倍高度
作为整数的双平面
双比特计数为整数
双压缩等长
比斯泽姆年龄一样长
倍径渗透计
双渗透计等长
长时间使用
这一点同样重要
端型
公共类型位图
bmType尽可能长
宽度与长度相同
高度与长度相同
BMW的长度为个字节
bm平面为整数
bmBitsPixel为整数
bmBits尽可能长
端型
' ===================================================================
'GDI/绘图函数(用于构建遮罩)
' ===================================================================
私有声明函数GetDC Lib“user32”(ByVal hwnd作为Long)作为Long
私有声明函数ReleaseDC Lib“user32”_
(ByVal hwnd尽可能长,ByVal hdc尽可能长)尽可能长
私有声明函数DeleteDC Lib“gdi32”(ByVal hdc作为Long)作为Long
私有声明函数CreateCompatibleDC Lib“gdi32”_
(ByVal hdc作为Long)作为Long
专用声明函数CreateCompatibleBitmap Lib“gdi32”_
(ByVal hdc等长,ByVal nWidth等长,ByVal nHeight等长)等长
私有声明函数CreateBitmap库“gdi32”_
(ByVal nWidth As Long,ByVal nHeight As Long,ByVal nPlans As Long_
ByVal nBitCount的长度与lpBits的长度相同
私有声明函数SelectObject库“gdi32”_
(ByVal hdc尽可能长,ByVal hObject尽可能长)尽可能长
私有声明函数DeleteObject库“gdi32”_
(ByVal hObject As Long)一样长
私有声明函数GetBkColor Lib“gdi32”_
(ByVal hdc作为Long)作为Long
私有声明函数SetBkColor Lib“gdi32”_
(ByVal hdc尽可能长,ByVal crColor尽可能长)尽可能长
私有声明函数GetTextColor库“gdi32”_
(ByVal hdc作为Long)作为Long
私有声明函数SetTextColor库“gdi32”_
(ByVal hdc尽可能长,ByVal crColor尽可能长)尽可能长
私有声明函数BitBlt Lib“gdi32”_
(ByVal hDestDC为长,ByVal x为长,ByVal y为长_
ByVal nWidth As Long,ByVal nHeight As Long,ByVal hSrcDC As Long_
ByVal xSrc为长,ByVal ySrc为长,ByVal dwRop为长)为长
私有声明函数CreateHalftonePalette库“gdi32”_
(ByVal hdc作为Long)作为Long
专用声明函数SelectPalette Lib“gdi32”_
(ByVal hdc尽可能长,ByVal HPALETE尽可能长_
ByVal bForceBackground(尽可能长)尽可能长
私有声明函数RealizePalette库“gdi32”_
(ByVal hdc作为Long)作为Long
专用声明函数OleTranslateColor Lib“oleaut32.dll”_
(ByVal lOleColor尽可能长,ByVal lHPalette尽可能长_
如:如:如:如:如:如:如
私有声明函数GetDIBits Lib“gdi32”_
(ByVal aHDC长,ByVal hBitmap长,ByVal nStartScan长_
ByVal nnums扫描长度为,lpBits扫描长度为,lpBI扫描长度为_
ByVal wUsage As Long)As Long
私有声明函数GetObjectAPI库“gdi32”别名“GetObjectA”_
(ByVal hObject尽可能长,ByVal nCount尽可能长,lpObject尽可能长)尽可能长
' ===================================================================
'剪贴板API
' ===================================================================
私有声明函数OpenClipboard Lib“user32”_
(ByVal hwnd As Long)一样长
私有声明函数CloseClipboard Lib“user32”(长度为
专用声明函数注册表ClipboardFormat库“user32”_
别名“RegisterClipboardFormatA”(ByVal lpString作为字符串)的长度相同
私有声明函数GetClipboardData库“user32”_
(ByVal wFormat As Long)一样长
私有声明函数SetClipboardData库“user32”_
(ByVal wFormat As Long,ByVal hMem As Long)一样长
私有声明函数EmptyClipboard Lib“user32”(长度为
专用常数CF_DIB=8
' ===================================================================
'内存API(用于剪贴板传输)
' ===================================================================
私有声明子CopyMemory Lib“kernel32”别名“rtlmovemory”_
(pDest为任意,pSource为任意,ByVal cbLength为任意长度)
专用声明函数GlobalAlloc Lib“kernel32”_
(ByVal wFlags为长,ByVal dwBytes为长)为长
私有声明函数GlobalFree Lib“kernel32”_
(ByVal-hMem-As-Long)一样长
私有声明函数GlobalLock Lib“kernel32”_
(ByVal-hMem-As-Long)一样长
专用声明函数GlobalSize Lib“kernel32”_
(ByVal-hMem-As-Long)一样长
专用声明函数GlobalUnlock Lib“kernel32”_
(ByVal-hMem-As-Long)一样长
私有常量GMEM\U DDESHARE=&H2000
私有常量GMEM_MOVEABLE=&H2
' ===================================================================
'CopyBitmapAsButtonFace
'
'这是要调用以创建掩码的公共函数
Private Sub Command1_Click()
Dim oPic As StdPicture
Dim oWord As Object
Dim oCommandBar As Object
Dim oButton As Object
' Load the picture (.bmp file) to use for the button image.
Set oPic = LoadPicture("C:\MyTestPic.bmp")
' Start Microsoft Word for Automation and create a new
' toolbar and button to test the PasteFace method.
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oCommandBar = oWord.CommandBars.Add("Test Bar")
oCommandBar.Visible = True
Set oButton = oCommandBar.Controls.Add(1)
With oButton
.Caption = "Test Button"
.Style = 1
' Here we create a mask based on the image and put both
' the image and the mask on the clipboard. Any color areas with
' magenta will be transparent.
CopyBitmapAsButtonFace oPic, &HFF00FF
' PasteFace will now add the image with transparency.
.PasteFace
.Visible = True
End With
MsgBox "You have a new button with a transparent picture.", _
vbMsgBoxSetForeground
Set oButton = Nothing
If MsgBox("Do you want to delete the toolbar?", _
vbYesNo Or vbQuestion) = vbYes Then
oCommandBar.Delete
End If
Set oCommandBar = Nothing
Set oWord = Nothing
End Sub
public class IconConverter : System.Windows.Forms.AxHost
{
private IconConverter()
: base(string.Empty)
{
}
public static stdole.IPictureDisp GetIPictureDispFromImage(System.Drawing.Image image)
{
return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
}
}
private stdole.IPictureDisp GetPictureMask(Bitmap img) {
if (img.IsNotNull()) {
int rgb;
Color c;
for (int y = 0; y < img.Height; y++)
for (int x = 0; x < img.Width; x++) {
c = img.GetPixel(x, y);
Color m = Color.Empty;
if (c.B == 0 && c.R == 0 && c.G == 0) {
m = Color.White;
}
else {
m = Color.Black;
}
img.SetPixel(x, y, Color.FromArgb(c.A, m.R, m.G, m.B));
}
return IconConverter.GetIPictureDispFromImage(img);
}
return null;
}
commandBarButton.Picture = IconConverter.GetIPictureDispFromImage(orginalImage);
commandBarButton.Mask = GetPictureMask(orginalImage);