chenlu_ling 发表于 2011-3-16 20:18:08

msgbox如何在主窗口居中显示?[已解决]

本帖最后由 chenlu_ling 于 2011-3-19 07:16 编辑



如何让MSGBOX在主窗口居中显示呢?而且移动主窗口后再弹出msgbox也能实现在主窗口居中显示
请问如何实现
代码如下#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$Form1 = GUICreate("Form1", 439, 297, 281, 213)
$Button1 = GUICtrlCreateButton("点我试试", 176, 128, 75, 25, 0)

GUISetState(@SW_SHOW,$Form1)

#EndRegion ### END Koda GUI section ###

While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
      Case $Button1
$xy=WinGetPos($Form1,"")
$Form2 = GUICreate("这是创建的新GUI可以实现", 225, 109,$xy+ ($xy-225)/2 , $xy+ ($xy-109)/2)
$Button2 = GUICtrlCreateButton("关闭", 64, 40, 75, 25, 0)
GUISetState(@SW_SHOW,$Form2)
MsgBox(0,"test","这是MsgBox弹出来的提示框,如何在主窗口居中呢?",$Form1)
While 1
$nMsg2 = GUIGetMsg()
Switch $nMsg2
                Case $Button2
         GUIDelete($Form2)
       ExitLoop
      EndSwitch
WEnd

      EndSwitch
WEnd

hbwazxf 发表于 2011-3-16 21:44:24

我是新手。只能帮顶。。。

星雨朝霞 发表于 2011-3-16 21:44:28

不会!!!{:face (270):}

chenlu_ling 发表于 2011-3-16 21:56:42

不会要自写_MsgBox 函数吧。。。。

aaeeff 发表于 2011-3-16 22:02:03

很有现实意义的问题,关注

lixiaolong 发表于 2011-3-16 23:28:34

本帖最后由 lixiaolong 于 2011-3-16 23:30 编辑

主窗口居中显示,这个方法不太好.#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 439, 297, 281, 213)
$Button1 = GUICtrlCreateButton("点我试试", 176, 128, 75, 25, 0)

GUISetState(@SW_SHOW, $Form1)
$Timer = DllCallbackRegister("_WinCenter", "int", "hwnd;uint;uint;dword")
$TimerDLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 0, "ptr", DllCallbackGetPtr($Timer))

#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $xy = WinGetPos($Form1, "")
                        $Form2 = GUICreate("这是创建的新GUI可以实现", 225, 109, $xy + ($xy - 225) / 2, $xy + ($xy - 109) / 2)
                        $Button2 = GUICtrlCreateButton("关闭", 64, 40, 75, 25, 0)
                        GUISetState(@SW_SHOW, $Form2)
                        MsgBox(0, "test", "这是MsgBox弹出来的提示框,如何在主窗口居中呢?", $Form1)
                        While 1
                                $nMsg2 = GUIGetMsg()
                                Switch $nMsg2
                                        Case $Button2
                                                GUIDelete($Form2)
                                                ExitLoop
                                EndSwitch
                        WEnd

        EndSwitch
WEnd

Func _WinCenter($hWnd, $uiMsg, $idEvent, $dwTime)
        If $idEvent = $TimerDLL Then
                $pos = WinGetPos($Form1)
                If Not @error Then _
                                WinMove('', "这是MsgBox弹出来的提示框,如何在主窗口居中呢?", $pos + 52, $pos + 80)
                                WinSetOnTop('', "这是MsgBox弹出来的提示框,如何在主窗口居中呢?", 1)
        EndIf
EndFunc   ;==>_WinCenter

qq82015930 发表于 2011-3-17 14:11:10

呵呵{:face (114):}移动

yhxhappy 发表于 2011-3-17 22:50:29

楼主自己画个GUI,做得像MsgBox差不多不就行了

mzjl001 发表于 2011-3-18 14:02:04

把代码修改为$Form1 = GUICreate("Form1", 439, 297)即可

love5173 发表于 2011-3-18 22:17:06

楼主,msgbox的大小是会根据你里面的内容长短而变化的。
函数要事先计算msgbox的长度跟高度,然后根据lixiaolong的函数去移动。

afan 发表于 2011-3-18 22:48:08

用钩子 _WinAPI_SetWindowsHookEx
P版以前写个一个类似的例子,需要的搜索下,稍加修改即可~

lynfr8 发表于 2011-3-19 02:18:31

http://www.autoitx.com/forum.php?mod=redirect&goto=findpost&ptid=13550&pid=184278&fromuid=1003
参考P版的一段源码,根据楼主要求稍作修改:
#include <WinAPI.au3>

$hGUI = GUICreate("msgbox在主窗口居中显示", 400, 300)
GUICtrlCreateButton("Popup a MsgBox Window", 20, 40, 150, 20)
GUISetState(@SW_SHOW, $hGUI)

$hInst = _WinAPI_GetWindowLong($hGUI, -6)
$iThreadId = _WinAPI_GetCurrentThreadId()

$hCallBack = DllCallBackRegister("_CallBack", "int", "int;hWnd;ptr")
$pCallBack = DllCallBackGetPtr($hCallBack)

$hHook = _WinAPI_SetWindowsHookEx(5, $pCallBack, $hInst, $iThreadId)

While 1
      Switch GUIGetMsg()
      Case -3
                ExitLoop
      Case 3
                                DllCall("user32.dll","int","MessageBox","hwnd",$hGUI,"str","不会在任务栏出现对话框" & @crlf, "str","测试例子","int",48)
      EndSwitch
WEnd
GUIDelete($hGUI)
_WinAPI_UnhookWindowsHookEx($hHook)
DllCallBackFree($hCallBack)


Func _CallBack($iCode, $wParam, $lParam)
      If ($iCode = 5) And ($wParam <> $hGUI) Then
                        $size = WinGetPos($hGUI)
                        WinMove($wParam, "", $size+$size/4, $size+$size/2.5, Default, Default)
      EndIf
      Return 0
EndFunc      ;==>_CallBack

chenlu_ling 发表于 2011-3-19 07:14:44

回复 12# lynfr8


    多谢 lynfr8 已达到效果。你的截图工具也挺强大的 O(∩_∩)O~

afan 发表于 2011-3-19 11:36:14

LZ还需注意10#的提示,MsgBox 宽度较宽,甚至超出主界面的处理。
还有将弹出的 MsgBox 移开后点击其它第三方窗口,再点击本程序窗口,MsgBox 又会自动返回原处的问题。

蓝叶 发表于 2011-3-21 15:54:08

新手正好学习下
页: [1] 2
查看完整版本: msgbox如何在主窗口居中显示?[已解决]