Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6849585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:00:35+00:00 2026-05-27T01:00:35+00:00

I was working through a book which shows me how to create a windows

  • 0

I was working through a book which shows me how to create a windows application. I have written the code, however when I compile and run it, it says that it was successfuly built but it doesn’t show a window where it should write “Hello World”. I am using Visual Studio 2010 with C++, what might be the problem?

Thanks

Here is the code;

//Header Files
#include <windows.h>
#include <stdlib.h>
#include <time.h>

//Application Title
#define APPTITLE L"Hello World"

//function prototypes (forward declarations)
BOOL InitInstance( HINSTANCE, int);
ATOM MyRegisterClass( HINSTANCE);
LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM);

//The window event callback function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    //char *szHello = "Hello World!";
    RECT rt;
    int x, y, n;
    COLORREF c;

    switch(message)
    {
        case WM_PAINT:
            //get the dimensions of the window
            GetClientRect( hWnd, &rt);

            //Start drawing on device context
            hdc = BeginPaint( hWnd, &ps);

            //Draw some text
            DrawText( hdc, L"Hello World!", strlen( "Hello World!"), &rt, DT_CENTER);

            //Draw 1000 random pixels
            for( n=0; n < 3000; n++)
            {
                x = rand() % (rt.right - rt.left);
                y = rand() % (rt.bottom - rt.top);
                c = RGB( rand()%256, rand()%256, rand()%256);
                SetPixel( hdc, x, y, c);
            }

            //Stop drawing
            EndPaint( hWnd, &ps);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);

}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof( WNDCLASSEX);

    //FILL THE STRUCT WITH INGO
    wc.cbSize = CS_HREDRAW |  CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);

}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    //create a new window
    hWnd = CreateWindow(
        APPTITLE,               //Window class
        APPTITLE,               //title bar
        WS_OVERLAPPEDWINDOW,    //window Style
        CW_USEDEFAULT,          //x position of window
        CW_USEDEFAULT,          //y postion of window
        500,                    //width of the window
        400,                    //height of the window
        NULL,                   //parent window
        NULL,                   //menu
        hInstance,              //application instance
        NULL);                  //window parameters

    //was there an error creating the window?
    if( !hWnd)
        return FALSE;

    //Display the window
    ShowWindow( hWnd, nCmdShow);
    UpdateWindow( hWnd);

    return TRUE;

}

//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //declare varuables
    MSG msg;

    //register the class
    MyRegisterClass( hInstance);

    //Initialize application
    if( !InitInstance(hInstance, nCmdShow))
        return FALSE;

    //set random number seed
    srand(time(NULL));

    //Main message loop
    while( GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage( &msg);
        DispatchMessage( &msg);
    }


    return msg.wParam;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:00:36+00:00Added an answer on May 27, 2026 at 1:00 am

    This line is wrong:

    wc.cbSize = CS_HREDRAW |  CS_VREDRAW;
    

    You mean

    wc.style = CS_HREDRAW |  CS_VREDRAW;
    

    In fact I would change the window class initialisation code so that you make it very clear in the code that the entire struct is initialised.

    WNDCLASSEX wc = { 0 };//initialise struct to 0
    wc.cbSize = sizeof( WNDCLASSEX);
    //FILL THE STRUCT WITH INGO
    wc.style =  CS_HREDRAW |  CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName = APPTITLE;
    

    And you were missing some error checking:

    if (!MyRegisterClass( hInstance))
        return FALSE;
    

    Stepping through under the debugger will allow you to see where in the process things are going wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working through some exercises in a c++ book, which uses text based
I'm working through the book Pro WF Windows Workflow in .NET 3.5 and on
I am working through the examples in a Django book that I have, but
I'm working through the Grails In Action book and have noticed 2 instances that
I am working through a book which gives examples of Ranges being converted to
I'm currently working my way through the book Teach Yourself Android Application Development in
I am working through the Microsoft .Net Framework--Application Development Foundation Training Kit book Chapter
I'm working through a MVC book which uses the older version of Html.RenderAction .
I am working my way through Learn Cocoa on the Mac which was written
Ok, I am working through a book and trying to learn C++ operator overloading.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.