【问题标题】:Take screenshot of WinForm after it has loadedWinForm加载后截图
【发布时间】:2012-08-21 09:16:02
【问题描述】:

我有一个屏幕截图应用程序,用户可以在其中传递命令行参数,并根据这些参数确定表单的行为。

我正在尝试在表单加载后截取屏幕截图,但是在InitializeComponent(); 之后我可以执行此操作。就这样-

if (counts > 0)
        {
            generateScreenshotButton_Click(null, null);

            button2_Click(null, null);
        }

我的问题是这些函数在表单加载之前触发。因此屏幕截图是空白的。我该如何解决这个问题?

【问题讨论】:

标签: c# arguments screenshot


【解决方案1】:

在您的 Form.cs 文件中添加 Load 事件。

InitializeComponentForm Load 之间存在差异。请参阅What does InitializeComponent() do, and how does it work in WPF? 它适用于 WPF,但在 Windows 窗体中相同。

试试这个代码:

public Form1()
        {
            InitializeComponent(); //this is the InitializeComponent method. this This method locates a URI to the XAML for the Window/UserControl that is loading, and passes it to the System.Windows.Application.LoadComponent() static method. 
            this.Load += new EventHandler(Form1_Load); //this create Load Form event
        }

        void Form1_Load(object sender, EventArgs e) //after your form is completely loaded, your program will run the code from here...
        {
            //your code goes here
        }

希望对你有所帮助。

【讨论】:

  • 这仍然会在表单完全加载之前触发按钮单击?
  • 不,这就是主意。如果您将方法放在Form1_Load 中,则在表单完全加载后将触发单击。
  • 我不明白这个,目前我的按钮点击是在初始化组件部分,这也是我收集值的地方。你建议我在 Form1_Load 中放入什么????
  • 查看编辑。希望你能理解得更好。您是否尝试将代码放入Form1_Load
【解决方案2】:

您是否尝试过使用 MainForm Loaded 事件?
在那个事件处理程序中做你的事情吗? :)

或者也许使用Shown 事件?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2010-12-27
    • 2021-11-07
    • 2013-03-27
    • 1970-01-01
    • 2019-04-13
    • 2018-04-14
    相关资源
    最近更新 更多