调用user32.dll显示其他窗口


本文摘自PHP中文网,作者PHP中文网,侵删。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<span style="color: #008080"> 1</span> <span style="color: #808080">///</span><span style="color: #008000"> 该函数设置由不同线程产生的窗口的显示状态

</span><span style="color: #008080"> 2</span>         <span style="color: #808080">///</span> <span style="color: #808080"></summary></span>

<span style="color: #008080"> 3</span>         <span style="color: #808080">///</span> <span style="color: #808080"><param name="hWnd"></span><span style="color: #008000">窗口句柄</span><span style="color: #808080"></param></span>

<span style="color: #008080"> 4</span>         <span style="color: #808080">///</span> <span style="color: #808080"><param name="cmdShow"></span><span style="color: #008000">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分</span><span style="color: #808080"></param></span>

<span style="color: #008080"> 5</span>         <span style="color: #808080">///</span> <span style="color: #808080"><returns></span><span style="color: #008000">如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零</span><span style="color: #808080"></returns></span>

<span style="color: #008080"> 6</span>         [DllImport(<span style="color: #800000">"</span><span style="color: #800000">User32.dll</span><span style="color: #800000">"</span><span style="color: #000000">)]

</span><span style="color: #008080"> 7</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> ShowWindowAsync(IntPtr hWnd, <span style="color: #0000ff">int</span><span style="color: #000000"> cmdShow);

</span><span style="color: #008080"> 8</span>         <span style="color: #808080">///</span> <span style="color: #808080"><summary></span>

<span style="color: #008080"> 9</span>         <span style="color: #808080">///</span><span style="color: #008000">  该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。

</span><span style="color: #008080">10</span>         <span style="color: #808080">///</span><span style="color: #008000">  系统给创建前台窗口的线程分配的权限稍高于其他线程。

</span><span style="color: #008080">11</span>         <span style="color: #808080">///</span> <span style="color: #808080"></summary></span>

<span style="color: #008080">12</span>         <span style="color: #808080">///</span> <span style="color: #808080"><param name="hWnd"></span><span style="color: #008000">将被激活并被调入前台的窗口句柄</span><span style="color: #808080"></param></span>

<span style="color: #008080">13</span>         <span style="color: #808080">///</span> <span style="color: #808080"><returns></span><span style="color: #008000">如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零</span><span style="color: #808080"></returns></span>

<span style="color: #008080">14</span>         [DllImport(<span style="color: #800000">"</span><span style="color: #800000">User32.dll</span><span style="color: #800000">"</span><span style="color: #000000">)]

</span><span style="color: #008080">15</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span><span style="color: #000000"> SetForegroundWindow(IntPtr hWnd);

</span><span style="color: #008080">16</span>

<span style="color: #008080">17</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span><span style="color: #000000"> BringToFront(IntPtr hWnd)

</span><span style="color: #008080">18</span> <span style="color: #000000">        {

</span><span style="color: #008080">19</span>             <span style="color: #008000">//</span><span style="color: #008000">Process currentProcess = Process.GetCurrentProcess();

</span><span style="color: #008080">20</span>             <span style="color: #008000">//</span><span style="color: #008000">Process process = Process.get(id);

</span><span style="color: #008080">21</span>             <span style="color: #008000">//</span><span style="color: #008000">if (process != null)

</span><span style="color: #008080">22</span>             <span style="color: #008000">//</span><span style="color: #008000">{

</span><span style="color: #008080">23</span>             <span style="color: #008000">//</span><span style="color: #008000">    ShowWindowAsync(process.MainWindowHandle, 1);</span><span style="color: #008000">//</span><span style="color: #008000">显示

</span><span style="color: #008080">24</span>             <span style="color: #008000">//</span><span style="color: #008000">    SetForegroundWindow(process.MainWindowHandle);</span><span style="color: #008000">//</span><span style="color: #008000">当到最前端

</span><span style="color: #008080">25</span>             <span style="color: #008000">//</span><span style="color: #008000">}</span>

<span style="color: #008080">26</span>             Process[] Processes = Process.GetProcesses();<span style="color: #008000">//</span><span style="color: #008000"> GetProcessesByName(currentProcess.ProcessName);</span>

<span style="color: #008080">27</span>             <span style="color: #0000ff">foreach</span> (Process process <span style="color: #0000ff">in</span><span style="color: #000000"> Processes)

</span><span style="color: #008080">28</span> <span style="color: #000000">            {

</span><span style="color: #008080">29</span>                 <span style="color: #008000">//</span><span style="color: #008000">process.Start();

</span><span style="color: #008080">30</span>                 <span style="color: #008000">//</span><span style="color: #008000">if (process.Id != currentProcess.Id)

</span><span style="color: #008080">31</span>                 <span style="color: #008000">//</span><span style="color: #008000">{

</span><span style="color: #008080">32</span>                 <span style="color: #008000">//</span><span style="color: #008000">                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\")

</span><span style="color: #008080">33</span>                 <span style="color: #008000">//</span><span style="color: #008000"><span style="white-space:pre">            </span> == currentProcess.MainModule.FileName)

</span><span style="color: #008080">34</span>                 <span style="color: #008000">//</span><span style="color: #008000">                    {

</span><span style="color: #008080">35</span>                 <span style="color: #008000">//</span><span style="color: #008000">                        return process;

</span><span style="color: #008080">36</span>                 <span style="color: #008000">//</span><span style="color: #008000">                    }</span>

<span style="color: #008080">37</span>                 <span style="color: #0000ff">if</span> (process.MainWindowHandle.ToString() == <span style="color: #800000">"</span><span style="color: #800000">0</span><span style="color: #800000">"</span><span style="color: #000000">)

</span><span style="color: #008080">38</span> <span style="color: #000000">                {

</span><span style="color: #008080">39</span>                     <span style="color: #0000ff">continue</span><span style="color: #000000">;

</span><span style="color: #008080">40</span> <span style="color: #000000">                }

</span><span style="color: #008080">41</span>                 <span style="color: #0000ff">if</span> (process.MainWindowHandle ==<span style="color: #000000"> hWnd)

</span><span style="color: #008080">42</span> <span style="color: #000000">                {

</span><span style="color: #008080">43</span>                     ShowWindowAsync(process.MainWindowHandle, <span style="color: #800080">1</span>);<span style="color: #008000">//</span><span style="color: #008000">显示</span>

<span style="color: #008080">44</span>                     SetForegroundWindow(process.MainWindowHandle);<span style="color: #008000">//</span><span style="color: #008000">当到最前端</span>

<span style="color: #008080">45</span> <span style="color: #000000">                }

</span><span style="color: #008080">46</span>                 <span style="color: #008000">//</span><span style="color: #008000">}</span>

<span style="color: #008080">47</span> <span style="color: #000000">            }

</span><span style="color: #008080">48</span>         }

 

以上就是调用user32.dll显示其他窗口的详细内容!


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...