ShowWindow显示和隐藏

NewMan7758 2009-05-22 06:17:17
为什么不能给分??
还是先发吧。。
简单来说就是,通过2个按钮控制一个文本文件的显示和隐藏。
问题: ShowWindow(Hwd, SW_HIDE) ShowWindow(Hwd, SW_NORMAL) 点击后,文本文件窗体都是隐藏了。不显示。

上代码。

Public Class Form1
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As UInteger

Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As UInteger, ByRef lpdwProcessId As UInteger) As UInteger
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As UInteger, _
ByVal bInheritHandle As UInteger, ByVal dwProcessId As UInteger) As UInteger

Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Public Const SW_HIDE = 0
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10


Const PROCESS_ALL_ACCESS = &H1F0FFF
Public Hwd As UInteger
Public pid As UInteger
Public hProcess As UInteger


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Hwd = FindWindow((vbNullString), "1.txt - 记事本")
Label1.Text = Hwd
If Hwd = 0 Then
Label2.Text = "程序未运行"
Else
Label2.Text = "程序已运行"
GetWindowThreadProcessId(Hwd, pid)
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
If hProcess = 0 Then
MsgBox("不能读取进程", , "提示")
Else
MsgBox("OK", , "提示!")

'隐藏窗体
'================================================
ShowWindow(Hwd, SW_HIDE) '隐藏
'===============================================

End If

End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Hwd = FindWindow((vbNullString), "1.txt - 记事本")
ShowWindow(Hwd, SW_NORMAL) '显示
End Sub

End Class
...全文
349 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlongwuhui 2009-05-24
  • 打赏
  • 举报
回复
参考:
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Int32
ShowWindow(Hwd, SW_SHOW)
cnzdgs 2009-05-24
  • 打赏
  • 举报
回复
VB.NET中Long是64位整数,要用UInt32。
另外,显示应该用SW_SHOW。
bw555 2009-05-23
  • 打赏
  • 举报
回复
api参数类型你写错了
 Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As UInteger

Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As UInteger, ByRef lpdwProcessId As UInteger) As UInteger
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As UInteger, _
ByVal bInheritHandle As UInteger, ByVal dwProcessId As UInteger) As UInteger
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
'Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Boolean

Public Const SW_HIDE = 0
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10


Const PROCESS_ALL_ACCESS = &H1F0FFF
Public Hwd As UInteger
Public pid As UInteger
Public hProcess As UInteger

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Hwd = FindWindow((vbNullString), "1.txt - 记事本")
Label1.Text = Hwd
If Hwd = 0 Then
Label2.Text = "程序未运行"
Else
Label2.Text = "程序已运行"
GetWindowThreadProcessId(Hwd, pid)
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
If hProcess = 0 Then
'MsgBox("不能读取进程", , "提示")
Else
'MsgBox("OK", , "提示!")

'隐藏窗体
'================================================
ShowWindow(Hwd, 0) '隐藏
'===============================================

End If

End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Hwd = FindWindow((vbNullString), "1.txt - 记事本")
ShowWindow(Hwd, SW_SHOW) '显示
End Sub

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