Click to See Complete Forum and Search --> : Window creation in C++


vc185002
February 27th, 2006, 11:40 PM
can we create a window without using-->

int APIENTRY WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdshow)

right now i m creating a window using CreateWindow(,,,,) but in this we hav to pass a register class which takes the HINSTANSE of the WinMian function.
I want to know if we can create a window without using this as i have to work on C++ not on Win32 ie

void main (void)
{
//here i want to create a window
}


Virender

Ali Imran
February 28th, 2006, 02:19 AM
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


#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

NoHero
February 28th, 2006, 06:05 AM
If any guru gets to this thread please implement something for this fellow by replacing '//do whatsoever is nedded' with some message map.

MSG msg = {0};

while ( GetMessage(&msg, myWin, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

ovidiucucu
February 28th, 2006, 06:27 AM
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).
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.
#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.
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".

Notsosuperhero
February 28th, 2006, 06:31 AM
You can also handle specific messages that you need to handle(sent in the message loop) with a WindowProcedure.


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.