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 8391807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:21:27+00:00 2026-06-09T19:21:27+00:00

Im trying to display random numbers in a windows application written in C, my

  • 0

Im trying to display random numbers in a windows application written in C, my program compiles but nothing displays in the window. Im using Visual Studio 2010, someone mentioned that the Microsoft compiler doesnt recognise my for loop?
Im not sure how much code you need so i added it all.

#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX_BUFF_SIZE 1024
#define IDM_FILE_RUN 40001
#define IDM_APP_EXIT 40002

//Window Function
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
               LPSTR lpszArgs, int nWinMode)
{

WNDCLASS wcls;
HWND hwnd;
MSG msg;

// Name of window and window class
LPCWSTR szWinName   = L"Threads Program";
LPCWSTR szClassName = L"ThreadsProgram";


wcls.hInstance = hThisInst;
wcls.lpszClassName = szClassName;
wcls.lpfnWndProc = WindowFunc;
wcls.style = 0;
wcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wcls.lpszMenuName = NULL;
wcls.cbClsExtra = 0;
wcls.cbWndExtra = 0;
wcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

// Register windows class
if(!RegisterClass(&wcls))
{
    return 0;
}

// Create main window
hwnd = CreateWindow(szClassName,
    szWinName,
    WS_OVERLAPPEDWINDOW,
    100,
    100,
    400,
    400,
    HWND_DESKTOP,
    NULL,
    hThisInst,
    NULL );

// Show main window
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);

// Message loop
while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int)msg.wParam;
}

void MyOutputDebugString(const char *str, ...)
{
char buf[4096];
va_list ptr;
va_start(ptr,str);
vsprintf(buf,str,ptr);
OutputDebugStringA(buf); 
}


LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message, 
                        WPARAM wParam, LPARAM lParam)


{
static char textBuffer[MAX_BUFF_SIZE];
static int nRead;




switch(message)    
{
case WM_CREATE:
    {
        HMENU hMenu;
        HMENU hMenuPopup;

        // create menus
        hMenu = CreateMenu();
        hMenuPopup = CreateMenu();

        // populate menus
        AppendMenu(hMenuPopup, MF_STRING,  IDM_FILE_RUN,   L"&Choose Balls");   
        AppendMenu(hMenuPopup, MF_STRING,  IDM_APP_EXIT,   L"&Exit");  
        AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup,   L"&File");

        // attach menus to main window
        SetMenu(hMainWindow, hMenu);
    }
    break;
case WM_COMMAND:
    {
        // Obey command
        switch(LOWORD(wParam))
        {
        case IDM_FILE_RUN:

            {
                int i;
                srand (time(NULL));
                for (i = 0; i < 6; i++)
                MyOutputDebugString ("%i\n", (rand ()% 49) +     1);




    return 0;

            }
            break;
        case IDM_APP_EXIT:
            SendMessage(hMainWindow, WM_CLOSE, 0, 0);
            break;
        }
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hMainWindow, message, wParam, lParam);
}
return 0;


}// Window function

