无法使用 CreateWindowEx 函数创建 window

Cant create window using CreateWindowEx function

我无法使用下面的代码创建 window 尽管我在 compilation.Only 中没有错误 window 标题为我的项目名称 appears.I 也设置了 hwnd=NULLCreateWindowEx 调用后查看 MessageBox 是否有效,但它也不起作用。

#include <windows.h>
#include <StdAfx.h>

const char* myClassName="myWindowsClassName";

//The window procedure
LRESULT CALLBACK WndProc( 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;
  }

 //Registering window

 int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance ,    LPSTR lPCmdLine , int nCmdShow ){
 WNDCLASSEX wc;
 HWND hwnd;
 MSG msg;

wc.cbSize=sizeof( WNDCLASSEX );
wc.style=0;
wc.lpfnWndProc= WndProc;
wc.cbClsExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon( NULL , IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL , IDC_ARROW);
wc.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName= (LPCWSTR) myClassName;
wc.hIconSm=LoadIcon( NULL , IDI_APPLICATION );

if( !RegisterClassEx( &wc ) ){
    MessageBox( NULL ,L"Window registeration failed" ,L"Error!" , MB_ICONEXCLAMATION | MB_OK );
    return 0;
}



  //Creating the window
  hwnd=CreateWindowEx( WS_EX_CLIENTEDGE , (LPCWSTR)myClassName , L"The title of my window" , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT, 320 , 240 , NULL , NULL , hInstance , NULL );
  hwnd=NULL;
  if( hwnd==NULL ){
MessageBox( NULL ,L"Window creation failed",L"Error!", MB_ICONEXCLAMATION | MB_OK );
return 0;
 }
  ShowWindow ( hwnd , nCmdShow );
  UpdateWindow(hwnd);


 // Step 3: The Message Loop
  while(GetMessage(&msg, NULL, 0, 0) > 0)
  {
     TranslateMessage(&msg);
     DispatchMessage(&msg);
  }
  return msg.wParam;

 }

问题出在哪里?

谢谢

Unicode 与 ANSI 不匹配?

myClassNamechar* 但您将其转换为 (LPCWSTR)。如果您正在编译为 Unicode(并且您应该),那么您应该将 class 名称定义为 const WCHAR* myClassName=L"myWindowsClassName"; 并删除强制转换(或使用 const_cast<LPTSTR>()

你也初始化失败.cbWndExtra。将 WNDCLASSEX wc; 更改为 WNDCLASSEX wc = {};

您应该执行更好的错误检查来诊断故障。检查 MSDN 以查看函数是否在失败时设置了最后一个错误。如果是,请在函数失败后调用 GetLastError()