How to use DialogBox in Windows API - winapi

I'm learning windows api for some weeks, now I got a problem, my code is like this
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case(...)
DialogBox(hInstance,MAKEINTRESOURCE(IDD_MYDIALOG),hwnd,(DLGPROC)MyDialogProc);
return 0;
}
bool MyDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch (message){
...
}
return false;
}
I don't know what else should I do before I use DialogBox where should I place EndDialog(). IDD_MYDIALOG is a resource file created by myself. I don't understand what is hInstance and how to get it, I think I just need a simple example, than I can know how use DialogBox. Thank you for helping!

Related

How can I get a message box to appear every time I left-click in a window?

I want to open a message box with the word "left" when I left click the mouse.
So I used wndproc and MK_LBUTTON, but the wndproc function wrote the code, but the WinMain part doesn't know how to write the code.
I don't want to open a window, but when I searched on Google, I only have a code example that only shows a window.
(Opening the window did not solve the problem ..)
What should I do? Help
(If you've written as much as possible but don't understand the question, please ask me and I'll answer it.
And I'm not good at English, so I wrote a translator.)
my code(try)
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
, LPSTR lpszCmdParam, int nCmdShow)
{
//???
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
switch (iMessage) {
case MK_LBUTTON:
MessageBox(hWnd, TEXT("left"),TEXT("message"), MB_OK);
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
MK_LBUTTON is not a message, you need to catch WM_LBUTTONDOWN, WM_LBUTTONUP or WM_LBUTTONDBLCLK.
These messages are only sent to the active window where the mouse is clicked. If you want to catch clicks on all windows then you need to use a mouse hook and pump messages.

Set WH_GETMESSAGE hook makes the system dead

I use SetWindowsHookEx to install a global GETMESSAGE hook in a dll like this:
SetWindowsHookEx(WH_GETMESSAGE,HookProc,hModule,0);
and the the hook procedure within the dll too.But when i called InstallHook(this function is exported from the DLL) to install the GETMESSAGE hook,booom, the system can not respon the mouse operation,the windows on the desktop can be close or move) ,here is my HookProc Code:
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
WCHAR szMsg[MAX_PATH] = { 0 };
PMSG pMsg = (PMSG)lParam;
wsprintf(szMsg, L"MSG_FROM:%08x,MSG_TYPE:%d", pMsg->hwnd,pMsg->message);
OutputDebugString(szMsg);
return CallNextHookEx(NULL,nCode, wParam, lParam);
}
I've read the MSDN for many times,it just said i must process the message if the ncode is HC_ACTION,but,i don't know how to process the message.I think there must be someone else has this problem too, so, hope somebody give some help, and tell me how you process this trouble.
Thanks a lot.

How to read the data from lParam (the pointers value)

I made a small application that sends text to notepad via SendMessage and EM_REPLACESEL.
Now I’m trying to hook notepad to get the EM_REPLACESEL value (the lParam value and in this case the “GET THIS TEXT” text).
EDIT: See this picture: http://i.stack.imgur.com/8scNL.jpg
The hook works fine, my problem is to listen for the EM_REPLACESEL message and grab the value from the lParam.
This code works fine, when messages are sent to notepad:
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
Beep (2000,100);
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
So now I want to intercept EM_REPLACESEL messages. This do not work:
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == EM_REPLACESEL)
{
Beep (2000,100);
}
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
1) How to listen for the EM_REPLACESEL message?
2) When I have gotten the message how to grab the lParam value and e.g. show it in a MessageBox. Something like this:
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == EM_REPLACESEL)
{
MSG *lpMsg;
lpMsg = (MSG *) lParam;
MessageBox(NULL,(LPCWSTR)lpMsg,NULL,NULL);
}
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
Thanks
EM_REPLACESEL is a sent message, not a posted message, so you need to use a WH_CALLWNDPROC hook instead of a WH_GETMESSAGE hook, eg:
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
CWPSTRUCT* cwps = (CWPSTRUCT*)lParam;
if (cwps->message == EM_REPLACESEL) {
Beep (2000,100);
// etc..
}
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
... = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, ...),
Your GetMsgProc() callback is coded wrong. Carefully read the linked MSDN page to see what the arguments of the callback mean. The nCode argument is not the message number, it specifies whether or not you should process the message. You want to use the passed lParam to recover the message that you intercepted. Make it look similar to this instead:
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
MSG* pmsg = (MSG*)lParam;
if (pmsg->message == WM_LBUTTONDOWN) {
Beep (2000,100);
// etc..
}
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
Do note that you appear to use the WH_GETMESSAGE hook. It only works for messages that are posted to the message queue with PostMessage(). But EM_REPLACESEL is a message that's sent with SendMessage(). That requires a different hook, WH_CALLWNDPROC or WH_CALLWNDPROCRET.
This is how I normally do it.
LRESULT CALLBACK GetMsgProc(MSG nCode, WPARAM wParam, LPARAM lParam)
{
while(GetMessage(&nCode, NULL, 0, 0) > 0)
{
if(nCode.message == EM_REPLACESEL)
{
//Do something
}
else
DispatchMessage(&nCode);
}
return 0;
}

