Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Slow "Hello World" Win32 App

I have had problems with setting up a Win32 App that just pops up a window with the simple minimize, exit, etc buttons in the upper right. I have three problems:
1. Getting the minimize, exit, etc buttons in the upper right
2. Getting the program to not be so slow that it basically crashes the system
3. Getting the program to stop running when the exit button is pressed

So I have two questions:
Does anyone know what the bug, bottleneck, etc is in the below code:
[code]
#include

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpszClassName = "Win32 Class";
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
return 0;
}

hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "Win32 Class", "My Window", WS_OVERLAPPED, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
return 0;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

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

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
[/code]

Also, is there a window/class style that puts the exit button in the upper right?


Thank you,
Mikfig

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : I have had problems with setting up a Win32 App that just pops up a
    : window with the simple minimize, exit, etc buttons in the upper
    : right. I have three problems:
    : 1. Getting the minimize, exit, etc buttons in the upper right
    : 2. Getting the program to not be so slow that it basically crashes
    : the system
    : 3. Getting the program to stop running when the exit button is
    : pressed
    :
    : So I have two questions:
    : Does anyone know what the bug, bottleneck, etc is in the below code:
    [color=Blue]See RED: (also, no idea, why you see it as "slow"... can you explain more?)[/color]
    : [code]:
    : #include
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    :
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : {
    : WNDCLASSEX wc;
    : HWND hWnd;
    : MSG msg;
    :
    : wc.cbClsExtra = 0;
    : wc.cbWndExtra = 0;
    : wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    : wc.style = CS_HREDRAW | CS_VREDRAW;
    : wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    : wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : wc.hInstance = hInstance;
    : wc.lpszClassName = "Win32 Class";
    : wc.lpszMenuName = NULL;
    : wc.lpfnWndProc = WndProc;
    : wc.cbSize = sizeof(WNDCLASSEX);
    : wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    :
    : if(!RegisterClassEx(&wc))
    : {
    : MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
    : return 0;
    : }
    :
    : hWnd = CreateWindowEx([color=Red]WS_EX_APPDWINDOW[/color], "Win32 Class", "My Window", [color=Red]WS_OVERLAPPEDWINDOW[/color], 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
    : if(hWnd == NULL)
    : {
    : MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
    : return 0;
    : }
    :
    : ShowWindow(hWnd, nCmdShow);
    : UpdateWindow(hWnd);
    :
    : while(GetMessage(&msg, [color=Red]0[/color], 0, 0))
    [color=Red]
    : //
    : // Zero here ^^^ instead of hWnd, because when you post
    : // WM_QUIT message - it goes to the thread, not to the window.
    : //[/color]
    : {
    : TranslateMessage(&msg);
    : DispatchMessage(&msg);
    : }
    :
    : return msg.wParam;
    : }
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : {
    : switch(msg)
    : {
    : case WM_CLOSE:
    : DestroyWindow(hWnd);
    : break;
    : case WM_DESTROY:
    : PostQuitMessage(0);
    : break;
    : default:
    : return DefWindowProc(hWnd, msg, wParam, lParam);
    : }
    : return 0;
    : }
    : [/code]:
    :
    : Also, is there a window/class style that puts the exit button in the
    : upper right?
    :
    :
    : Thank you,
    : Mikfig
    :
  • mikfigmikfig Member Posts: 7
    Sorry, what I meant by it is really slow is that it took a lot longer to open the application than what makes sense for such a simple application. It took maybe half a minute or so to open, but when it did open it basically crashed my system because Visual Studio could not close and the application itself would not close with Alt + F4. Basically I had to do a hard reset on my system because I was unable to get it to a soft shutdown.

    The reason I say it is slower than it should be is also because it runs a lot slower than for example this application:
    [code]
    #include
    #include
    #include

    #include "header.h"

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    bool initD3D();
    void writeDeviceCapsToFile(IDirect3D9* lpD3D);

    IDirect3D9* lpD3D;

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    HWND hWnd;
    WNDCLASS wndclass;
    MSG msg;

    wndclass.style = CS_VREDRAW | CS_HREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = "Windows Class";

    if(!(RegisterClass(&wndclass)))
    {
    MessageBox(NULL, "Error", "Error: Couldn't register window class.", MB_OK);
    PostQuitMessage(0);
    }

    RECT R = {0, 0, 800, 600};
    AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);

    if(!(hWnd = CreateWindow("Windows Class", "DX3D 9 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, R.right, R.bottom, NULL, NULL, hInstance, NULL)))
    {
    MessageBox(NULL, "Error", "Error: Couldn't create window.", MB_OK);
    PostQuitMessage(0);
    }

    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    if(initD3D())
    {
    writeDeviceCapsToFile(lpD3D);
    }

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

    lpD3D->Release();
    return (int)msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CLOSE:
    DestroyWindow(hWnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
    }

    bool initD3D()
    {
    lpD3D = Direct3DCreate9(D3D_SDK_VERSION);

    if( !lpD3D )
    {
    MessageBox(NULL, "Error", "Error getting interface to Direct3D.", MB_OK);
    PostQuitMessage(0);
    return false;
    }
    return true;
    }

    void writeDeviceCapsToFile(IDirect3D9* lpD3D)
    {
    UINT modeCount;
    D3DDISPLAYMODE displayMode;
    std::stringstream ssBuffer;
    DWORD bytesWritten;
    char tempStr[500];
    D3DCAPS9 caps;
    lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

    HFILE hFile = (HFILE) CreateFile("directx video card general caps.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL,
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    ssBuffer << "MaxPointSize: " << caps.MaxPointSize << "
    ";
    ssBuffer << "MaxPrimitiveCount: " << caps.MaxPrimitiveCount << "
    ";
    ssBuffer << "MaxActiveLights: " << caps.MaxActiveLights << "
    ";
    ssBuffer << "MaxUserClipPlanes: " << caps.MaxUserClipPlanes << "
    ";
    ssBuffer << "MaxVertexIndex: " << caps.MaxVertexIndex << "
    ";
    ssBuffer << "MaxVertexShaderConst: " << caps.MaxVertexShaderConst << "
    ";

    ssBuffer << "D3DPRESENT_INTERVAL_IMMEDIATE is ";
    if(caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
    {
    ssBuffer << "supported.";
    }
    else
    {
    ssBuffer << "not supported.";
    }
    ssBuffer << "
    ";

    ssBuffer << "D3DPTADDRESSCAPS_CLAMP is ";
    if(caps.TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
    {
    ssBuffer << "supported.";
    }
    else
    {
    ssBuffer << "not supported.";
    }
    ssBuffer << "
    ";

    ssBuffer << "D3DFVFCAPS_PSIZE is ";
    if(caps.FVFCaps & D3DFVFCAPS_PSIZE)
    {
    ssBuffer << "supported.";
    }
    else
    {
    ssBuffer << "not supported.";
    }
    ssBuffer << "
    ";

    ssBuffer << "D3DCAPS2_CANAUTOGENMIPMAP is ";
    if(caps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
    {
    ssBuffer << "supported.";
    }
    else
    {
    ssBuffer << "not supported.";
    }
    ssBuffer << "
    ";

    ssBuffer << "D3DPRASTERCAPS_DITHER is ";
    if(caps.RasterCaps & D3DPRASTERCAPS_DITHER)
    {
    ssBuffer << "supported.";
    }
    else
    {
    ssBuffer << "not supported.";
    }
    ssBuffer << "
    ";

    int sizeOfStr = ssBuffer.str().size() + 1;
    sprintf(tempStr, "%s", ssBuffer.str().c_str());
    WriteFile((HANDLE) hFile, (char*) tempStr, (DWORD) (sizeOfStr), &bytesWritten, NULL);
    ssBuffer.str("");
    CloseHandle((HANDLE) hFile);
    }
    [/code]

    And also I have one more question.
    Why will this application stop running when the exit button is pressed:
    [code]
    #define D3D_DEBUG_INFO

    #include <windows.h>
    #include
    #include
    #include
    #include

    #include "header.h"

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    bool initD3D(HWND hWnd);
    void drawLines();
    bool isDeviceLost();
    void writeToDebug(std::stringstream* text);

    IDirect3D9* lpD3D;
    IDirect3DDevice9* lpD3Ddevice;
    D3DPRESENT_PARAMETERS d3d_pm;
    HFILE hFile;
    bool hitBreakPoint;

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    HWND hWnd;
    WNDCLASS wndclass;
    MSG msg;
    msg.message = WM_NULL;

    wndclass.style = CS_VREDRAW | CS_HREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = "Windows Class";

    if(!(RegisterClass(&wndclass)))
    {
    MessageBox(NULL, "Error", "Error: Couldn't register window class.", MB_OK);
    PostQuitMessage(0);
    }

    RECT R = {0, 0, 800, 600};
    AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);

    if(!(hWnd = CreateWindow("Windows Class", "DX3D 9 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, R.right, R.bottom, NULL, NULL, hInstance, NULL)))
    {
    MessageBox(NULL, "Error", "Error: Couldn't create window.", MB_OK);
    PostQuitMessage(0);
    }

    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    hFile = (HFILE) CreateFile("debug.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL,
    CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

    if(initD3D(hWnd))
    {
    while(msg.message != WM_QUIT)
    {
    while(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(msg.message == WM_CLOSE)
    {
    MessageBox(NULL, "Debug", "Closing Window", MB_OK);
    }
    if(!isDeviceLost() && !hitBreakPoint)
    {
    drawLines();
    }
    }
    lpD3Ddevice->Release();
    lpD3D->Release();
    }
    CloseHandle((HANDLE) hFile);
    return (int)msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CLOSE:
    DestroyWindow(hWnd);
    hitBreakPoint = true;
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    hitBreakPoint = true;
    break;
    default:
    return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
    }

    bool initD3D(HWND hWnd)
    {
    lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
    D3DCAPS9 caps;
    DWORD behaviorFlags = NULL;
    ZeroMemory(&d3d_pm, sizeof(d3d_pm));

    if( !lpD3D )
    {
    MessageBox(NULL, "Error", "Error getting interface to Direct3D.", MB_OK);
    PostQuitMessage(0);
    return false;
    }

    if (FAILED(lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps)))
    {
    MessageBox(NULL, "Error", "Error getting capabilites of default graphics adapter.", MB_OK);
    PostQuitMessage(0);
    return false;
    }

    if(caps.DevCaps & D3DDEVCAPS_PUREDEVICE)
    behaviorFlags |= D3DCREATE_PUREDEVICE;
    if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
    behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
    behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    RECT winRect;
    GetClientRect(hWnd, &winRect);
    d3d_pm.BackBufferWidth = winRect.right;
    d3d_pm.BackBufferHeight = winRect.bottom;
    d3d_pm.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3d_pm.hDeviceWindow = hWnd;
    d3d_pm.Windowed = true;
    d3d_pm.SwapEffect = D3DSWAPEFFECT_DISCARD;
    if(SUCCEEDED(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, true)))
    {
    d3d_pm.BackBufferFormat = D3DFMT_X8R8G8B8;
    }
    else
    {
    d3d_pm.BackBufferFormat = D3DFMT_UNKNOWN;
    }
    if(caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
    {
    d3d_pm.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    }
    else
    {
    d3d_pm.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
    }
    d3d_pm.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3d_pm.EnableAutoDepthStencil = true;
    d3d_pm.BackBufferCount = 1;
    d3d_pm.Flags = 0;

    if(FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, behaviorFlags, &d3d_pm, &lpD3Ddevice)))
    return false;

    return true;
    }

    void drawLines()
    {
    LPD3DXLINE lpD3Dline;
    D3DXVECTOR2 points[2];

    lpD3Ddevice->Clear(NULL, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);

    if(SUCCEEDED(D3DXCreateLine(lpD3Ddevice, &lpD3Dline)) && (SUCCEEDED(lpD3Ddevice->BeginScene())))
    {
    if(SUCCEEDED(lpD3Dline->Begin()))
    {
    points[0].x = 20.0f;
    points[0].y = 10.0f;
    points[1].x = 20.0f;
    points[1].y = 500.0f;
    lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));

    points[0].x = 20.0f;
    points[0].y = 10.0f;
    points[1].x = 350.0f;
    points[1].y = 500.0f;
    lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));

    points[0].x = 700.0f;
    points[0].y = 10.0f;
    points[1].x = 700.0f;
    points[1].y = 500.0f;
    lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));

    points[0].x = 700.0f;
    points[0].y = 10.0f;
    points[1].x = 350.0f;
    points[1].y = 500.0f;
    lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));
    }
    lpD3Ddevice->EndScene();
    lpD3Ddevice->Present(NULL,NULL,NULL,NULL);
    }
    lpD3Dline->Release();
    }

    void writeToDebug(std::stringstream* text)
    {
    char tempStr[100];
    sprintf(tempStr, "%s", text->str().c_str());
    DWORD bytesWritten;

    WriteFile((HANDLE) hFile, (char*) &tempStr, (DWORD) (text->str().size() + 1), &bytesWritten, NULL);
    }

    bool isDeviceLost()
    {
    HRESULT hr = lpD3Ddevice->TestCooperativeLevel();

    if(SUCCEEDED(hr))
    {
    return false;
    }
    else if(hr == D3DERR_DEVICELOST)
    {
    return true;
    }
    else if(hr == D3DERR_DEVICENOTRESET)
    {
    lpD3Ddevice->Reset(&d3d_pm);
    return true;
    }
    else
    {
    MessageBox(NULL, "Error", "Internal device error...quiting application", MB_OK);
    PostQuitMessage(0);
    return true;
    }
    }
    [/code]

    : : I have had problems with setting up a Win32 App that just pops up a
    : : window with the simple minimize, exit, etc buttons in the upper
    : : right. I have three problems:
    : : 1. Getting the minimize, exit, etc buttons in the upper right
    : : 2. Getting the program to not be so slow that it basically crashes
    : : the system
    : : 3. Getting the program to stop running when the exit button is
    : : pressed
    : :
    : : So I have two questions:
    : : Does anyone know what the bug, bottleneck, etc is in the below code:
    : [color=Blue]See RED: (also, no idea, why you see it as "slow"... can
    : you explain more?)[/color]
    : : [code]: :
    : : #include
    : :
    : : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    : :
    : : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : : {
    : : WNDCLASSEX wc;
    : : HWND hWnd;
    : : MSG msg;
    : :
    : : wc.cbClsExtra = 0;
    : : wc.cbWndExtra = 0;
    : : wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    : : wc.style = CS_HREDRAW | CS_VREDRAW;
    : : wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    : : wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : : wc.hInstance = hInstance;
    : : wc.lpszClassName = "Win32 Class";
    : : wc.lpszMenuName = NULL;
    : : wc.lpfnWndProc = WndProc;
    : : wc.cbSize = sizeof(WNDCLASSEX);
    : : wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    : :
    : : if(!RegisterClassEx(&wc))
    : : {
    : : MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
    : : return 0;
    : : }
    : :
    : : hWnd = CreateWindowEx([color=Red]WS_EX_APPDWINDOW[/color], "Win32 Class", "My Window", [color=Red]WS_OVERLAPPEDWINDOW[/color], 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
    : : if(hWnd == NULL)
    : : {
    : : MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
    : : return 0;
    : : }
    : :
    : : ShowWindow(hWnd, nCmdShow);
    : : UpdateWindow(hWnd);
    : :
    : : while(GetMessage(&msg, [color=Red]0[/color], 0, 0))
    : [color=Red]
    : : //
    : : // Zero here ^^^ instead of hWnd, because when you post
    : : // WM_QUIT message - it goes to the thread, not to the window.
    : : //[/color]
    : : {
    : : TranslateMessage(&msg);
    : : DispatchMessage(&msg);
    : : }
    : :
    : : return msg.wParam;
    : : }
    : :
    : : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : : {
    : : switch(msg)
    : : {
    : : case WM_CLOSE:
    : : DestroyWindow(hWnd);
    : : break;
    : : case WM_DESTROY:
    : : PostQuitMessage(0);
    : : break;
    : : default:
    : : return DefWindowProc(hWnd, msg, wParam, lParam);
    : : }
    : : return 0;
    : : }
    : : [/code]: :
    : :
    : : Also, is there a window/class style that puts the exit button in the
    : : upper right?
    : :
    : :
    : : Thank you,
    : : Mikfig
    : :
    :

  • mikfigmikfig Member Posts: 7
    Sorry, about the second question, I just fixed it by changing the hWnd parameter in PeekMessage to 0 like you said previously.

    : Sorry, what I meant by it is really slow is that it took a lot
    : longer to open the application than what makes sense for such a
    : simple application. It took maybe half a minute or so to open, but
    : when it did open it basically crashed my system because Visual
    : Studio could not close and the application itself would not close
    : with Alt + F4. Basically I had to do a hard reset on my system
    : because I was unable to get it to a soft shutdown.
    :
    : The reason I say it is slower than it should be is also because it
    : runs a lot slower than for example this application:
    : [code]:
    : #include
    : #include
    : #include
    :
    : #include "header.h"
    :
    : LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    : bool initD3D();
    : void writeDeviceCapsToFile(IDirect3D9* lpD3D);
    :
    : IDirect3D9* lpD3D;
    :
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    : {
    : HWND hWnd;
    : WNDCLASS wndclass;
    : MSG msg;
    :
    : wndclass.style = CS_VREDRAW | CS_HREDRAW;
    : wndclass.lpfnWndProc = WndProc;
    : wndclass.cbClsExtra = 0;
    : wndclass.cbWndExtra = 0;
    : wndclass.hInstance = hInstance;
    : wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    : wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    : wndclass.lpszMenuName = NULL;
    : wndclass.lpszClassName = "Windows Class";
    :
    : if(!(RegisterClass(&wndclass)))
    : {
    : MessageBox(NULL, "Error", "Error: Couldn't register window class.", MB_OK);
    : PostQuitMessage(0);
    : }
    :
    : RECT R = {0, 0, 800, 600};
    : AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
    :
    : if(!(hWnd = CreateWindow("Windows Class", "DX3D 9 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, R.right, R.bottom, NULL, NULL, hInstance, NULL)))
    : {
    : MessageBox(NULL, "Error", "Error: Couldn't create window.", MB_OK);
    : PostQuitMessage(0);
    : }
    :
    : ShowWindow(hWnd, nShowCmd);
    : UpdateWindow(hWnd);
    :
    : if(initD3D())
    : {
    : writeDeviceCapsToFile(lpD3D);
    : }
    :
    : while(GetMessage(&msg, hWnd, 0, 0) > 0)
    : {
    : TranslateMessage(&msg);
    : DispatchMessage(&msg);
    : }
    :
    : lpD3D->Release();
    : return (int)msg.wParam;
    : }
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : {
    : switch(msg)
    : {
    : case WM_CLOSE:
    : DestroyWindow(hWnd);
    : break;
    : case WM_DESTROY:
    : PostQuitMessage(0);
    : break;
    : default:
    : return DefWindowProc(hWnd, msg, wParam, lParam);
    : }
    : return 0;
    : }
    :
    : bool initD3D()
    : {
    : lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
    :
    : if( !lpD3D )
    : {
    : MessageBox(NULL, "Error", "Error getting interface to Direct3D.", MB_OK);
    : PostQuitMessage(0);
    : return false;
    : }
    : return true;
    : }
    :
    : void writeDeviceCapsToFile(IDirect3D9* lpD3D)
    : {
    : UINT modeCount;
    : D3DDISPLAYMODE displayMode;
    : std::stringstream ssBuffer;
    : DWORD bytesWritten;
    : char tempStr[500];
    : D3DCAPS9 caps;
    : lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
    :
    : HFILE hFile = (HFILE) CreateFile("directx video card general caps.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL,
    : CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    :
    : ssBuffer << "MaxPointSize: " << caps.MaxPointSize << "
    ";
    : ssBuffer << "MaxPrimitiveCount: " << caps.MaxPrimitiveCount << "
    ";
    : ssBuffer << "MaxActiveLights: " << caps.MaxActiveLights << "
    ";
    : ssBuffer << "MaxUserClipPlanes: " << caps.MaxUserClipPlanes << "
    ";
    : ssBuffer << "MaxVertexIndex: " << caps.MaxVertexIndex << "
    ";
    : ssBuffer << "MaxVertexShaderConst: " << caps.MaxVertexShaderConst << "
    ";
    :
    : ssBuffer << "D3DPRESENT_INTERVAL_IMMEDIATE is ";
    : if(caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
    : {
    : ssBuffer << "supported.";
    : }
    : else
    : {
    : ssBuffer << "not supported.";
    : }
    : ssBuffer << "
    ";
    :
    : ssBuffer << "D3DPTADDRESSCAPS_CLAMP is ";
    : if(caps.TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
    : {
    : ssBuffer << "supported.";
    : }
    : else
    : {
    : ssBuffer << "not supported.";
    : }
    : ssBuffer << "
    ";
    :
    : ssBuffer << "D3DFVFCAPS_PSIZE is ";
    : if(caps.FVFCaps & D3DFVFCAPS_PSIZE)
    : {
    : ssBuffer << "supported.";
    : }
    : else
    : {
    : ssBuffer << "not supported.";
    : }
    : ssBuffer << "
    ";
    :
    : ssBuffer << "D3DCAPS2_CANAUTOGENMIPMAP is ";
    : if(caps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
    : {
    : ssBuffer << "supported.";
    : }
    : else
    : {
    : ssBuffer << "not supported.";
    : }
    : ssBuffer << "
    ";
    :
    : ssBuffer << "D3DPRASTERCAPS_DITHER is ";
    : if(caps.RasterCaps & D3DPRASTERCAPS_DITHER)
    : {
    : ssBuffer << "supported.";
    : }
    : else
    : {
    : ssBuffer << "not supported.";
    : }
    : ssBuffer << "
    ";
    :
    : int sizeOfStr = ssBuffer.str().size() + 1;
    : sprintf(tempStr, "%s", ssBuffer.str().c_str());
    : WriteFile((HANDLE) hFile, (char*) tempStr, (DWORD) (sizeOfStr), &bytesWritten, NULL);
    : ssBuffer.str("");
    : CloseHandle((HANDLE) hFile);
    : }
    : [/code]:
    :
    : And also I have one more question.
    : Why will this application stop running when the exit button is
    : pressed:
    : [code]:
    : #define D3D_DEBUG_INFO
    :
    : #include <windows.h>
    : #include
    : #include
    : #include
    : #include
    :
    : #include "header.h"
    :
    : LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    : bool initD3D(HWND hWnd);
    : void drawLines();
    : bool isDeviceLost();
    : void writeToDebug(std::stringstream* text);
    :
    : IDirect3D9* lpD3D;
    : IDirect3DDevice9* lpD3Ddevice;
    : D3DPRESENT_PARAMETERS d3d_pm;
    : HFILE hFile;
    : bool hitBreakPoint;
    :
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    : {
    : HWND hWnd;
    : WNDCLASS wndclass;
    : MSG msg;
    : msg.message = WM_NULL;
    :
    : wndclass.style = CS_VREDRAW | CS_HREDRAW;
    : wndclass.lpfnWndProc = WndProc;
    : wndclass.cbClsExtra = 0;
    : wndclass.cbWndExtra = 0;
    : wndclass.hInstance = hInstance;
    : wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    : wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    : wndclass.lpszMenuName = NULL;
    : wndclass.lpszClassName = "Windows Class";
    :
    : if(!(RegisterClass(&wndclass)))
    : {
    : MessageBox(NULL, "Error", "Error: Couldn't register window class.", MB_OK);
    : PostQuitMessage(0);
    : }
    :
    : RECT R = {0, 0, 800, 600};
    : AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
    :
    : if(!(hWnd = CreateWindow("Windows Class", "DX3D 9 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, R.right, R.bottom, NULL, NULL, hInstance, NULL)))
    : {
    : MessageBox(NULL, "Error", "Error: Couldn't create window.", MB_OK);
    : PostQuitMessage(0);
    : }
    :
    : ShowWindow(hWnd, nShowCmd);
    : UpdateWindow(hWnd);
    :
    : hFile = (HFILE) CreateFile("debug.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL,
    : CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
    :
    : if(initD3D(hWnd))
    : {
    : while(msg.message != WM_QUIT)
    : {
    : while(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
    : {
    : TranslateMessage(&msg);
    : DispatchMessage(&msg);
    : }
    : if(msg.message == WM_CLOSE)
    : {
    : MessageBox(NULL, "Debug", "Closing Window", MB_OK);
    : }
    : if(!isDeviceLost() && !hitBreakPoint)
    : {
    : drawLines();
    : }
    : }
    : lpD3Ddevice->Release();
    : lpD3D->Release();
    : }
    : CloseHandle((HANDLE) hFile);
    : return (int)msg.wParam;
    : }
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : {
    : switch(msg)
    : {
    : case WM_CLOSE:
    : DestroyWindow(hWnd);
    : hitBreakPoint = true;
    : break;
    : case WM_DESTROY:
    : PostQuitMessage(0);
    : hitBreakPoint = true;
    : break;
    : default:
    : return DefWindowProc(hWnd, msg, wParam, lParam);
    : }
    : return 0;
    : }
    :
    : bool initD3D(HWND hWnd)
    : {
    : lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
    : D3DCAPS9 caps;
    : DWORD behaviorFlags = NULL;
    : ZeroMemory(&d3d_pm, sizeof(d3d_pm));
    :
    : if( !lpD3D )
    : {
    : MessageBox(NULL, "Error", "Error getting interface to Direct3D.", MB_OK);
    : PostQuitMessage(0);
    : return false;
    : }
    :
    : if (FAILED(lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps)))
    : {
    : MessageBox(NULL, "Error", "Error getting capabilites of default graphics adapter.", MB_OK);
    : PostQuitMessage(0);
    : return false;
    : }
    :
    : if(caps.DevCaps & D3DDEVCAPS_PUREDEVICE)
    : behaviorFlags |= D3DCREATE_PUREDEVICE;
    : if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
    : behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
    : else
    : behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    :
    : RECT winRect;
    : GetClientRect(hWnd, &winRect);
    : d3d_pm.BackBufferWidth = winRect.right;
    : d3d_pm.BackBufferHeight = winRect.bottom;
    : d3d_pm.MultiSampleType = D3DMULTISAMPLE_NONE;
    : d3d_pm.hDeviceWindow = hWnd;
    : d3d_pm.Windowed = true;
    : d3d_pm.SwapEffect = D3DSWAPEFFECT_DISCARD;
    : if(SUCCEEDED(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, true)))
    : {
    : d3d_pm.BackBufferFormat = D3DFMT_X8R8G8B8;
    : }
    : else
    : {
    : d3d_pm.BackBufferFormat = D3DFMT_UNKNOWN;
    : }
    : if(caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
    : {
    : d3d_pm.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    : }
    : else
    : {
    : d3d_pm.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
    : }
    : d3d_pm.AutoDepthStencilFormat = D3DFMT_D24S8;
    : d3d_pm.EnableAutoDepthStencil = true;
    : d3d_pm.BackBufferCount = 1;
    : d3d_pm.Flags = 0;
    :
    : if(FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, behaviorFlags, &d3d_pm, &lpD3Ddevice)))
    : return false;
    :
    : return true;
    : }
    :
    : void drawLines()
    : {
    : LPD3DXLINE lpD3Dline;
    : D3DXVECTOR2 points[2];
    :
    : lpD3Ddevice->Clear(NULL, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);
    :
    : if(SUCCEEDED(D3DXCreateLine(lpD3Ddevice, &lpD3Dline)) && (SUCCEEDED(lpD3Ddevice->BeginScene())))
    : {
    : if(SUCCEEDED(lpD3Dline->Begin()))
    : {
    : points[0].x = 20.0f;
    : points[0].y = 10.0f;
    : points[1].x = 20.0f;
    : points[1].y = 500.0f;
    : lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));
    :
    : points[0].x = 20.0f;
    : points[0].y = 10.0f;
    : points[1].x = 350.0f;
    : points[1].y = 500.0f;
    : lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));
    :
    : points[0].x = 700.0f;
    : points[0].y = 10.0f;
    : points[1].x = 700.0f;
    : points[1].y = 500.0f;
    : lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));
    :
    : points[0].x = 700.0f;
    : points[0].y = 10.0f;
    : points[1].x = 350.0f;
    : points[1].y = 500.0f;
    : lpD3Dline->Draw(points, 2, D3DCOLOR_XRGB(0, 0, 255));
    : }
    : lpD3Ddevice->EndScene();
    : lpD3Ddevice->Present(NULL,NULL,NULL,NULL);
    : }
    : lpD3Dline->Release();
    : }
    :
    : void writeToDebug(std::stringstream* text)
    : {
    : char tempStr[100];
    : sprintf(tempStr, "%s", text->str().c_str());
    : DWORD bytesWritten;
    :
    : WriteFile((HANDLE) hFile, (char*) &tempStr, (DWORD) (text->str().size() + 1), &bytesWritten, NULL);
    : }
    :
    : bool isDeviceLost()
    : {
    : HRESULT hr = lpD3Ddevice->TestCooperativeLevel();
    :
    : if(SUCCEEDED(hr))
    : {
    : return false;
    : }
    : else if(hr == D3DERR_DEVICELOST)
    : {
    : return true;
    : }
    : else if(hr == D3DERR_DEVICENOTRESET)
    : {
    : lpD3Ddevice->Reset(&d3d_pm);
    : return true;
    : }
    : else
    : {
    : MessageBox(NULL, "Error", "Internal device error...quiting application", MB_OK);
    : PostQuitMessage(0);
    : return true;
    : }
    : }
    : [/code]:
    :
    : : : I have had problems with setting up a Win32 App that just pops up a
    : : : window with the simple minimize, exit, etc buttons in the upper
    : : : right. I have three problems:
    : : : 1. Getting the minimize, exit, etc buttons in the upper right
    : : : 2. Getting the program to not be so slow that it basically crashes
    : : : the system
    : : : 3. Getting the program to stop running when the exit button is
    : : : pressed
    : : :
    : : : So I have two questions:
    : : : Does anyone know what the bug, bottleneck, etc is in the below code:
    : : [color=Blue]See RED: (also, no idea, why you see it as "slow"... can
    : : you explain more?)[/color]
    : : : [code]: : :
    : : : #include
    : : :
    : : : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    : : :
    : : : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : : : {
    : : : WNDCLASSEX wc;
    : : : HWND hWnd;
    : : : MSG msg;
    : : :
    : : : wc.cbClsExtra = 0;
    : : : wc.cbWndExtra = 0;
    : : : wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    : : : wc.style = CS_HREDRAW | CS_VREDRAW;
    : : : wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    : : : wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : : : wc.hInstance = hInstance;
    : : : wc.lpszClassName = "Win32 Class";
    : : : wc.lpszMenuName = NULL;
    : : : wc.lpfnWndProc = WndProc;
    : : : wc.cbSize = sizeof(WNDCLASSEX);
    : : : wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    : : :
    : : : if(!RegisterClassEx(&wc))
    : : : {
    : : : MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
    : : : return 0;
    : : : }
    : : :
    : : : hWnd = CreateWindowEx([color=Red]WS_EX_APPDWINDOW[/color], "Win32 Class", "My Window", [color=Red]WS_OVERLAPPEDWINDOW[/color], 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
    : : : if(hWnd == NULL)
    : : : {
    : : : MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
    : : : return 0;
    : : : }
    : : :
    : : : ShowWindow(hWnd, nCmdShow);
    : : : UpdateWindow(hWnd);
    : : :
    : : : while(GetMessage(&msg, [color=Red]0[/color], 0, 0))
    : : [color=Red]
    : : : //
    : : : // Zero here ^^^ instead of hWnd, because when you post
    : : : // WM_QUIT message - it goes to the thread, not to the window.
    : : : //[/color]
    : : : {
    : : : TranslateMessage(&msg);
    : : : DispatchMessage(&msg);
    : : : }
    : : :
    : : : return msg.wParam;
    : : : }
    : : :
    : : : LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : : : {
    : : : switch(msg)
    : : : {
    : : : case WM_CLOSE:
    : : : DestroyWindow(hWnd);
    : : : break;
    : : : case WM_DESTROY:
    : : : PostQuitMessage(0);
    : : : break;
    : : : default:
    : : : return DefWindowProc(hWnd, msg, wParam, lParam);
    : : : }
    : : : return 0;
    : : : }
    : : : [/code]: : :
    : : :
    : : : Also, is there a window/class style that puts the exit button in the
    : : : upper right?
    : : :
    : : :
    : : : Thank you,
    : : : Mikfig
    : : :
    : :
    :
    :

Sign In or Register to comment.