-
July 31st, 2015, 09:06 AM
#1
win32- hook: how changing the aligment text and position of messagebox?
i have these functions for show a messagebox:
Code:
int GetWindowString(HWND hwnd, std::string &s)
{
char buffer[65536];
int txtlen=GetWindowTextLength(hwnd) + 1;
GetWindowText(hwnd, buffer, txtlen);
s = buffer;
return txtlen;
}
HHOOK hhookCBTProc = 0;
LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
{
HWND hwnd = (HWND)wParam;
// At this point you have the hwnd of the newly created
// message box that so you can position it at will
HDC test=GetDC(hwnd);
//these 2 messages aren't showed
if(SetTextAlign(test,TA_CENTER)==GDI_ERROR)
MessageBox(NULL,"aligment", "error", MB_OK);
if(SetWindowPos(hwnd, 0, 10,10,0,0,SWP_NOSIZE | SWP_NOZORDER)==FALSE)
MessageBox(NULL,"position", "error", MB_OK);
ReleaseDC(hwnd,test);
}
}
return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
}
void MessageBox(string text, string title="",UINT flags=MB_OK, bool modal=false)
{
HWND parent=NULL;
if(modal==true)
parent=ActivatedForm;
if(title=="")
{
GetWindowString(ActivatedForm,title);
}
if(hhookCBTProc==0)
hhookCBTProc = SetWindowsHookEx(WH_CBT,
pfnCBTMsgBoxHook,
0, GetCurrentThreadId());
if(hhookCBTProc==NULL)
::MessageBox(parent,"error","error", MB_OK);
::MessageBox(parent,text.c_str(),title.c_str(), flags);
if(hhookCBTProc!=0)
UnhookWindowsHookEx(hhookCBTProc);
}
i have an error on pfnCBTMsgBoxHook() function. how i know? the aligment and position aren't changed. why?
and no error messageboxes are showed
Last edited by Cambalinho; July 31st, 2015 at 10:25 AM.
-
July 31st, 2015, 10:28 AM
#2
Re: win32- hook: why i can't change anything?
changing the pcs->x and pcs->y values, the messagebox is changed in real time:
Code:
LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
{
HWND hwnd = (HWND)wParam;
// At this point you have the hwnd of the newly created
// message box that so you can position it at will
//SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
RECT parentrct;
GetWindowRect(ActivatedForm,&parentrct);
LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
pcs->x=x;
pcs->y=y;
}
}
return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
}
anotherthing: we can't call UnhookWindowsHookEx() after show the messagebox... only when it's not needed.
the SendMessage() have 1 message for change the text aligment or is just for HDC?
-
July 31st, 2015, 10:38 AM
#3
Re: win32- hook: how changing the aligment text and position of messagebox?
1. Your test is strange: you are trying to create a MessageBox form the hook set on creation of message boxes; looks like infinite recursion.
2. Because of #1 and multiple other reasons, NEVER use MessageBox() for debugging/tracing. Instead, call ::OutputDebugString().
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 31st, 2015, 10:53 AM
#4
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by VladimirF
1. Your test is strange: you are trying to create a MessageBox form the hook set on creation of message boxes; looks like infinite recursion.
2. Because of #1 and multiple other reasons, NEVER use MessageBox() for debugging/tracing. Instead, call ::OutputDebugString().
1 - humm.... confused me that
2 - i'm using the codeBlocks. for now, i don't know see the OutputDebugString() messages.
i'm reading: http://wiki.codeblocks.org/index.php...h_Code::Blocks
but i only see the watches window nothing more
-
July 31st, 2015, 11:12 AM
#5
Re: win32- hook: how changing the aligment text and position of messagebox?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 31st, 2015, 11:29 AM
#6
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by VladimirF
thanks for all
going back to the code. the messagebox 1 STATIC child control, wright?
so i can catch its HWND:
Code:
HWND hwnd = (HWND)wParam;
// At this point you have the hwnd of the newly created
// message box that so you can position it at will
//SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
HWND hwnd2 = FindWindowEx(hwnd, NULL, TEXT("STATIC"), NULL);
if (hwnd2!=NULL)
SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
else
MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
but the hwnd2 recives NULL. what you can tell me?
(the GetLastError() is 0)
-
July 31st, 2015, 02:06 PM
#7
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by Cambalinho
...but the hwnd2 recives NULL. what you can tell me?
I can say that the STATIC child control is not yet created. Are you calling that from your HCBT_CREATEWND? If yes - it is too early. Try HCBT_ACTIVATE.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 31st, 2015, 02:43 PM
#8
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by VladimirF
I can say that the STATIC child control is not yet created. Are you calling that from your HCBT_CREATEWND? If yes - it is too early. Try HCBT_ACTIVATE.
yes you have wright. thanks:
Code:
HHOOK hhookCBTProc = 0;
LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode==HCBT_ACTIVATE)//center the text on STATIC child control
{
HWND hwnd = (HWND)wParam;
HWND hwnd2 = FindWindowEx(hwnd, NULL,"STATIC",NULL);
if (hwnd2!=NULL)
{
SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
//MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
}
}
else if (nCode == HCBT_CREATEWND)//center the messagebox on it's parent
{
CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
{
//HWND hwnd = (HWND)wParam;
// At this point you have the hwnd of the newly created
// message box that so you can position it at will
//SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
RECT parentrct;
GetWindowRect(ActivatedForm,&parentrct);
LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
pcs->x=x;
pcs->y=y;
}
}
return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
}
void MessageBox(string text, string title="",UINT flags=MB_OK | MB_ICONERROR, bool modal=true)
{
HWND parent=NULL;
if(modal==true)
parent=ActivatedForm;
if(title=="")
{
GetWindowString(ActivatedForm,title);
}
if(hhookCBTProc==0)
hhookCBTProc = SetWindowsHookEx(WH_CBT,
pfnCBTMsgBoxHook,
0, GetCurrentThreadId());
if(hhookCBTProc==NULL)
OutputDebugString("error");
::MessageBox(parent,text.c_str(),title.c_str(), flags);
}
now i'm thinking on anotherthing, that i don't know how: can i do 2 new flags for the messagebox() and how can i test if that flag is used?
-
July 31st, 2015, 03:37 PM
#9
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by Cambalinho
can i do 2 new flags for the messagebox() and how can i test if that flag is used?
What flags are you asking about? Could you please explain?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 31st, 2015, 03:43 PM
#10
Re: win32- hook: how changing the aligment text and position of messagebox?
Originally Posted by VladimirF
What flags are you asking about? Could you please explain?
the messagebox() have MB_RIGHT, MB_OK and others. imagine that i want do more 2 MB_CENTER and MB_PARENTCENTER. what values can i use?
-
August 1st, 2015, 08:41 AM
#11
Re: win32- hook: why i can't change anything?
i must create enum values\flags, but if i don't know, correctly, the messagebox flags values, i can't add them. so i inclued my own enum:
Code:
HHOOK hhookCBTProc = 0;
//for flags we must do 2^X
enum MessageBoxExtendedFlags
{
MB_EX_NONE=1, //2^0 = 1
MB_EX_MODAL=2, //2^1 = 2
MB_EX_PARENTCENTER=4, //2^2 = 4
MB_EX_TEXTCENTER=8 //2^3 = 8
};
UINT MGBEXTENDED=0;
LRESULT CALLBACK MsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode==HCBT_ACTIVATE)//center the text on STATIC child control
{
HWND hwnd = (HWND)wParam;
HWND hwnd2 = FindWindowEx(hwnd, NULL,"STATIC",NULL);
if (hwnd2!=NULL && (MGBEXTENDED & MB_EX_TEXTCENTER))
{
SetWindowLong(hwnd2, GWL_STYLE, GetWindowLong(hwnd2, GWL_STYLE) | SS_CENTER);
//MessageBox(NULL, to_string(GetLastError()).c_str(), "error", MB_OK);
}
}
else if (nCode == HCBT_CREATEWND)//center the messagebox on it's parent
{
CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
{
HWND hwnd = (HWND)wParam;
// At this point you have the hwnd of the newly created
// message box that so you can position it at will
//SendMessage(hwnd, WM_SETA ,(WPARAM) font, TRUE)
if(MGBEXTENDED & MB_EX_PARENTCENTER)
{
RECT parentrct;
GetWindowRect(ActivatedForm,&parentrct);
LONG x=parentrct.left+(parentrct.right-parentrct.left)/2 - pcs->cx/2;
LONG y=parentrct.top+(parentrct.bottom-parentrct.top)/2 - pcs->cy/2;
pcs->x=x;
pcs->y=y;
}
}
}
return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
}
int MessageBox(string text, string title="",UINT flags=MB_OK, UINT ExtendedFlags=MB_EX_NONE)
{
HWND parent=NULL;
MGBEXTENDED=ExtendedFlags;
if(ExtendedFlags & MB_EX_MODAL)
parent=GetForegroundWindow();
if(title=="")
{
GetWindowString(ActivatedForm,title);
}
if(hhookCBTProc==0)
hhookCBTProc = SetWindowsHookEx(WH_CBT,
MsgBoxHook,
0, GetCurrentThreadId());
if(hhookCBTProc==NULL)
OutputDebugString("error");
return ::MessageBox(parent,text.c_str(),title.c_str(), flags);
}
maybe with time i can include the checkbox
out-off-topic: i have 2 windows(not child), 1 above other. on back isn't enabled and on front is.
heres the 2 situations:
1 - i'm on 2nd window and i show 1 messagebox. then i close the messagebox. and then the window. why the window on back loses the focus and other window\program get the focus?
2 - i close the 2nd window. the window on back get the focus... normal.
can anyone explain to me these ZOrder problem?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|