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
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
forloop now creates a string and appends it to the edit control (by selecting the end position of the text and replacing it).There are a number of problems with this though:
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.