如何获取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
最满意答案
以下是一些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.)
相关问答
更多-
二进制选择过程(Binary selection process)[2022-04-09]
以下是一些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