'在弹出模态对话框的同时设置他的位置
'本例子掩饰与父窗体左上角对齐
'根据bcb_fans(四大名捕之追杀令)的bcb代码改写为vb的
'仅供参考
'附bcb_fans源代码
'bcb_fans (四大名捕之追杀令)
'当messagebox对话框显示的时候,主窗口将失去焦点,知道了这一点,问题就很简单了。下面是我的c++builder代码。
'
'//替换主窗口的窗口过程(我不知道vc怎样办到这一点,反正原理就是这样)
'
'void __fastcall tform1::wndproc(messages::tmessage &message)
'{
' tform::wndproc(message);
'
' //失去焦点之前
' if(message.msg == wm_ncactivate)
' {
' }
' //失去焦点之后
' else if(message.msg == wm_activate)
' {
' if(loword(message.wparam) == wa_inactive)
' {
' hwnd hwnd;
' char classbuf[64];
'
' hwnd = (hwnd)message.lparam;
' getclassname(hwnd,classbuf,sizeof(classbuf));
'
' //首先判断是否是messagebox对话框,
' if(ansistring(classbuf) == "#32770")
' {
' //再判断这个窗口是否是本身程序的窗口(因为别的程序弹出窗口时,你的主窗口也同样失去焦点)
' dword dwprocessid;
'
' getwindowthreadprocessid(hwnd,&dwprocessid);
'
' if(dwprocessid == getcurrentprocessid())
' {
' //移动窗口的位置
' movewindow(.......);//或者调用 setwindowpos(...)
'
' //替换左边的标题栏图标(如果有的话)
' setclasslong(hwnd,gcl_hicon,(long)image1->picture->icon->handle);
'
' //重新设置按钮的文本(比如把确定改为“ok 007”)
' hwnd htemp;
'
' htemp = getdlgitem(hwnd,1);
' setwindowtext(htemp,"new text for button");
'
' //.........其他操作
' }
' }
' }
' }
'}
'窗体
option explicit
private sub form_load()
on error resume next
lhsysmenu = getsystemmenu(hwnd, 0&)
lret = appendmenu(lhsysmenu, mf_separator, 0&, vbnullstring)
lret = appendmenu(lhsysmenu, mf_string, idm_about, "about...")
show
procold = setwindowlong(hwnd, gwl_wndproc, addressof windowproc)
end sub
private sub form_mousedown(button as integer, shift as integer, x as single, y as single)
dim r as rect
dim p as pointapi
if button = vbrightbutton then
getcursorpos p
trackpopupmenu lhsysmenu, 0, p.x, p.y, 0, me.hwnd, r
end if
end sub
private sub form_unload(cancel as integer)
setwindowlong hwnd, gwl_wndproc, procold
end sub
'模块
option explicit
public declare function trackpopupmenu lib "user32" (byval hmenu as long, byval wflags as long, byval x as long, byval y as long, byval nreserved as long, byval hwnd as long, lprc as rect) as long
public declare function getcursorpos lib "user32" (lppoint as pointapi) as long
public declare function setwindowlong lib "user32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
public declare function callwindowproc lib "user32" alias "callwindowproca" (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long
public declare function appendmenu lib "user32" alias "appendmenua" (byval hmenu as long, byval wflags as long, byval widnewitem as long, byval lpnewitem as string) as long
public declare function getsystemmenu lib "user32" (byval hwnd as long, byval brevert as long) as long
public declare function getclassname lib "user32" alias "getclassnamea" (byval hwnd as long, byval lpclassname as string, byval nmaxcount as long) as long
public declare function getwindowthreadprocessid lib "user32" (byval hwnd as long, lpdwprocessid as long) as long
public declare function getcurrentprocessid lib "kernel32" () as long
public declare function getwindowrect lib "user32" (byval hwnd as long, lprect as rect) as long