C程序中的Windows程序不起作用

时间:2012-05-31 16:12:26

标签: c windows

我需要在C中制作一些程序,但我无法让窗口工作。它提出了大约30个错误,主要是说;当有一个,没有存储类或类型说明符,并且预期声明,不确定这些意味着什么时,预计会发生。我看了两个turtorials,它们看起来非常相似,我看起来一样,所以不确定这些丢失的东西是什么。

继承我的代码

#include <windows.h>

LRESULT CALLBACK WindowFunc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

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

{
WNDCLASS wcls;
HWND hwnd;
MSG msg;

LPCWSTR szClassName = L"ThreadsProgram";
LPCWSTR szWinName = L"My Threads Program"

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

      if(!RegisterClass(&wcls))
    {
        MessageBox(NULL, "Window Registration Failed!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

//Make Window 
hwnd = CreateWindow(szClassName,
szWinName,
WS_OVERLAPPINGWINDOW,
100,
100,
400,
400,
HWND_DESKTOP,
NULL,
hThisInst,
NULL);

//Show Window

if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Failed!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);

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

2 个答案:

答案 0 :(得分:3)

我看到的第一个问题是:

int WINAPI WinMain(HINSTANCE hThisInst, 
                     HINSTANCE hPrevInst, 
                     LPSTR lpszArgs, 
                     int nWinMode);      /* <---- This semi-colon causes grief! */

{
WNDCLASS wcls;

由于int nWinMode);之后的分号,您有一个函数声明。

删除它。

可能还有其他问题;我没有进一步看,也不打算这样做。如果您自己的代码审查无效,编译器将指导您。

答案 1 :(得分:2)

那里有很多错别字。

  1. WinMain后的分号

  2. MessageBox()函数占用3而不是4个参数。

  3. LPWCSTR params

  4. 使用nCmdShow的ShowWindow()没有...显示

  5. WS_OVERPLAPPEDWINDOW(不是WS_OVERLAPPINGWINDOW)

  6. LoadCurser的LoadCursor实例

  7. 现在应该工作。下次仔细输入

    #include <windows.h>
    
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
         switch(msg)
         {
             case WM_CLOSE: DestroyWindow(hwnd); break;
             case WM_DESTROY: PostQuitMessage(0); break;
             default: return DefWindowProc(hwnd, msg, wParam, lParam);
         }
         return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hThisInst,  HINSTANCE hPrevInst,  LPSTR lpszArgs,  int nWinMode)
    {
        WNDCLASS wcls;
        HWND hwnd;
        MSG msg;
    
        LPCSTR szClassName = "ThreadsProgram";
        LPCSTR szWinName = "My Threads Program";
    
        //Register Class
        wcls.style         =0; 
        wcls.lpfnWndProc   =WindowFunc;
        wcls.cbClsExtra    =0;
        wcls.cbWndExtra    =0;
        wcls.hInstance     =hThisInst;
        wcls.hIcon         =LoadIcon(NULL, IDI_APPLICATION);
        wcls.hCursor       =LoadCursor(NULL, IDC_ARROW);
        wcls.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
        wcls.lpszMenuName  =NULL;
        wcls.lpszClassName =szClassName;
    
         if(!RegisterClassA(&wcls))
        {
            MessageBoxA(NULL, 0, "Window Registration Failed!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        //Make Window 
        hwnd = CreateWindowA(szClassName, szWinName,
           WS_OVERLAPPEDWINDOW,
           100, 100, 400, 400,
           HWND_DESKTOP,
           NULL, hThisInst, NULL);
    
        //Show Window
    
        if(hwnd == NULL)
        {
            MessageBoxA(NULL, 0, "Window Failed!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, SW_SHOW/*nWinMode*/);
        UpdateWindow(hwnd);
    
        //Main Message Loop
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int)msg.wParam;
    }