Error in windows api program: cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM) to WNDPROC

i've made a simple windows api program having the functions WinMain() and WinProc() , but i'm getting this error :
error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,UINT,LPARAM,WPARAM)' to 'WNDPROC'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast
#include<windows.h>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowClass;
static LPCTSTR szAppName = L"OFWin";
HWND hWnd;
MSG msg;
WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = WindowProc; // error
....
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam)
{ ..... }
the program's taken word to word from my book (ivor horton's beginning visual c++ 2010), what's wrong?
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, LPARAM lParam, WPARAM wParam);
Here's your problem: the LPARAM and WPARAM are backwards, it should be:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
WPARAM and LPARAM have two different types (UINT_PTR and INT_PTR respectively - for historical reasons mostly), so you get a type related error if you accidentally swap them around. Which is a lucky thing in your case: if they were the same type, then instead of getting a compiler error, the code would compile fine, and you'd instead spend some time wondering why your wndproc was apparently getting mixed up parameters passed to it!

WM_MOUSEHOVER in a global hook

I have implemented custom tooltips for a couple of controls in my application (MFC) but I would like to make it general for all the controls.
Right now I am calling TrackMouseEvent in WM_MOUSEMOVE and then catching WM_MOUSEHOVER (both in the overwritten function WindowProc of the control). But this way I have to duplicate code for every control. So my intention was to set a global hook for mouse events and there ask the control for the message to display in the tooltip.
The problem is that I am not able to catch WM_MOUSEHOVER in a global hook. This is the code:
hMouseHook = SetWindowsHookEx( WH_MOUSE,
CallWndMouseProc,
NULL,
AfxGetThread()->m_nThreadID);
hMainHook = SetWindowsHookEx( WH_CALLWNDPROC,
CallWndProc,
NULL,
AfxGetThread()->m_nThreadID);
LRESULT CALLBACK CallWndMouseProc( int nCode,
WPARAM wParam,
LPARAM lParam )
{
if(nCode == HC_ACTION)
{
MOUSEHOOKSTRUCT* pwp = (MOUSEHOOKSTRUCT*)lParam;
TRACE( _T("message: %x hwnd: %x x: %d y: %d\n"),
wParam,
pwp->hwnd,
pwp->pt.x,
pwp->pt.y);
TRACKMOUSEEVENT eventTrack;
eventTrack.cbSize = sizeof(TRACKMOUSEEVENT);
eventTrack.dwFlags = TME_HOVER;
eventTrack.dwHoverTime = HOVER_DEFAULT;
eventTrack.hwndTrack = pwp->hwnd;
_TrackMouseEvent(&eventTrack);
if(wParam == WM_MOUSEHOVER)
{
AfxMessageBox(_T("CallWndMouseProc: WM_MOUSEHOVER"));
}
}
// let the messages through to the next hook
return CallNextHookEx( hMouseHook,
nCode,
wParam,
lParam);
}
LRESULT CALLBACK CallWndProc( int nCode,
WPARAM wParam,
LPARAM lParam )
{
if(nCode == HC_ACTION)
{
CWPSTRUCT *pData = (CWPSTRUCT*)lParam;
if(pData->message == WM_MOUSEHOVER)
{
AfxMessageBox(_T("CallWndProc: WM_MOUSEHOVER"));
}
}
// let the messages through to the next hook
return CallNextHookEx( hMainHook,
nCode,
wParam,
lParam);
}
Both hooks are being call for the rest of messages and I am sure WM_MOUSEHOVER is being sent because it's capture in the WindowProc function. For instance this is the WindowProc function for a custom CListBox:
LRESULT CMyListBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_MOUSEHOVER)
{
AfxMessageBox(_T("WindowProc: WM_MOUSEHOVER"));
}
return CListBox::WindowProc(message, wParam, lParam);
}
Ok. So what I am missing? It's just not possible to capture this message in a global hook? Is there other way to make this without having to put the same code in every single control?
Thanks.
Javier
WM_MOUSEHOVER is posted to the thread's message queue, so you won't see it with WH_CALLWNDPROC (that's for sent messages). WH_MOUSE does get posted messages, so I find it a little strange that you aren't seeing it... Perhaps the hook only gets low level mouse input messages? Try a WH_GETMESSAGE hook.

Resources