如何在启动时控制应用程序的位置 [英] How to control placement of app on startup
问题描述
我想启动一个应用程序,同时控制它的放置位置。我用
使用以下代码,但它似乎无法正常工作。 MoveWindow调用
不会移动窗口。我究竟做错了什么?是不是shell函数
应该将句柄返回窗口?或者这只是一个过程ID
和
我是否应该再拨打一个电话将其翻译成一个窗口
句柄?或者还有什么问题?
公共类Form1
继承System.Windows.Forms.Form
声明Sub MoveWindow Lib" ; USER32" (ByVal hWnd As Long,_
ByVal X As Long,ByVal Y As Long,ByVal Width
As Long,_
ByVal Height As很长,ByVal bRepaint As
布尔)
....
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim cProgram As String =" Notepad.exe"
Dim nHandle As Integer = Shell(cProgram )
MoveWindow(nHandle,10,10,500,300,True)
End Sub
结束班
>
另一个问题:.net框架内是否有任何电话支持
这个?
任何帮助表示赞赏
Peter
嗨Peter,
签出form.StartPosition属性
< br $> b $ b hth
Greetz Peter
" Peter BL拉斯穆森" < Pe的************** @ discussions.microsoft.com>写在
消息新闻:FA ********************************** @ microsof t.com ...我想启动一个应用程序,同时控制它的放置位置。我使用以下代码,但似乎无法正常工作。 MoveWindow调用
不会移动窗口。我究竟做错了什么?是不是shell函数
应该将句柄返回窗口?或者,这只是一个过程ID
和
我是否应该再打电话将其翻译成一个窗口
句柄?或者还有什么问题?
公共类Form1
继承System.Windows.Forms.Form
声明Sub MoveWindow Lib" User32" (ByVal hWnd As Long,_
ByVal X As Long,ByVal Y As Long,ByVal
宽度长,_
ByVal高度为长,ByVal bRepaint为
布尔)<私有子Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim cProgram As String =" Notepad.exe" ;
Dim nHandle As Integer = Shell(cProgram)
MoveWindow(nHandle,10,10,500,300,True)
End Sub
End Class
另一个问题:.net框架内是否有任何调用
支持这个?
任何帮助表示赞赏
Peter
为什么你不能只设置表格位置属性?
在文章< ; FA ********************************** @ microsoft.co m> ;, Peter BL Rasmussen写道:我想启动一个应用程序,同时控制它的放置位置。我使用以下代码,但似乎无法正常工作。 MoveWindow调用
不会移动窗口。我究竟做错了什么?是不是shell函数
应该将句柄返回窗口?或者这只是一个过程ID
和
嗯,文档说它是进程ID。所以,大概你可以使用
System.Diagnostics.Process类来获取窗口句柄......
嗯...实验时间:
选项严格开启
选项明确开启
进口系统
进口系统。诊断
模块模块1
声明函数MoveWindow Lib" user32" _
(ByVal hWnd As IntPtr,_
ByVal X As Integer,_
ByVal Y As Integer,_
ByVal nWidth As Integer,_
ByVal nHeight As Integer,_
ByVal bRepaint As Boolean)As Boolean
Sub Main()
Dim procId As Integer = Shell(" notepad.exe",
AppWinStyle.NormalFocus)
Dim proc As Process = Process.GetProcessById(procId)
proc.WaitForInputIdle()''让窗口有机会
初始化
''将它移动到我们想要的位置......
MoveWindow(proc.MainWindowHandle,10,10,500,300,True)
End Sub
结束模块
这似乎有效。显然,你可能想要多做一点错误
处理:)只是一个注释,这似乎也没有
WaitForInputIdle调用 - 但是我把它放进去了确保主要的
窗口已完全初始化并准备接受消息。
我当时应该再拨打一个电话将其翻译成一个窗口
处理?或者还有什么问题?
公共类Form1
继承System.Windows.Forms.Form
声明Sub MoveWindow Lib" User32" (ByVal hWnd As Long,_
ByVal X As Long,ByVal Y As Long,ByVal Width
As Long,_
ByVal Height As Long,ByVal bRepaint As
Boolean)
您的声明是错误的... VB.NET中的数据类型大小已经改变。长
是一个64位整数。它应该是Integer,因为它现在是32位。
另外,句柄应声明为System.IntPtr ...
声明函数MoveWindow Lib " USER32" _
(ByVal hWnd As IntPtr,_
ByVal X As Integer,_
ByVal Y As Integer,_
ByVal nWidth As Integer,_
ByVal nHeight As Integer,_
ByVal bRepaint As Boolean)As Boolean
...
System.EventArgs)处理Button1.Click
Dim cProgram As String =" Notepad.exe"
Dim nHandle As Integer = Shell(cProgram)
MoveWindow(nHandle,10,10,500,300,True)
End Sub
End Class
另一个问题:有没有在.net框架内调用以支持
这个?
不,不是真的。你需要为此做互操作。
-
Tom Shelton [MVP]
操作系统名称:Microsoft Windows XP Professional
操作系统版本:5.1.2600 Service Pack 2 Build 2600
系统启动时间:7天,14小时,26分钟,22秒
I want to start an app, and at the same time control where it is placed. I
use the following code, but it doesn''t seem to work. The MoveWindow call
doesn''t move the window. What am I doing wrong? Isn''t the shell function
supposed to return the handle to the window? Or is this just a process id
and
am I then supposed to make another call to translate this into a window
handle? Or what else is wrong?
Public Class Form1
Inherits System.Windows.Forms.Form
Declare Sub MoveWindow Lib "User32" (ByVal hWnd As Long, _
ByVal X As Long, ByVal Y As Long, ByVal Width
As Long, _
ByVal Height As Long, ByVal bRepaint As
Boolean)
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cProgram As String = "Notepad.exe"
Dim nHandle As Integer = Shell(cProgram)
MoveWindow(nHandle, 10, 10, 500, 300, True)
End Sub
End Class
Another question: Are there any calls inside the .net framework to support
this?
Any help appreciated
Peter
Hi Peter,
Checkout the form.StartPosition property
hth
Greetz Peter
"Peter B.L. Rasmussen" <Pe**************@discussions.microsoft.com> wrote in
message news:FA**********************************@microsof t.com...I want to start an app, and at the same time control where it is placed. I
use the following code, but it doesn''t seem to work. The MoveWindow call
doesn''t move the window. What am I doing wrong? Isn''t the shell function
supposed to return the handle to the window? Or is this just a process id
and
am I then supposed to make another call to translate this into a window
handle? Or what else is wrong?
Public Class Form1
Inherits System.Windows.Forms.Form
Declare Sub MoveWindow Lib "User32" (ByVal hWnd As Long, _
ByVal X As Long, ByVal Y As Long, ByVal Width As Long, _
ByVal Height As Long, ByVal bRepaint As
Boolean)
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cProgram As String = "Notepad.exe"
Dim nHandle As Integer = Shell(cProgram)
MoveWindow(nHandle, 10, 10, 500, 300, True)
End Sub
End Class
Another question: Are there any calls inside the .net framework to support this?
Any help appreciated
Peter
Why can''t you just set the forms Location property?
In article <FA**********************************@microsoft.co m>, Peter B.L. Rasmussen wrote:I want to start an app, and at the same time control where it is placed. I
use the following code, but it doesn''t seem to work. The MoveWindow call
doesn''t move the window. What am I doing wrong? Isn''t the shell function
supposed to return the handle to the window? Or is this just a process id
and
Well, the docs say it''s the process ID. So, presumably you can use the
System.Diagnostics.Process class to get the window handle...
Hmmm... Time to experiment:
Option Strict On
Option Explicit On
Imports System
Imports System.Diagnostics
Module Module1
Declare Function MoveWindow Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal bRepaint As Boolean) As Boolean
Sub Main()
Dim procId As Integer = Shell("notepad.exe",
AppWinStyle.NormalFocus)
Dim proc As Process = Process.GetProcessById(procId)
proc.WaitForInputIdle() '' give the window a chance to
initialize
'' move it where we want it...
MoveWindow(proc.MainWindowHandle, 10, 10, 500, 300, True)
End Sub
End Module
This seems to work. Obviously, you might want to do a little more error
handling :) Just a note, this also seems to work without the
WaitForInputIdle call - but I put it in just to make sure the main
window is fully initialized and ready to accept messages anyway.
am I then supposed to make another call to translate this into a window
handle? Or what else is wrong?
Public Class Form1
Inherits System.Windows.Forms.Form
Declare Sub MoveWindow Lib "User32" (ByVal hWnd As Long, _
ByVal X As Long, ByVal Y As Long, ByVal Width
As Long, _
ByVal Height As Long, ByVal bRepaint As
Boolean)
Your declare is wrong... Datatype sizes have changed in VB.NET. Long
is a 64-bit integer. It should be Integer, as that is now 32-bit.
Also, handles should be declared as System.IntPtr...
Declare Function MoveWindow Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal bRepaint As Boolean) As Boolean
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cProgram As String = "Notepad.exe"
Dim nHandle As Integer = Shell(cProgram)
MoveWindow(nHandle, 10, 10, 500, 300, True)
End Sub
End Class
Another question: Are there any calls inside the .net framework to support
this?
No, not really. You''ll need to do interop for this.
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 7 Days, 14 Hours, 26 Minutes, 22 Seconds
这篇关于如何在启动时控制应用程序的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!