首页 \ 问答 \ 如何获取Window实例的hWnd?(How to get the hWnd of Window instance?)

如何获取Window实例的hWnd?(How to get the hWnd of Window instance?)

我的WPF应用程序有多个窗口,我需要能够获取每个Window实例的hWnd,以便我可以在Win32 API调用中使用它们。

我想做的例子:

Window myCurrentWindow = Window.GetWindow(this);
IntPtr myhWnd = myCurrentWindow.hWnd; // Except this property doesn't exist.

最好的方法是什么?


My WPF application has more than one window, I need to be able to get the hWnd of each Window instance so that I can use them in Win32 API calls.

Example of what I would like to do:

Window myCurrentWindow = Window.GetWindow(this);
IntPtr myhWnd = myCurrentWindow.hWnd; // Except this property doesn't exist.

What's the best way to do this?


原文:https://stackoverflow.com/questions/10675305
更新时间:2023-01-26 15:01

最满意答案

以下是一些Python代码,可以为您的示例生成类似的输出:

def f(low, high):
    ranges = collections.deque([(low, high)])
    while ranges:
        low, high = ranges.popleft()
        mid = (low + high) // 2
        yield mid
        if low < mid:
            ranges.append((low, mid))
        if mid + 1 < high:
            ranges.append((mid + 1, high))

例:

>>> list(f(0, 20))
[10, 5, 15, 2, 8, 13, 18, 1, 4, 7, 9, 12, 14, 17, 19, 0, 3, 6, 11, 16]

与Python中的约定一样, low, high范围不包括端点,因此结果包含从0到19的数字。

该代码使用FIFO来存储仍需要处理的子范围。 FIFO在整个范围内初始化。 在每次迭代中,下一个范围都会弹出,并且中点会放弃。 然后,如果当前范围的下部和上部子范围非空,则将其附加到FIFO。

编辑 :这是C99中完全不同的实现:

#include <stdio.h>

int main()
{
    const unsigned n = 20;
    for (unsigned i = 1; n >> (i - 1); ++i) {
        unsigned last = n;    // guaranteed to be different from all x values
        unsigned count = 1;
        for (unsigned j = 1; j < (1 << i); ++j) {
            const unsigned x = (n * j) >> i;
            if (last == x) {
                ++count;
            } else {
                if (count == 1 && !(j & 1)) {
                    printf("%u\n", last);
                }
                count = 1;
                last = x;
            }
        }
        if (count == 1)
            printf("%u\n", last);
    }
    return 0;
}

这避免了使用一些技巧来确定先前迭代中是否已经发生整数的FIFO的必要性。

你也可以很容易地在C中实现原来的解决方案。既然你知道FIFO的最大尺寸(我猜它是类似于(n + 1)/ 2的,但是你需要仔细检查这个),你可以使用环形缓冲区保持排队的范围。

编辑2 :这是C99的又一个解决方案。 它被优化为仅执行一半循环迭代,并且仅使用位操作和添加,而不用乘法或除法。 它也更简洁,并且在结果中不包含0 ,所以您可以按照您的初始设想在开始时使用它。

for (int i = 1; n >> (i - 1); ++i) {
    const int m = 1 << i;
    for (int x = n; x < (n << i); x += n << 1) {
        const int k = x & (m - 1);
        if (m - n <= k && k < n)
            printf("%u\n", x >> i);
    }
}

(这是我一开始就想写的代码,但花了我一些时间来围绕它。)


Here is some Python code producing similar output to your example:

def f(low, high):
    ranges = collections.deque([(low, high)])
    while ranges:
        low, high = ranges.popleft()
        mid = (low + high) // 2
        yield mid
        if low < mid:
            ranges.append((low, mid))
        if mid + 1 < high:
            ranges.append((mid + 1, high))

Example:

>>> list(f(0, 20))
[10, 5, 15, 2, 8, 13, 18, 1, 4, 7, 9, 12, 14, 17, 19, 0, 3, 6, 11, 16]

The low, high range excludes the endpoint, as is convention in Python, so the result contains the numbers from 0 to 19.

The code uses a FIFO to store the subranges that still need to be processed. The FIFO is initialised with the full range. In each iteration, the next range is popped and the mid-point yielded. Then, the lower and upper subrange of the current range is appended to the FIFO if they are non-empty.

Edit: Here is a completely different implementation in C99:

#include <stdio.h>

int main()
{
    const unsigned n = 20;
    for (unsigned i = 1; n >> (i - 1); ++i) {
        unsigned last = n;    // guaranteed to be different from all x values
        unsigned count = 1;
        for (unsigned j = 1; j < (1 << i); ++j) {
            const unsigned x = (n * j) >> i;
            if (last == x) {
                ++count;
            } else {
                if (count == 1 && !(j & 1)) {
                    printf("%u\n", last);
                }
                count = 1;
                last = x;
            }
        }
        if (count == 1)
            printf("%u\n", last);
    }
    return 0;
}

