2011-01-13 28 views
1

我創造了這個簡單的窗口與Visual Studio 2010:編譯與不同的IDE,但只有一個工作!

#include <stdio.h> 
#include <string.h> 
#include <windows.h> 
#include <tchar.h> 

#define MAX_LOADSTRING 100 
#define IDS_APP_TITLE   103 

#define IDR_MAINFRAME   128 
#define IDD_PROVIAMO_DIALOG 102 
#define IDD_ABOUTBOX   103 
#define IDM_ABOUT    104 
#define IDM_EXIT    105 
#define IDI_PROVIAMO   107 
#define IDI_SMALL    108 
#define IDC_PROVIAMO   109 
#define IDC_MYICON    2 
#ifndef IDC_STATIC 
#define IDC_STATIC    -1 
#endif 
// Next default values for new objects 
// 
#ifdef APSTUDIO_INVOKED 
#ifndef APSTUDIO_READONLY_SYMBOLS 

#define _APS_NO_MFC     130 
#define _APS_NEXT_RESOURCE_VALUE 129 
#define _APS_NEXT_COMMAND_VALUE  32771 
#define _APS_NEXT_CONTROL_VALUE  1000 
#define _APS_NEXT_SYMED_VALUE  110 
#endif 
#endif 


// Global Variables: 
HINSTANCE hInst;        // current instance 
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 

// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

int APIENTRY _tWinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPTSTR lpCmdLine, 
        int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 
    MSG msg; 
    HACCEL hAccelTable; 

    // Initialize global strings 
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_PROVIAMO, szWindowClass, MAX_LOADSTRING); 
    MyRegisterClass(hInstance); 

    // Perform application initialization: 
    if (!InitInstance (hInstance, nCmdShow)) 
    { 
     return FALSE; 
    } 

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROVIAMO)); 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 

    return (int) msg.wParam; 
} 



// 
// FUNCTION: MyRegisterClass() 
// 
// PURPOSE: Registers the window class. 
// 
// COMMENTS: 
// 
// This function and its usage are only necessary if you want this code 
// to be compatible with Win32 systems prior to the 'RegisterClassEx' 
// function that was added to Windows 95. It is important to call this function 
// so that the application will get 'well formed' small icons associated 
// with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = hInstance; 
    wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PROVIAMO)); 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PROVIAMO); 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 

    return RegisterClassEx(&wcex); 
} 

// 
// FUNCTION: InitInstance(HINSTANCE, int) 
// 
// PURPOSE: Saves instance handle and creates main window 
// 
// COMMENTS: 
// 
//  In this function, we save the instance handle in a global variable and 
//  create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 

    hInst = hInstance; // Store instance handle in our global variable 

    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 

    if (!hWnd) 
    { 
     return FALSE; 
    } 

    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    return TRUE; 
} 

// 
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 
// 
// PURPOSE: Processes messages for the main window. 
// 
// WM_COMMAND - process the application menu 
// WM_PAINT - Paint the main window 
// WM_DESTROY - post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 
    RECT rect ; 

    switch (message) 
    { 
    case WM_COMMAND: 
     wmId = LOWORD(wParam); 
     wmEvent = HIWORD(wParam); 
     // Parse the menu selections: 
     switch (wmId) 
     { 
     case IDM_ABOUT: 
      DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
      MessageBox (NULL,TEXT("Hello emy"), TEXT ("HelloMsg"), 0x00000006L | MB_ICONASTERISK); 
      break; 
     case IDM_EXIT: 
      DestroyWindow(hWnd); 
      break; 
     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
     break; 
    case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     // TODO: Add any drawing code here... 

     GetClientRect (hWnd, &rect) ; 

     DrawText (hdc, TEXT ("Ciao Emiliano :)"), -1, &rect, 
        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; 
     EndPaint(hWnd, &ps); 
     break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    UNREFERENCED_PARAMETER(lParam); 
    switch (message) 
    { 
    case WM_INITDIALOG: 
     return (INT_PTR)TRUE; 

    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
     { 
      EndDialog(hDlg, LOWORD(wParam)); 
      return (INT_PTR)TRUE; 
     } 
     break; 
    } 
    return (INT_PTR)FALSE; 
} 

如果我的Visual Studio編譯然後工作,但如果我代碼::塊編譯和執行編譯的文件,然後該窗口不會出現,但會出現一個命令提示符:

"Process Returned 0 (0x0) execution time 0,266s 
    Press any key to continue" 

爲什麼?

回答

2

在Windows上,console applicationwindows application應用程序類型之間存在差異。

嘗試在Code :: Block項目中指定應用程序類型爲windows application

+1

誰說你不能在*控制檯應用程序顯示一個窗口*? – SuperSaiyan 2011-01-13 17:21:30

0

這個簡單的測試項目中是否包含一個.rc資源文件?如果窗口類註冊引用了資源(例如字符串),而這些資源實際上並不存在於可執行文件中的任何位置,則可能會導致問題。

我打算猜測你的窗口創建在某處失敗了,可能是由於資源在編譯的應用程序中不可用。它看起來不像代碼對返回值進行徹底的錯誤檢查,所以你可能需要通過調試器來查看發生了什麼。

檢查資源文件是否假設與它一起去。將它包含到代碼塊中,以便它實際上被編譯並嘗試再次運行該應用程序。

編輯:在另一方面,幫助人類通過支持此proposal :)

相關問題