【问题标题】:Blank windows application not displaying?空白窗口应用程序不显示?
【发布时间】:2013-04-04 18:30:52
【问题描述】:

当我输入这个时,我的计算机上没有显示空白窗口:

#define WIN32_LEAN_AND_MEAN

#include<Windows.h>

bool InitMainWindow(HINSTANCE, int);
LRESULT CALLBACK MsgProc(HWND, UINT, WPARAM, LPARAM);
const int WIDTH=800;
const int HEIGHT=600;

HWND hwnd=NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline,       int mCmdShow)
{
    if(!InitMainWindow(hInstance,mCmdShow))
        return 1;
    MSG msg={0};

    while(WM_QUIT!=msg.message)
    {
        if(PeekMessage(&msg,0,0,0,PM_REMOVE))

        {  
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return static_cast<int>(msg.wParam);

}

bool InitMainWindow( HINSTANCE hInstance, int mCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize=sizeof(wcex);
    wcex.style=CS_HREDRAW|CS_VREDRAW;
    wcex.cbClsExtra=0;
    wcex.cbWndExtra=0;
    wcex.lpfnWndProc=MsgProc;
    wcex.hInstance=hInstance;
    wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
    wcex.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszClassName = (LPCWSTR)"tutorialClass";

    wcex.lpszMenuName=NULL;
    wcex.hIconSm=LoadIcon(NULL,IDI_WINLOGO);

    if(!RegisterClassEx(&wcex))
        return false;

    hwnd=CreateWindow((LPCWSTR)"tutorial class",
            (LPCWSTR)"tutorial window",
            WS_OVERLAPPED | WS_SYSMENU |WS_CAPTION,
            GetSystemMetrics(SM_CXSCREEN)/2-WIDTH/2,
            GetSystemMetrics(SM_CYSCREEN)/2-HEIGHT/2,
            WIDTH,
            HEIGHT,
            (HWND)NULL,
            NULL,
            hInstance,
            (LPVOID*)NULL);

    if(!hwnd)
        return false;

    ShowWindow(hwnd, mCmdShow);

    return true;

}

LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CHAR:
            switch(wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                    return 0;
            }
            return 0;
    }
    return DefWindowProc(hwnd, msg ,wParam,lParam);
}

Visual Studio 2010 Express Edition 中,我收到以下错误和警告:

'WindowBlank.exe': Loaded 'C:\Users\ramapriya\Documents\Visual Studio 2010\Projects\WindowBlank\Debug\WindowBlank.exe', Symbols loaded.
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\ProgramData\Browser Manager\2.6.1125.80\{16cdff19-861d-48e3-a751-d99a27784753}\browsemngr.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\imagehlp.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\psapi.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
The program '[7792] WindowBlank.exe: Native' has exited with code 1 (0x1).

有时在我的电脑上编译需要很长时间,因为它很慢。我不明白代码中有什么问题。我刚开始学习,我尝试更改参数、顺序、重新输入所有内容、创建一个新项目,但似乎没有出现一个空白窗口。这是为什么呢?

我不知道这段代码有什么问题。或者可能缺少一些图书馆?我有 windows 7 64 位家庭版,也安装了 Directx 11 SDK。

【问题讨论】:

  • 尝试单步调试调试器中的代码,看看是否有任何函数调用失败。如果有,请检查错误代码。 (此外,将 char* 转换为 LPCWSTR 往往不太顺利。)
  • 如果我在字符前不给出 (LPCWSTR) 则会出错。这就是我添加它的原因。还有什么我可以做的吗?没有它它不会编译。
  • 编译器告诉您您传递的字符串类型错误。投射并不能解决这个问题。
  • This question 讨论选角问题。
  • 仔细查看CreateWindow的返回值。检查错误代码。该错误代码会告诉您错字在哪里。

标签: c winapi window


【解决方案1】:

您也可以使用LPCTSTR 来更安全,例如:

wcex.lpszClassName = (LPCTSTR)_T("tutorialClass");

在调用CreateWindow() 函数时也做同样的事情。

另外,只需从 Visual Studio 的 Build 菜单中选择 Rebuild Solution 一次即可。

或者先选择清洁解决方案,然后选择构建解决方案,看看这是否能解决您的问题。

您可以做的另一件事是删除 .ncb、.sdf、.opt、.aps 文件(如果它们在您的解决方案目录中),然后再次重建解决方案并检查。

【讨论】:

  • 演员阵容在这里毫无意义。如果您使用_TTEXT 宏来声明字符串文字,那么您已经拥有了正确的字符串类型。铸造只是隐藏错误;没有什么比这更“安全”了。
【解决方案2】:

我知道你的班级名称在wcex.lpszClassName = (LPCWSTR)"tutorialClass" 中不同;和hwnd=CreateWindow((LPCWSTR)“教程班”,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多