-2

我需要用 C 编写一些程序,但我无法让窗口工作。它提出了大约 30 个错误,主要是说;当有一个,没有存储类或类型说明符,以及预期的声明时,应该是预期的,不确定这些是什么意思。我看过两个教程,它们看起来都非常相似,而我的看起来一样,所以不确定这些缺失的东西是什么。

这是我的代码

#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;
}
4

2 回答 2

3

我看到的第一个问题在这里:

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

{
WNDCLASS wcls;

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

去掉它。

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

于 2012-05-31T16:15:48.213 回答
2

那里有很多错别字。

  1. WinMain 后的分号

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

  3. LPWCSTR 参数

  4. 带有 nCmdShow 的 ShowWindow() 不...显示

  5. WS_OVERPLAPPEDWINDOW(不是 WS_OVERLAPPINGWINDOW)

  6. LoadCursor 的 LoadCursor 插入

现在应该可以工作了。下次仔细打字

#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;
}
于 2012-05-31T16:19:15.940 回答