Any help would be great.
Thanks

  • 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-06-09T19:21:29+00:00Added an answer on June 9, 2026 at 7:21 pm

    The easiest way to display some numbers in a window is to use a control that can display text. I’ve modified your code to create a multiline edit window. The for loop now creates a string and appends it to the edit control (by selecting the end position of the text and replacing it).

    #include <windows.h>
    #include <Windowsx.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define MAX_BUFF_SIZE 1024
    #define IDM_FILE_RUN 40001
    #define IDM_APP_EXIT 40002
    #define IDC_OUTPUT   40003
    
    //Window Function
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                   LPSTR lpszArgs, int nWinMode)
    {
    
    WNDCLASS wcls;
    HWND hwnd;
    MSG msg;
    
    // Name of window and window class
    LPCWSTR szWinName   = L"Threads Program";
    LPCWSTR szClassName = L"ThreadsProgram";
    
    
    wcls.hInstance = hThisInst;
    wcls.lpszClassName = szClassName;
    wcls.lpfnWndProc = WindowFunc;
    wcls.style = 0;
    wcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcls.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcls.lpszMenuName = NULL;
    wcls.cbClsExtra = 0;
    wcls.cbWndExtra = 0;
    wcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
    // Register windows class
    if(!RegisterClass(&wcls))
    {
        return 0;
    }
    
    // Create main window
    hwnd = CreateWindow(szClassName,
        szWinName,
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        400,
        400,
        HWND_DESKTOP,
        NULL,
        hThisInst,
        NULL );
    
        HANDLE  hEdit = CreateWindow(L"EDIT", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE, 10, 10, 300, 300, hwnd, (HMENU)IDC_OUTPUT, hThisInst, NULL);
    
    
    // Show main window
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);
    
    // Message loop
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
    }
    
    void MyOutputDebugString(const char *str, ...)
    {
    char buf[4096];
    va_list ptr;
    va_start(ptr,str);
    vsprintf(buf,str,ptr);
    OutputDebugStringA(buf);
    }
    
    
    LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message, 
                            WPARAM wParam, LPARAM lParam)
    
    
    {
    static char textBuffer[MAX_BUFF_SIZE];
    static int nRead;
    
    
    
    
    switch(message)    
    {
    case WM_CREATE:
        {
            HMENU hMenu;
            HMENU hMenuPopup;
    
            // create menus
            hMenu = CreateMenu();
            hMenuPopup = CreateMenu();
    
            // populate menus
            AppendMenu(hMenuPopup, MF_STRING,  IDM_FILE_RUN,   L"&Choose Balls");   
            AppendMenu(hMenuPopup, MF_STRING,  IDM_APP_EXIT,   L"&Exit");  
            AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup,   L"&File");
    
            // attach menus to main window
            SetMenu(hMainWindow, hMenu);
        }
        break;
    case WM_COMMAND:
        {
            // Obey command
            switch(LOWORD(wParam))
            {
            case IDM_FILE_RUN:
    
                {
                    int i;
                    srand (time(NULL));
                    for (i = 0; i < 6; i++)
                    {
                        int number = (rand ()% 49) +     1;
                        MyOutputDebugString ("%i\r\n", number);
                        wchar_t buffer[8];
                        wsprintf(buffer, L"%i\r\n", number);
    
                        HWND    h = GetDlgItem(hMainWindow, IDC_OUTPUT);
                        int text_len = Edit_GetTextLength(h);
                        Edit_SetSel(h, text_len, text_len);
                        Edit_ReplaceSel(h, buffer);
                    }
    
    
    
    
        return 0;
    
                }
                break;
            case IDM_APP_EXIT:
                SendMessage(hMainWindow, WM_CLOSE, 0, 0);
                break;
            }
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hMainWindow, message, wParam, lParam);
    }
    return 0;
    
    
    }// Window function
    

    There are a number of problems with this though:

    • Text is limited to 65535
    • Border around text is ugly, you might want to look into CreateWindowEx() for more border options
    • Text does not automatically scroll down when lines are added
    • Uses system default font instead of a more typical GUI/dialog font
    • Doesn’t use visual styles (but that would depend on your manifest and linker settings, not the code)

    You can read more about the standard Windows controls here.

    If you are going to do more UI work then you might want to look at using a GUI framework. They tend to take out a lot of the tedious nature of UI programming, but have their own learning curve too. But your time would be better spent learning one of those than the low level Windows UI programming, which can be quite obtuse when you are only trying to do something seemingly simple.

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

Sidebar

Related Questions

I'm trying to generate an arraylist of random numbers and display it. I'm not
I'm trying to display some data points using google graphs, but unfortunately there is
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
I'm trying to add random numbers onto the end of this array when ever
I'm trying to display the position(index) numbers of largest values in an array. It's
I am trying to display random banner each time the user refresh the page.
I'm trying to create a program for homework that displays a Tic-Tac-Toe board, and
I'm using Python and Flask to display a randomized game board, and trying to
I'm trying to make a simple javascript application to pick a random number between
I have a WPF application built with MVVM and am trying to display a

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.