如何捕获从VB.Net 2010中的控制台应用程序启动的表单中的击键? [英] How do I capture keystrokes in a form launched from a console app in VB.Net 2010?

查看:78
本文介绍了如何捕获从VB.Net 2010中的控制台应用程序启动的表单中的击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新我在VB6中编写的旧版ScreenSaver到VB.net 2010我最初使用的Windows窗体应用程序工作正常,直到预览屏幕。使用以下代码将应用程序加载到预览屏幕(或其他应用程序,如记事本):





 公共  Sub  SetForm( ByRef  frm 作为表单, ByRef  arg  As  字符串
Dim 样式作为 整数
Dim preview_hwnd As 整数 = 整数 .Parse( CType (arg,_
字符串))
Dim r 作为 R. ECT

' 获取预览窗口的大小。
GetClientRect( preview_hwnd,r)

使用 frm
.WindowState = FormWindowState.Normal
.FormBorderStyle = FormBorderStyle.None
.Width = r.right
.Height = r.bottom
结束 使用

' 将WS_CHILD样式添加到表单中。
style = GetWindowLong(frm.Handle.ToInt32,GWL_STYLE)
style = style WS_CHILD
SetWindowLong(frm.Handle.ToInt32,GWL_STYLE,样式)

' 将表格重新显示到预览窗口。
SetParent(frm.Handle.ToInt32,preview_hwnd)

' 将表单的GWL_PARENT值设置为预览窗口。
SetWindowLong(frm.Handle.ToInt32,GWL_HWNDPARENT ,_
preview_hwnd)

' 将表单放在预览窗口中。
SetWindowPos(frm.Handle.ToInt32, 0 ,r.left, 0 ,r.right,_
r.bottom,SWP_NOACTIVATE SWP_NOZORDER SWP_SHOWWINDOW)
结束 Sub





冻结父窗口,启动表单没有输入。



切换到控制台应用程序并使用



  Dim  scrsvr  As   frmScr 
scrsvr.Preview = True
SetForm(scrsvr,args( 1 ))
Application.Run(scrsvr)





修复了大部分问题,但表单/屏幕保护程序仍然在预览屏幕中或在不运行setform的情况下作为全屏幕屏幕保护程序运行时,不接受键盘输入。有趣的是,配置屏幕在从控制台应用程序启动时运行正常。



在VB.net 2010中,唯一的选项似乎是从表单运行应用程序(表单应用程序)或从控制台(控制台应用程序)。在VB6中,我们可以运行一个表单应用程序,但是从Sub Main开始。



任何帮助都表示赞赏。

解决方案

请参阅我对该问题的评论。这个问题对我来说很难看,但当然你可以通过多种不同的方式搞砸键盘事件处理;问题是你没有向我们展示相关的代码。



通常,屏幕保护程序没有特定的问题,并且与键盘事件处理有关。这一切都很重要。

例如,你可以看一下这个项目:

https://msdn.microsoft.com/en-us/library/ms686421%28v=vs.85%29.aspx [<一个href =https://msdn.microsoft.com/en-us/library/ms686421%28v=vs.85%29.aspxtarget =_ blanktitle =New Window> ^ ]。



你可以找到更多。



-SA

I'm updating an old ScreenSaver that I wrote in VB6 to VB.net 2010 I initially used a windows forms application that worked fine until the preview screen. Loading the application into the preview screen (or other application like notepad) using the code:


Public Sub SetForm(ByRef frm As Form, ByRef arg As String)
     Dim style As Integer
     Dim preview_hwnd As Integer = Integer.Parse(CType(arg, _
         String))
     Dim r As New RECT

     'Get the preview window's size.
     GetClientRect(preview_hwnd, r)

     With frm
         .WindowState = FormWindowState.Normal
         .FormBorderStyle = FormBorderStyle.None
         .Width = r.right
         .Height = r.bottom
     End With

     ' Add the WS_CHILD style to the form.
     style = GetWindowLong(frm.Handle.ToInt32, GWL_STYLE)
     style = style Or WS_CHILD
     SetWindowLong(frm.Handle.ToInt32, GWL_STYLE, style)

     ' Reparent the form into the preview window.
     SetParent(frm.Handle.ToInt32, preview_hwnd)

     'Set the form's GWL_PARENT value to the preview window.
     SetWindowLong(frm.Handle.ToInt32, GWL_HWNDPARENT, _
         preview_hwnd)

     ' Position the form in the preview window.
     SetWindowPos(frm.Handle.ToInt32, 0, r.left, 0, r.right, _
         r.bottom, SWP_NOACTIVATE Or SWP_NOZORDER Or SWP_SHOWWINDOW)
 End Sub



freezes the parent window and there is no input to the launched form.

Switching to a console app and using

Dim scrsvr As New frmScr
 scrsvr.Preview = True
 SetForm(scrsvr, args(1))
 Application.Run(scrsvr)



fixed most of the problem, but the form/screensaver still doesn't accept keyboard input, either when in the preview screen or running as a full screen screensaver without running "setform". Interestingly, the configuration screen runs fine when launched from the console app.

In VB.net 2010, the only options seem to be running an app either from a form (forms application) or from the console(Console app). In VB6 we could run a forms app yet start from a Sub Main.

Any help is appreciated.

解决方案

Please see my comment to the question. The problem looks artificial to me, but of course you could screw up the keyboard event handling in one of many different ways; the problem is that you did not show us relevant piece of code.

Generally, there is no a problem specific to the screen savers and related to keyboard event handling. This is all that matters.
For example, you can look at this project:
https://msdn.microsoft.com/en-us/library/ms686421%28v=vs.85%29.aspx[^].

You can find a lot more.

—SA


这篇关于如何捕获从VB.Net 2010中的控制台应用程序启动的表单中的击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