您的位置:程序门 -> vb ->



如何控制在vb中打开其它程序时该程序的显示位置?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何控制在vb中打开其它程序时该程序的显示位置?[已结贴,结贴人:ahopera]
发表于:2007-09-06 14:55:46 楼主
在vb中要用开其它程序,但是用shell命令是不能控制打开程序的位置和大小的,请问vb中有什么函数可以达到控制程序窗体出现的位置和窗体大小么?能不能拿notepad.exe来做个示范呢?
我用以下代码不能实现:
option   explicit

const   synchronize   =   &h100000

const   infinite   =   &hffffffff

private   declare   function   openprocess   lib   "kernel32 "   (byval   dwdesiredaccess   as   long,   byval   binherithandle   as   long,   byval   dwprocessid   as   long)   as   long

private   declare   function   closehandle   lib   "kernel32 "   (byval   hobject   as   long)   as   long

private   declare   function   waitforsingleobject   lib   "kernel32 "   (byval   hhandle   as   long,   byval   dwmilliseconds   as   long)   as   long

private   declare   function   setwindowpos   lib   "user32 "   (byval   hwnd   as   long,   byval   hwndinsertafter   as   long,   byval   x   as   long,   byval   y   as   long,   byval   cx   as   long,   byval   cy   as   long,   byval   wflags   as   long)   as   long
private   declare   function   movewindow   lib   "user32 "   (byval   hwnd   as   long,   byval   x   as   long,   byval   y   as   long,   byval   nwidth   as   long,   byval   nheight   as   long,   byval   brepaint   as   long)   as   long

sub   apptest()

dim   pid   as   long,   phnd   as   long   '   分别声明process   id   及   process   handle   变数

pid   =   shell( "notepad.exe ",   vbnormalfocus)   'shell   传回   process   id
phnd   =   openprocess(synchronize,   0,   pid)   '取得   process   handle


setwindowpos   phnd,   0,   1024,   120,   130,   0,   &h40


'movewindow   phnd,   500,   500,   120,   130,   false

end   sub

发表于:2007-09-06 16:42:541楼 得分:100
option   explicit

const   synchronize   =   &h100000

const   infinite   =   &hffffffff

private   declare   function   openprocess   lib   "kernel32 "   (byval   dwdesiredaccess   as   long,   byval   binherithandle   as   long,   byval   dwprocessid   as   long)   as   long

private   declare   function   closehandle   lib   "kernel32 "   (byval   hobject   as   long)   as   long

private   declare   function   waitforsingleobject   lib   "kernel32 "   (byval   hhandle   as   long,   byval   dwmilliseconds   as   long)   as   long

private   declare   function   setwindowpos   lib   "user32 "   (byval   hwnd   as   long,   byval   hwndinsertafter   as   long,   byval   x   as   long,   byval   y   as   long,   byval   cx   as   long,   byval   cy   as   long,   byval   wflags   as   long)   as   long
private   declare   function   movewindow   lib   "user32 "   (byval   hwnd   as   long,   byval   x   as   long,   byval   y   as   long,   byval   nwidth   as   long,   byval   nheight   as   long,   byval   brepaint   as   long)   as   long
private   declare   function   getwindowthreadprocessid   lib   "user32 "   (byval   hwnd   as   long,   lpdwprocessid   as   long)   as   long
    private   declare   function   findwindowex   lib   "user32 "   alias   "findwindowexa "   (byval   hwnd1   as   long,   byval   hwnd2   as   long,   byval   lpsz1   as   string,   byval   lpsz2   as   string)   as   long
    private   declare   function   getwindowtext   lib   "user32 "   alias   "getwindowtexta "   (byval   hwnd   as   long,   byval   lpstring   as   string,   byval   cch   as   long)   as   long
    private   declare   function   getparent   lib   "user32 "   (byval   hwnd   as   long)   as   long
    private   declare   function   showwindow   lib   "user32 "   (byval   hwnd   as   long,   byval   ncmdshow   as   long)   as   long
    dim   i           as   long
                    dim   mnum           as   long
                    private   const   sw_showmaximized   =   3
       
       
    private   function   mgetwindow(byval   mpid   as   long)   as   long
                    dim   lpdpid()           as   long
                    dim   bwnd           as   long
                    dim   bwndback           as   long
                    dim   lpid           as   long
    dim   mstr1           as   string   *   25,   mstr2                       as   string   *   25,   m1                       as   long,   m2               as   long,   mb               as   boolean
    m1   =   len(mstr1)
    m2   =   len(mstr2)
                    bwndback   =   0
                    mnum   =   0
                    redim   lpdpid(0)
                    '历遍所有窗体,如果对应进程pid与已知pid相同,则存入数组
                    do
                                    bwnd   =   findwindowex(0,   bwndback,   vbnullstring,   vbnullstring)
                                    if   bwnd   =   0   then   exit   do
                                    getwindowthreadprocessid   bwnd,   lpid
                                    if   lpid   =   mpid   then                                                                       '如果pid相同
                                                    if   getparent(bwnd)   =   0   then                       '列举所有为父窗的窗体,如果需要的是mid应该把这里的条件修改为> 0
                                                                    mb   =   false
                                                                    for   i   =   0   to   ubound(lpdpid)
                                                                                    getwindowtext   bwnd,   mstr1,   m1
                                                                                    getwindowtext   lpdpid(i),   mstr2,   m2
                                                                                    'debug.print       mstr1       &       "------------------ "       &       mstr2
                                                                                    if   mstr1   =   mstr2   then
                                                                                                    mb   =   true
                                                                                                    lpdpid(i)   =   0
                                                                                    end   if
                                                                    next
                                                                    if   mb   =   false   then
       
                                                                                    lpdpid(mnum)   =   bwnd
'                                                                                     list2.additem   lpdpid(mnum)
                                                                                    mnum   =   mnum   +   1
                                                                                    redim   preserve   lpdpid(mnum)
                                                                    end   if
                                                    end   if
                                    end   if
                    bwndback   =   bwnd
'                     list1.additem   bwnd
                    doevents
                    loop
                    mgetwindow   =   lpdpid(0)
    end   function


private   sub   command1_click()
dim   pid   as   long,   phnd   as   long   '   分别声明process   id   及   process   handle   变数

pid   =   shell( "notepad.exe ",   vbnormalfocus)   'shell   传回   process   id
me.print   pid
'phnd   =   openprocess(synchronize,   0,   pid)   '取得   process   handle
phnd   =   mgetwindow(pid)
me.print   phnd
'setwindowpos   phnd,   0,   1024,   120,   130,   0,   &h40


movewindow   phnd,   500,   500,   120,   130,   true

end   sub
发表于:2007-09-06 19:05:562楼 得分:0
api:setwindowpos
发表于:2007-09-07 10:01:353楼 得分:0
我是用findwindow实现的,但是你的要更精确些


快速检索

最新资讯
热门点击