无法创建窗口

时间:2012-09-18 10:27:34

标签: c++ winapi

#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>

#pragma comment(linker,"\"/manifestdependency:type='win32'name='Microsoft.Windows.Common-Controls'\
                version='6.0.0.0' processorArchitecture='*'\
                publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM lParam,LPARAM wParam)
{
    return DefWindowProc(hWnd,msg,wParam,lParam);
}

int WinMain(HINSTANCE hInst,HINSTANCE hpInst,LPSTR cmdLine,int cmdshow)
{
    static TCHAR classname[]=_T("SCONF");
    static TCHAR wTitle[]=_T("Server Configurator");

    WNDCLASSEX wClass;
    wClass.cbClsExtra=NULL;
    wClass.cbSize=sizeof(WNDCLASSEX);
    wClass.cbWndExtra=NULL;
    wClass.hbrBackground=(HBRUSH)(CTLCOLOR_SCROLLBAR);
    wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon=LoadIcon(hInst,IDI_APPLICATION);
    wClass.hIconSm=LoadIcon(hInst,IDI_APPLICATION);
    wClass.hInstance=hInst;
    wClass.lpfnWndProc=WndProc;
    wClass.lpszClassName=classname;
    wClass.lpszMenuName=NULL;
    wClass.style=CS_HREDRAW|CS_VREDRAW;
    if(!RegisterClassEx(&wClass))
    {
        MessageBox(NULL,L"Could not register class!", L"ERROR",MB_ICONERROR|MB_OK);
    }
    RECT wr={0,0,200,250};
    AdjustWindowRect(&wr,WS_CAPTION,false);

    HWND hWnd=CreateWindow(
        classname,
        wTitle,
        WS_CAPTION,
        GetSystemMetrics(SM_CXSCREEN)/2-100,
        GetSystemMetrics(SM_CYSCREEN)/2-125,
        wr.right-wr.left,//width
        wr.bottom-wr.top,//height
        NULL,
        NULL,
        hInst,
        NULL);

    if(!hWnd)
    {
        MessageBox(NULL,_T("Could not create window!"),_T("ERROR!"),MB_ICONERROR);
        return 1;
    }
    ShowWindow(hWnd,cmdshow);
    MSG msg;
    while(GetMessage(&msg,0,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

这是完整的代码,当我尝试编译时,hWnd总是返回false ...我甚至尝试从工作程序中复制代码并且它不起作用......这里发生了什么?它总是起作用......

1 个答案:

答案 0 :(得分:3)

你在WndProc函数中扭曲/越过你的wParam和lParam -

LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM lParam,LPARAM wParam) 
{
    return DefWindowProc(hWnd,msg,wParam,lParam);
}

LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM lParam,LPARAM wParam) {
    return DefWindowProc(hWnd,msg,wParam,lParam);
}

我认为这没有帮助。

LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM lParam,LPARAM wParam) //check your parameter names!!!!!!!!!!!!!!!!!!!!!!!
{
    return DefWindowProc(hWnd,msg,wParam,lParam);
}