1

我试图在用 C 编写的 Windows 应用程序中显示随机数,我的程序编译但窗口中没有任何显示。我使用 Visual Studio 2010,有人提到 Microsoft 编译器无法识别我的 for 循环?我不确定您需要多少代码,所以我全部添加了。

#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

任何帮助都会很棒。谢谢

4

1 回答 1

0

在窗口中显示一些数字的最简单方法是使用可以显示文本的控件。我已修改您的代码以创建多行编辑窗口。循环现在创建一个字符串并将其for附加到编辑控件(通过选择文本的结束位置并替换它)。

#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

但是,这样做存在许多问题:

  • 文本限制为 65535
  • 文本周围的边框很难看,您可能需要查看 CreateWindowEx() 以获取更多边框选项
  • 添加行时文本不会自动向下滚动
  • 使用系统默认字体而不是更典型的 GUI/对话框字体
  • 不使用视觉样式(但这取决于您的清单和链接器设置,而不是代码)

您可以在此处阅读有关标准 Windows 控件的更多信息。

如果您打算做更多的 UI 工作,那么您可能想要考虑使用 GUI 框架。他们倾向于摆脱 UI 编程的许多乏味本质,但也有自己的学习曲线。但是你的时间最好花在学习其中之一,而不是低级 Windows UI 编程,当你只尝试做一些看似简单的事情时,这可能会非常迟钝。

于 2012-08-15T21:09:22.873 回答