Sign in to follow this  
squigglesoft

Windows Application

Recommended Posts

Hi, Please note: i only just started C++ and DX: :) I just started readng my book, "Teach yourself Game Programming in 21 Days with DirectX". It starts off with creating a simple window. So i went and downloaded MS VC++ EE and installed it. Then, I got the PSDK that i needed for it. I then did the following (there may be a problem here, please tell me if you see something wrong): project properties -> C/C++ -> General -> Additional Include Directories. In there, i linked to "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include", the default location of the Include files. next: project properties -> Linker -> General -> Additional Include Directories. Ine there, i linked to "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib", the default location of the libs. Then i copied and pasted the code given in the book into my C++ file in that project, and lo and behold, there were errors. I am thinking they maybe compatability errors? the book uses "MS's latest version of C++, Visual C++ .NET", an older version of what i have, i believe? I managed to tweak the code so that it would run, window title bar and the class name are GIBBERISH. You see, i had to modify these to get the damn thing to atleast run. In the book, they are defined:
void CreateAppWindow(HINSTANCE hInstance)
{
    g_hWnd = CreateWindowEx(
        NULL,
        "WinApp", /////////////////////////////this line
        "Basic Windows Application", ////////////////////this line
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        648,
        514,
        GetDesktopWindow(),
        NULL,
        hInstance,
        NULL);
}
and also in this funtion:
void RegisterWindowClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "WinApp"; //////////this line
    wc.hIconSm = NULL;

    RegisterClassEx(&wc);
}
What I did to to make it work was:
        LPCWSTR("WinApp"),
        LPCWSTR("Basic Windows Application"), 
and
wc.lpszClassName = LPCWSTR("WinApp");
I am doing something wrong, aren't I? Here is the full code (with no tweaks):
//////////////////////////////////////////////////////
// BasicWindowsApp.cpp
//////////////////////////////////////////////////////

#include <windows.h>

// Function prototypes.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
    WPARAM wParam, LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();

// Global variables.
HWND g_hWnd;


//////////////////////////////////////////////////////
// WinMain()
//////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
    RegisterWindowClass(hInstance);
    CreateAppWindow(hInstance);
    ShowWindow(g_hWnd, SW_SHOWDEFAULT);
    UpdateWindow(g_hWnd);
    int result = StartMessageLoop();
    return result;
}

//////////////////////////////////////////////////////
// WndProc()
//////////////////////////////////////////////////////
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        return 0;

    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    case WM_PAINT:
        ValidateRect(g_hWnd, NULL);
        return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

//////////////////////////////////////////////////////
// RegisterWindowClass()
//////////////////////////////////////////////////////
void RegisterWindowClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = LPCWSTR("WinApp");
    wc.hIconSm = NULL;

    RegisterClassEx(&wc);
}

//////////////////////////////////////////////////////
// CreateAppWindow()
//////////////////////////////////////////////////////
void CreateAppWindow(HINSTANCE hInstance)
{
    g_hWnd = CreateWindowEx(
        NULL,
        LPCWSTR("WinApp"),
        LPCWSTR("Basic Windows Application"), 
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        648,
        514,
        GetDesktopWindow(),
        NULL,
        hInstance,
        NULL);
}

//////////////////////////////////////////////////////
// StartMessageLoop()
//////////////////////////////////////////////////////
WPARAM StartMessageLoop()
{
    MSG msg;
    while(1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            // Use idle time here.
        }
    }
    return msg.wParam;
}
Does this work for anyone? Anyone gonna fix it for meh :D :p Cheers, ss

Share this post


Link to post
Share on other sites
Quote:
Original post by squigglesoft
The book uses "MS's latest version of C++, Visual C++ .NET", an older version of what i have, i believe?


That's correct.

Quote:
LPCWSTR("WinApp")


"WinApp" is a narrow (ANSI, 8-bit) string. Visual C++ 2005 defaults to unicode mode, in which CreateWindow and friends are aliased to CreateWindowW, and hence take wide strings as parameters.

What you have done is, rather than passing in a wide string, simply given the function a pointer to a narrow string and said "treat this as though it points to a wide string".

The correct solution is to pass a wide string directly. A wide string literal is created using the following syntax:
L"WinApp"
Note that with this fixed, your cast to LPCWSTR is no longer necessary.

Share this post


Link to post
Share on other sites
The other solution is to go to Project Settings (Alt+F7) -> Configuration properties -> General, and set "Character Set" to "Use Multi-Byte Character Set".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this