【问题标题】:create a GUi using window native API使用窗口原生 API 创建 GUI
【发布时间】:2010-10-23 03:30:59
【问题描述】:

我正在尝试创建一个仅使用本机 Windows api 的 GUI。 我使用win32控制台项目和win32窗口项目在vs 2008中创建项目,我使用了微软的示例代码如下

    #include <windows.h> 

// Global variable 

HINSTANCE hinst; 

// Function prototypes. 

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int); 
InitApplication(HINSTANCE); 
InitInstance(HINSTANCE, int); 
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); 

// Application entry point. 

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) 
{ 
    MSG msg; 

    if (!InitApplication(hinstance)) 
        return FALSE; 

    if (!InitInstance(hinstance, nCmdShow)) 
        return FALSE; 

    BOOL fGotMessage;
    while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1) 
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    } 
    return msg.wParam; 
        UNREFERENCED_PARAMETER(lpCmdLine); 
} 

BOOL InitApplication(HINSTANCE hinstance) 
{ 
    WNDCLASSEX wcx; 

    // Fill in the window class structure with parameters 
    // that describe the main window. 

    wcx.cbSize = sizeof(wcx);          // size of structure 
    wcx.style = CS_HREDRAW | 
        CS_VREDRAW;                    // redraw if size changes 
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure 
    wcx.cbClsExtra = 0;                // no extra class memory 
    wcx.cbWndExtra = 0;                // no extra window memory 
    wcx.hInstance = hinstance;         // handle to instance 
    wcx.hIcon = LoadIcon(NULL, 
        IDI_APPLICATION);              // predefined app. icon 
    wcx.hCursor = LoadCursor(NULL, 
        IDC_ARROW);                    // predefined arrow 
    wcx.hbrBackground = GetStockObject( 
        WHITE_BRUSH);                  // white background brush 
    wcx.lpszMenuName =  "MainMenu";    // name of menu resource 
    wcx.lpszClassName = "MainWClass";  // name of window class 
    wcx.hIconSm = LoadImage(hinstance, // small class icon 
        MAKEINTRESOURCE(5),
        IMAGE_ICON, 
        GetSystemMetrics(SM_CXSMICON), 
        GetSystemMetrics(SM_CYSMICON), 
        LR_DEFAULTCOLOR); 

    // Register the window class. 

    return RegisterClassEx(&wcx); 
} 

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow) 
{ 
    HWND hwnd; 

    // Save the application-instance handle. 

    hinst = hinstance; 

    // Create the main window. 

    hwnd = CreateWindow( 
        "MainWClass",        // name of window class 
        "Sample",            // title-bar string 
        WS_OVERLAPPEDWINDOW, // top-level window 
        CW_USEDEFAULT,       // default horizontal position 
        CW_USEDEFAULT,       // default vertical position 
        CW_USEDEFAULT,       // default width 
        CW_USEDEFAULT,       // default height 
        (HWND) NULL,         // no owner window 
        (HMENU) NULL,        // use class menu 
        hinstance,           // handle to application instance 
        (LPVOID) NULL);      // no window-creation data 

    if (!hwnd) 
        return FALSE; 

    // Show the window and send a WM_PAINT message to the window 
    // procedure. 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 
    return TRUE; 

} 

然后我得到了错误:

1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(56) : error C2440: '=' : cannot convert from 'HGDIOBJ' to 'HBRUSH'
1>        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(57) : error C2440: '=' : cannot convert from 'const char [9]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(58) : error C2440: '=' : cannot convert from 'const char [11]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(64) : error C2440: '=' : cannot convert from 'HANDLE' to 'HICON'
1>        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\users\helloworld\documents\visual studio 2008\projects\motionwindow2\motionwindow2\main.cpp(92) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\Users\HelloWorld\Documents\Visual Studio 2008\Projects\motionwindow2\motionwindow2\Debug\BuildLog.htm"
1>motionwindow2 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

有谁知道我做错了什么?我根本没有修改代码,它是一个空项目。

谢谢

【问题讨论】:

  • 已提交文档错误报告。

标签: c++ windows user-interface native


【解决方案1】:

你有几个问题:

【讨论】:

  • @Merlyn:您对这三个问题中的每一个都有正确的反应,即使不是历史解释。好吧,我可以对你其他一些写得很好的答案进行投票,给你应得的代表。
  • 如果您愿意,谢谢。不过没必要。我删除了我的答案,因为指导比纠正更好,而您已经在这里做到了。
【解决方案2】:

第一个错误是指第 10 行。

因此,请看第 10 行:

InitApplication(HINSTANCE);

声明缺少函数结果类型。

等等。

干杯,

【讨论】:

  • 但这是来自 MSDN 库 它应该可以工作,对吧?msdn.microsoft.com/en-us/library/ms633575(v=VS.85).aspx
  • @Grey:它可能在某个时间点编译过,其中默认的 int 说明符(在某些地方 C/C++ 假设 int 如果您不指定类型)完全相同作为 BOOL 的东西。但是时代变了,微软还没有更新他们的(糟糕的)示例代码。顺便说一句,不要使用 default-int 行为。几乎没有人再使用它了。
  • @Merlyn:传统 Win32 API 的示例都是用 C 编写的。默认类型是笼统的短语“C/C++”无用的领域之一,因为 C++ 的行为非常不同在这方面来自 C。
  • @Ben:为了支持您的评论,我刚刚意识到编译器说“注意:C++ 不支持默认整数”。我想这很好,因为我从来没有用过它:)
猜你喜欢
  • 1970-01-01
  • 2021-02-03
  • 2010-11-11
  • 2012-09-11
  • 2012-08-21
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多