gpt4 book ai didi

c# - VSTO 中的透明控件

转载 作者:行者123 更新时间:2023-11-30 18:41:11 25 4
gpt4 key购买 nike

我正在尝试使用 vsto 将一些 Windows 窗体控件添加到工作表。不过我希望它们是透明的(以便 Excel 中的实际内容可见)。

我的 winforms 用户控件构造函数如下所示:

    public Tag()
{
InitializeComponent();
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
}

我正在像这样添加控件:

void Application_WorkbookOpen(Excel.Workbook Wb)
{
var nativeSheet = Wb.ActiveSheet as Excel.Worksheet;

if (nativeSheet != null)
{
var tag = new Tag();
var vstoSheet = nativeSheet.GetVstoObject();
var range = nativeSheet.Range["A1", missing];
vstoSheet.Controls.AddControl(tag, range, Guid.NewGuid().ToString());
}
}

如果有一些内容单元格 A1,它将被控件覆盖(该单元格将显示为纯白色)。

有人对此有任何想法吗?

最佳答案

正如在 MSDN 中提到的:

When a Windows form control are hosted on Office document, the control is not directly embedded in the document. An ActiveX control called the Windows Forms control host is added to document surface first and the control host acts as a host for each Windows Form control added too the document.

你可以自己看看:

var btn = new ImageButton();
btn.Name = "link1";
btn.Text = controlText;
btn.Click += new EventHandler(btn_Click);
vstoWorksheet.Controls.AddControl(pic, nativeWorksheet.Range[address], controlText);


public class ImageButton : Control, IButtonControl
{
public ImageButton()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.Transparent;

}

protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
// don't call the base class
//base.OnPaintBackground(pevent);
}

protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x20;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
// rest of class here...
}

关于c# - VSTO 中的透明控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7153438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com