C# 64位程序調用32位微信截圖工具PrScrn.dll方法


如果使用的是x86的運行方式,直接調用PrScrn.dll接口即可,siukwan/screenshot: 通過調用微信的截圖dll庫文件,實現微信截圖功能 (github.com)

如果主程序是AnyCPU或者x64的運行方式,據我所知有下面幾種方案:

1、封裝成COM組件的方式太麻煩,拋棄(這個門檻有點高,而且引用繁瑣,還要注冊dll)

2、做成服務,采用調用服務的方式進行(感覺也很麻煩)

3、截圖工具原C#調用項目地址:,采用WinAPI,SendMessage的方式進行,但是由於窗體在任務欄隱藏后會接收不到消息,因為拋棄此方案。

 

本人最終解決方案:

基於上面提到的項目(https://github.com/siukwan/screenshot),設置為x86的方式運行,去除多余的代碼,如注冊快捷鍵等,運行后程序自動調用截圖工具,完了自己關閉,廢話少說,上代碼:

 

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);

    try
    {
        int i = DLL.PrScrn();
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCaptrue.txt");
        File.WriteAllText(path, i.ToString());
    }
    catch { }
    finally
    {
        Application.Exit();
    }
}

窗體屬性設置如下:

private void InitializeComponent()
{
    this.SuspendLayout();
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(0, 0);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "Form1";
    this.ShowIcon = false;
    this.ShowInTaskbar = false;
    this.Text = "截圖工具";
    this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
    this.ResumeLayout(false);
}

編譯生成即可,自己放好路徑。

 

64位程序調用方法如下:

try
{
    Process proc = new Process();
    proc.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ScreenCaptrue\ScreenCaptrue.exe");
    proc.Start();
    proc.WaitForExit();
    string resultFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ScreenCaptrue\ScreenCaptrue.txt");
    if (File.Exists(resultFilePath))
    {
        string printResult = File.ReadAllText(resultFilePath);
        if (printResult == "1")
        {
            pictureBox1.Image?.Dispose();
            Image image = Clipboard.GetImage();
            pictureBox1.Image = image;
        }
        lblResult.Text = printResult == "0" ? "取消截圖" : (printResult == "1" ? "截圖確認完成" : "截圖工具直接保存");
    }
    else
    {
        lblResult.Text = "讀取操作結果失敗";
        Image image = Clipboard.GetImage();
        if (image != null)
        {
            pictureBox1.Image?.Dispose();
            pictureBox1.Image = image;
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM