简体   繁体   English

如何在插入图像时防止Excel互操作窃取焦点

[英]How to keep Excel interop from stealing focus while inserting images

I'm using the Excel COM interop to insert images (specifically EPS) into a spreadsheet. 我正在使用Excel COM互操作程序将图像(特别是EPS)插入电子表格中。 The images are inserted fine, but Excel ignores all the visible/background settings and steals the focus the display a dialog box saying something like "importing image". 可以很好地插入图像,但是Excel会忽略所有可见/背景设置,并在显示对话框中显示“正在导入图像”之类的内容时失去焦点。 The dialog box only stays a fraction of a section, but it makes the screen flicker, and worse, when I'm inserting many images at once, it can monopolize the system for several seconds (including stealing keystrokes from the foreground process). 该对话框仅占一个部分的一小部分,但会使屏幕闪烁,更糟糕的是,当我一次插入许多图像时,它会垄断系统几秒钟(包括从前台进程中窃取按键)。

I'm setting the background options as follows: 我将如下设置背景选项:

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = false;
xlApp.ScreenUpdating = false;
xlApp.DisplayAlerts = false;
Excel.Worksheet worksheet;
//....
worksheet.Pictures(Type.Missing).Insert(filename,Type.Missing); //steals focus

How can I get Excel to stay in the background here like it belongs? 如何使Excel像它所属的一样保留在后台?

I suspect this is caused by a component that Microsoft licensed that is badly behaved. 我怀疑这是由Microsoft许可的行为不当的组件引起的。

The only way to deal with situations like this is by intercepting the appropriate low-level Windows message and blocking it using a Win32 hook 处理这种情况的唯一方法是拦截相应的低级Windows消息并使用Win32挂钩阻止它

The easiest way to do this, and, believe me, it's not pretty, is to use the CBT hook. 做到这一点最简单的方法,并且相信我,这并不漂亮,是使用CBT挂钩。 CBT stands for "Computer Based Training." CBT代表“基于计算机的培训”。 It is an ancient and nearly obsolete technology intended to make it possible to create training apps which watch what you're doing and respond accordingly. 它是一项古老且几乎过时的技术,旨在使人们可以创建监视应用程序的应用程序,这些应用程序可以监视您的工作并做出相应的响应。 The only thing it's good for any more is hooking and preventing window activation in code you don't have access to. 唯一有益的是钩住并阻止您无法访问的代码中的窗口激活。 You could intercept the HCBT_ACTIVATE code and prevent a window from activating. 您可以拦截HCBT_ACTIVATE代码,并阻止激活窗口。

This would probably need to be done in C or C++. 这可能需要在C或C ++中完成。

Contrary to common sense, setting xlApp.ScreenUpdating = true; 与常识相反,设置xlApp.ScreenUpdating = true; prevents focus steal! 防止焦点窃取! ( source ) 来源

I'm not sure I have a good answer to your question (I would have left a comment, but apparently I don't have enough reputation yet), but in case it's helpful, I often also disable events 我不确定您的问题是否能得到很好的回答(我本来会发表评论,但显然我还没有足够的声誉),但是如果有帮助,我经常会禁用事件

xlApp.EnableEvents = False

Obviously you have to be sensitive to this setting when you're relying on Excel events in your code, but just in case there are some unwanted events firing while you're inserting, this might help. 显然,当您依赖代码中的Excel事件时,您必须对此设置敏感,但是以防万一在插入时触发了一些不需要的事件,这可能会有所帮助。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM