Re: Window creation in C++
Actually, as far I understand there must be a main loop where you will check the messages comming from windows, and those are tracked in WinMain, secondly each window has its own windows procedure which controls messages of specific window individually.
You may create a window in main like this
Code:
#include <windows.h>
HWND myWin;
int main(int argc, char *argv[])
{
myWin = CreateWindow(
WC_DIALOG,"My Window",
WS_OVERLAPPEDWINDOW,
100,100,400,300,
HWND_DESKTOP,
NULL,
NULL,
NULL
);
ShowWindow(myWin, SW_SHOW);
//do whatsoever is nedded
DestroyWindow(myWin);
return 0;
}
But problem is where are you going to track the messages. there are hundreds of messages which are sent/processed by windows.
If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.
I am also interested in knowing the implementation of custom message map rather than using the default method.
I hope it was of a little 0.01% help.
regards
Re: Window creation in C++
Quote:
Originally Posted by Ali Imran
If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.
Code:
MSG msg = {0};
while ( GetMessage(&msg, myWin, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Re: Window creation in C++
Quote:
Originally Posted by Ali Imran
But problem is where are you going to track the messages. there are hundreds of messages which are sent/processed by windows.
Yes, indeed there are many hundreds of messages, but luckily we have not to process all of them and just send "not interesting" ones to default window procedure (by a call of DefWindowProc).
Quote:
Originally Posted by Ali Imran
If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.
Not a place for "message map" there. Maybe a call of SetWindowLong to assign a window procedure followed by a message loop.
Code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch(nMsg)
{
// ...
// ...
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}
return TRUE;
}
int main(int argc, char* argv[])
{
HWND hWnd = CreateWindow(WC_DIALOG,
"My Window",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
100,100,400,300,
HWND_DESKTOP,
NULL,
NULL,
NULL);
::SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
::UpdateWindow(hWnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
However, I cannot guarantee you'll have not headaches developing the code above and I do not recommend it.
Once you suggested a dialog, much more easier and correct is to call DialogBox or CreateDialog for making a modal or modeless dialog, respectively.
Quote:
Originally Posted by Ali Imran
I am also interested in knowing the implementation of custom message map rather than using the default method.
PS. Please, explain what do you mean with "custom message map" and "default method".
Re: Window creation in C++
You can also handle specific messages that you need to handle(sent in the message loop) with a WindowProcedure.
Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
default:break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
This returns DefWindowProc that handles the rest of the messages with their defaults. LRESULT is a typedef for long and CALLBACK is a typedef for __stdcall.