This avoids the necessity of a FIFO by using some tricks to determine if an integer has already occured in an earlier iteration.

You could also easily implement the original solution in C. Since you know the maximum size of the FIFO (I guess it's something like (n+1)/2, but you would need to double check this), you can use a ring buffer to hold the queued ranges.

Edit 2: Here is yet another solution in C99. It is optimised to do only half the loop iterations and to use only bit oprations and additions, no multiplication or divisions. It's also more succinct, and it does not include 0 in the results, so you can have this go at the beginning as you initially intended.

for (int i = 1; n >> (i - 1); ++i) {
    const int m = 1 << i;
    for (int x = n; x < (n << i); x += n << 1) {
        const int k = x & (m - 1);
        if (m - n <= k && k < n)
            printf("%u\n", x >> i);
    }
}

(This is the code I intended to write right from the beginning, but it took me some time to wrap my head around it.)

相关问答

更多
  • 以下是一些Python代码,可以为您的示例生成类似的输出: def f(low, high): ranges = collections.deque([(low, high)]) while ranges: low, high = ranges.popleft() mid = (low + high) // 2 yield mid if low < mid: ranges.append((low, mid ...
  • 1)-9 - 7 -9 - 7 = -9 + -7 9(二进制)= 01001 -9(2的补码)= 10111 7(二进制)= 00111 -7(2的补码)= 11001 10111 + 11001 = 110000 这不适合5位。 去除溢出我们得到10000,这是-16(二进制)。 2)6 - 10 6 - 10 = 6 + -10 6(二进制)= 00110 10(二进制)= 01010 -10(2的补码)= 10110 00110 + 10110 = 11100 这适合5位并且是-4(二 ...
  • 你已经提到了spawn但似乎认为你不能使用它。 可能在这里显示我的无知,但看起来它应该只是你正在寻找的东西:通过spawn启动openssl,然后写入child.stdin并从child.stdout读取。 非常类似于完全未经测试的代码: var util = require('util'), spawn = require('child_process').spawn; function sslencrypt(buffer_to_encrypt, callback) { var ssl ...
  • 您可以使用GetCurrentProcessId()来获取当前进程的进程ID。 然后,您可以使用ultoa将该数字转换为使用基数2 (以及sizeof(DWORD) * 8 + 1 )的缓冲区的字符串,然后您可以使用ofstream或fwrite将其写入文件。 例: DWORD id = GetCurrentProcessId(); char buf[sizeof(DWORD) * 8 + 1]; ultoa(id, buf, 2); ofstream f("file.txt"); f << id; ...
  • // save to file // ======================================= InputStream is = new BufferedInputStream(item.openStream()); BufferedOutputStream output = null; try { output = new BufferedOutputStream(new FileOutputStream("temp.txt", false)); int data ...
  • 有些事情是这个例子没有正确解释的。 首先,假设它是一个你正在搜索名字的字典 。 假设你正在寻找“约翰”。 第(n / 2)页为您提供以字母L开头的名称(比如第X页)。 因此,您忽略从此页面开始的所有内容,因为您知道此页面和其他页面无法满足您的搜索要求。 现在,您的搜索集将缩减为从第1页到第X页的搜索。继续执行您每次选择页面时忽略搜索集的n / 2项的步骤。 所以,从我给出的解释: 1.)如果n是偶数/奇数,只需选择你得到的n / 2。 您要么在下一次迭代中将第1页考虑到(n / 2)-1(或)页面(n / ...
  • high=arr[i].second; for(j=0;j=arr[j].first.second) high=std::max(high, dp[j]+arr[i].second); } ...
  • 在WWDC上,有人提到你可以构建一个二进制文件,它既支持iPhone和iPod touch的原始型号的ARM v6指令集,也支持使用iPhone 3G S和新iPod的新ARM v7指令集。触摸模型。 我相信你可以构建一个胖二进制文件,支持这两种体系结构并将其上传到iTunes Connect。 xcode-users邮件列表中的此消息似乎支持这一点。 At WWDC, it was mentioned that you can build a binary which supports both the ...
  • 使用二进制搜索查找产品名称的代码是正确的,但似乎数据按SKU排序。 二进制搜索算法仅在数据排序时才有效。 在应用二进制搜索之前,您可以修改程序以按产品名称对数据进行排序,但在这种情况下,由于您使用的冒号排序是N ^ 2搜索时间,因此最好只使用线性搜索产品名称。 You code for looking up the product name using Binary Search is correct, but it appears that the data is sorted by SKU. The B ...
  • 您可以将二进制文件移动到Developer Rejected 即使您没有真正更改任何内容,也要确保更新版本号 You can move the binary to Developer Rejected Make sure that you update the version number as well, even if you haven't really changed anything

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)