2012-08-15 148 views
1

我試圖在C編寫的Windows應用程序中顯示隨機數,我的程序編譯但沒有顯示在窗口中。我使用Visual Studio 2010,有人提到微軟編譯器不能識別我的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 

任何幫助將是偉大的。 謝謝

+0

你希望你的輸出出現在什麼窗口? 'OutputDebugString()'輸出到調試輸出,這通常只在諸如Visual Studio的開發環境或類似DebugView的調試工具(SysInternals)中可見。 – tinman 2012-08-15 20:20:49

+0

printf也不工作,我被告知OutputDebugString是一個窗口應用程序。 – 2012-08-15 20:24:12

+2

@GennySaxo如果您在Visual Studio中以調試模式運行OutputDebugString,它將顯示在Visual Studio內的輸出窗口中,它不會顯示在您自己的程序中的任何位置。 – nos 2012-08-15 20:27:26

回答

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控件here

如果你打算做更多的UI工作,那麼你可能想看看使用GUI框架。他們傾向於去掉UI編程的許多枯燥的本質,但也有自己的學習曲線。但是,如果你僅僅學習一些看起來很簡單的東西,那麼你的時間會比學習低級別的Windows UI編程更好。