- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Why does closing a console that was started with AllocConsole cause my whole application to exit? Can I change this behavior?
(4 个回答)
7年前关闭。
我有一个标准的 Windows 窗体应用程序,我需要一个解决方案来在 Windows 窗体应用程序设置中打开控制台。我想出了这个解决方案:
Public Class Win32
<DllImport("kernel32.dll")>
Public Shared Function AllocConsole() As Boolean
End Function
<DllImport("kernel32.dll")>
Public Shared Function FreeConsole() As Boolean
End Function
' ...
Win32.AllocConsole()
Console.WriteLine("Test")
Win32.FreeConsole()
以编程方式关闭控制台。)
最佳答案
我想你可能会觉得这很有趣,试试看;对我很有用!另一个注意事项:用户无法点击关闭按钮,因为它已被禁用,退出的唯一方法是您如何设置它...
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports Microsoft.Win32
Public Class Form1
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function AllocConsole() As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function FreeConsole() As Boolean
End Function
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, _
ByVal uPosition As Integer, ByVal uFlags As Integer) As Boolean
Private Declare Function GetForegroundWindow Lib "user32" () As Integer
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Integer, _
ByVal bRevert As Boolean) As Integer
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, _
ByVal uCmd As Integer) As Integer
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer
Private Sub btnStartConsole_Click(sender As Object, e As EventArgs) Handles btnStartConsole.Click
StartConsole()
End Sub
Private Sub StartConsole()
AllocConsole()
Console.Title = "TEST"
' Obtain a handle to the console application window by passing the title of your application.
Dim hWnd As Integer = ObtainWindowHandle("TEST") 'Can change this, but must match the name you give it!'
' Obtain a handle to the console application system menu.
Dim hMenu As Integer = GetSystemMenu(hWnd, False)
' Delete the Close menu item from the console application system menu.
' This will automatically disable the Close button on the console application title bar.
DeleteMenu(hMenu, 6, 1024)
Console.WriteLine("We have a console! Enter something!")
' Read value.
Dim s As String = Console.ReadLine()
' Write the value.
Console.WriteLine("You typed " + s)
Console.WriteLine("Press any key! ...")
Console.ReadLine()
FreeConsole()
End Sub
Private Function ObtainWindowHandle(ByVal lpstrCaption As String) As Integer
' To store the handle to a window.
Dim hWnd As Integer
' Maximum number of characters in the GetWindowText method.
Dim nMaxCount As Integer
' Actual number of characters copied in the GetWindowText method.
Dim nCopiedLength As Integer
' To store the text of the title bar of the window.
Dim lpString As String
nMaxCount = 255
' Obtain a handle to the first window.
hWnd = GetForegroundWindow
' Loop through the various windows until you encounter the console application window, _
' or there are no more windows.
While hWnd <> 0
' Fill lpString with spaces.
lpString = Space(nMaxCount)
' Get the text of the title bar of the window in lpString.
nCopiedLength = GetWindowText(hWnd, lpString, nMaxCount)
' Verify that lpString is neither empty, nor NULL.
If Len(Trim(lpString)) <> 0 And Asc(Trim(lpString)) <> 0 Then
' Verify that the title of the retrieved window is the same as the title of the console application window.
If CType(InStr(Microsoft.VisualBasic.Left(lpString, nCopiedLength), lpstrCaption), Boolean) Then
' Return hWnd to the Main method.
Return hWnd
End If
End If
' Get the next window.
hWnd = GetWindow(hWnd, 2)
End While
' If no corresponding windows are found, return 0.
Return 0
End Function
End Class
关于vb.net - 关闭控制台时防止 Windows 窗体应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21504253/
对于一个科学实验,我写了一个turtle.py ,它会打开一个 800x480 的窗口并绘制一个缓慢增长的黑点。 turtle.py以 C:\Users\kaza>python C:\Users\ka
我开发了一个 swing 应用程序,但每次运行应用程序时都会打开一个新窗口。我希望如果一个窗口已经打开,则其他窗口不允许打开。 最佳答案 Here是一个 Java 单一应用实例的例子: A singl
有没有办法检测主进程中 Electron 的结构? process.platform 似乎也在 x64 机器上返回 win32,我没有在文档中找到任何获取架构的选项。 最佳答案 你试过 process
public short[] HanningWindow(short[] signal_in ,int pos ,int size) { for (int i= pos; i < pos+si
我有一个具有这些属性的 Electron 窗口: mainWindow = new BrowserWindow({ width: 800, height: 600, title: "Aqu
我有一个 Ubuntu 工作站,我正在尝试引导一个 Windows 节点。 Windows 节点在端口 2222 上打开了 ssh。我一直在关注 http://docs.opscode.com/plu
我是一名优秀的程序员,十分优秀!