这段代码是如何工作的? [难的]

发布于 2024-10-05 19:51:44 字数 1668 浏览 0 评论 0原文

关联: http://www.codeproject.com/KB/dotnet/twaindotnet.aspx

我正在尝试为 TWAIN 的开源 .NET 实现创建一个包装类,但我无法理解它实际上是如何获取图像的。

我已经下载了源代码,在 GUI 中有一个名为 Acquire 的按钮。当我单击此按钮转到它的事件处理程序时,我发现这段代码,我假设它获取了图像:

private void menuItemScan_Click(object sender, System.EventArgs e)
{
    if (!msgfilter)
    {
        this.Enabled = false;
        msgfilter = true;
        Application.AddMessageFilter(this);
    }
    tw.Acquire();
}

如果我按照 Acquire() 方法查看它的内容,我会看到以下内容:

public void Acquire()
{
    TwRC rc;
    CloseSrc();
    if (appid.Id == IntPtr.Zero)
    {
        Init(hwnd);
        if (appid.Id == IntPtr.Zero)
            return;
    }
    rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds);
    if (rc != TwRC.Success)
        return;

    TwCapability cap = new TwCapability(TwCap.XferCount, 1);
    rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }

    TwUserInterface guif = new TwUserInterface();
    guif.ShowUI = 1;
    guif.ModalUI = 1;
    guif.ParentHand = hwnd;
    rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }
}

我不明白的是一个方法如何使用“void”返回类型实际上可以有一个 return 语句。另外,它在哪里获取和返回图像?

有人可以帮忙吗?

我正在尝试创建一个有用的包装器并将其开源,因为就目前情况而言,在 C# 中没有简单的拖放解决方案来扫描图像。

感谢您的帮助!

编辑:感谢您有关提前退货的帮助。直到!现在我很好奇应用程序如何获取图像以显示在表单上。

有什么指导吗?

LINK:
http://www.codeproject.com/KB/dotnet/twaindotnet.aspx

I'm trying to create a wrapper class for this open source .NET implementation of TWAIN and I'm having trouble understand how it actually gets the image.

I've downloaded the source code and in the GUI there is a button called Acquire. When I click this button to go to it's event handler I find this code which I assume gets the image:

private void menuItemScan_Click(object sender, System.EventArgs e)
{
    if (!msgfilter)
    {
        this.Enabled = false;
        msgfilter = true;
        Application.AddMessageFilter(this);
    }
    tw.Acquire();
}

If I follow the Acquire() method to see it's contents, I see this:

public void Acquire()
{
    TwRC rc;
    CloseSrc();
    if (appid.Id == IntPtr.Zero)
    {
        Init(hwnd);
        if (appid.Id == IntPtr.Zero)
            return;
    }
    rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds);
    if (rc != TwRC.Success)
        return;

    TwCapability cap = new TwCapability(TwCap.XferCount, 1);
    rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }

    TwUserInterface guif = new TwUserInterface();
    guif.ShowUI = 1;
    guif.ModalUI = 1;
    guif.ParentHand = hwnd;
    rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }
}

What I don't understand is how a method with a 'void' return type can actually have a return statement. Also, where is it acquiring and returning an image?

Can anyone help out?

I'm trying to create a useful wrapper and open source it, because as it stands there is no easy drag and drop solution for scanning images in C#.

Thanks for the help!

Edit: Thanks for the help regarding early returns. TIL! Now I'm curious about how the application gets the images to display on the form.

Any guidance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

椒妓 2024-10-12 19:51:44

“Void”意味着它不返回任何内容,而不是不返回。因此 return 语句只是终止函数并返回给调用者

对于您的其他问题,还有一些其他相关的堆栈溢出问题

DSCap 行正在查看是否有多个图像。捕获是作为调用 DSuserif 的一部分发生的

"Void" means it returns nothing, not that it doesn't return. So the return statement just terminates the function and returns to the caller

For your other question, there are a few other relevant stack overflow questions

The DSCap line is seeing if there are multiple images. The capture happens as part of the call to DSuserif

诠释孤独 2024-10-12 19:51:44

事实上,您可以通过调用 Application.AddMessageFilter(this) 方法在表单上设置消息过滤器。因此,您必须监听扫描仪事件,当您收到 TwainCommand.TransferReady 事件时,您将调用 TransferPictures() 来获取图像集合。

Infact, you set a message filter on your form by calling Application.AddMessageFilter(this) method. So, you have to listen to scanner events and when you get a TwainCommand.TransferReady event, you will call TransferPictures() to get the image collection.

此岸叶落 2024-10-12 19:51:44

该方法只是返回 void 以避免执行其他代码段。这是完全合法的。我想说,该方法不是获取图像,它只是准备获取图像的硬件和 UI。

The method simply returns void to avoid other code segments being executed. That is completely legal. The method is not acquiring an image it only prepares the hardware and UI that is acquiring the image, i'd say.

萌吟 2024-10-12 19:51:44

return; 导致控制流退出函数。

去图书馆看了一眼。看起来 Acquire() 只是导致驱动程序执行获取,并且调用 TransferPictures() 来检索图片(返回一个 ArrayList code>,所以是的,它正在返回一些东西)。

return; causes the control flow to exit the function.

Had a look at the library. It seems that Acquire() just causes the driver to perform an acquire, and TransferPictures() is called to retrieve the pictures (that one returns an ArrayList, so yes it is returning something).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文