simle form in c++

CODE

bool InitWindowApp(HINSTANCE instanceHandle, int Show);

int Run();

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd)
{
if(!InitWindowApp(hInstance, nShowCmd))
{
::MessageBox(0, "Failed to create Window!", "Fail", MB_OK);
return 0;
}
}

bool InitWindowApp(HINSTANCE instanceHandle, int Show)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instanceHandle;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH) ::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "MyWindow";

if(!::RegisterClass(&wc))
{
::MessageBox(0, "Failed to register Window!", "Fail", MB_OK);
return false;
}

MainWindowHandle = ::CreateWindow("MyWindow", "MyWindow", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instanceHandle, 0);

if(MainWindowHandle == 0)
{
::MessageBox(0, "Failed to create Window!", "Fail", MB_OK);
return false;
}

::ShowWindow(MainWindowHandle, Show);
::UpdateWindow(MainWindowHandle);

return true;
}

int Run()
{
MSG msg;

while(true)
{
if(::PeekMessage(&msg,0,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
break;
}

::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
{
::DestroyWindow(MainWindowHandle);
}
return 0;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}

return ::DefWindowProc(windowHandle, msg, wParam, lParam);
}



Hi

I`m new to c++ .. i do most of windows programing in C#.. but i also want to learn C++.. not C++ .NET .. i understand that the code above creates a window.. i dont understnad..
i cand see void main() how ill this start f void main isnt there



Answer this question

simle form in c++

  • Tamer Shaaban

    Then Petzold (mentioned above) is probably your best bet.

  • Zappo

    If you are trying to learn C++ for the sake of learning C++, then I stronly suggest that you stay away as far as possible from any kind of GUI programming until you get comfortable with the language. GUI programming in C++ is inherently messy, because often you have to deal with little idiosyncrasies of the platform that you are programming for as opposed to focussing on the task at hand. So if you want to learn C++, then I suggest a good book on the language itself, such as: "C++ How to program" by H.M. Deitel and P.J. Deitel. If you want to get into the type of programming such as what you posted for sample code above, then I suggest that you get "Programming Windows" by Charles Petzold.



  • Stephan Ruhland

    well.. i`m not interested in C++ sintax.. .. as you know C# its like c++.. i know C++ but let`s say.. i dont know very good .. programing windows.. i dont know


  • kawano1h

    Instead of main, it has WinMain which is the entry point for applications targetting the GUI subsystem (as opposed to the console).

    See http://msdn.microsoft.com/library/default.asp url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/winmain.asp



  • simle form in c++