Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


#ActualPsychopathetica

Posted 23 July 2013 - 01:46 PM

I was terminating the process because I had a situation in the past where the program remained in the process list after I exited out. But after commenting out the 3 lines that terminated the process, I see it exit ok still. So I went ahead and removed these:

 

            HANDLE Process;
            Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
            TerminateProcess(Process , 0);

 

So I tried out Khatharrs method and it works like a charm. Thanks a bunch. And heres the new code I have:

#include <windows.h>

HWND hWnd;
MSG msg;
WNDCLASSEX wc;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
    wc.lpfnWndProc   = WindowProcedure;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "Window";
    wc.hIconSm       = NULL;

    RegisterClassEx(&wc);
    hWnd = CreateWindowEx (0, "Window", "Win32", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    ShowWindow (hWnd, nCmdShow);
    while (1)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
        {
            if (WM_QUIT == msg.message) break;
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        else
        {
            //Render code here
        }
        
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        case WM_KEYDOWN:
            if(wParam == VK_ESCAPE)
            {
                DestroyWindow(hWnd);
                return(0);
            }
            break;
        case WM_PAINT:
            wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
            break;
        default:
            return DefWindowProc (hWnd, msg, wParam, lParam);
    }
    return 0;
}

#1Psychopathetica

Posted 23 July 2013 - 01:38 PM

I was terminating the process because I had a situation in the past where the program remained in the process list after I exited out. But after commenting out the 3 lines that terminated the process, I see it exit ok still. So I went ahead and removed these:

 

            HANDLE Process;
            Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
            TerminateProcess(Process , 0);

 

So I tried out Khatharrs method and it works like a charm. Thanks a bunch. And heres the new code I have:

 

#include <windows.h>

HWND hWnd;
MSG msg;
WNDCLASSEX wc;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
    wc.lpfnWndProc   = WindowProcedure;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "Window";
    wc.hIconSm       = NULL;

    RegisterClassEx(&wc);
    hWnd = CreateWindowEx (0, "Window", "Win32", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    ShowWindow (hWnd, nCmdShow);
    while (1)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
        {
            if (WM_QUIT == msg.message) break;
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        else
        {
            //Render code here
        }
        
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        case WM_KEYDOWN:
            if(wParam == VK_ESCAPE)
            {
                DestroyWindow(hWnd);
                return(0);
            }
        case WM_PAINT:
            wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);

        default:
            return DefWindowProc (hWnd, msg, wParam, lParam);
    }
    return 0;
}

PARTNERS