C winapi hwnd返回null

时间:2017-10-11 02:31:35

标签: c windows winapi

我已经尝试了一切来解决问题,但是下面是我的代码,hwnd在运行程序时返回NULL。可能是什么原因?代码似乎很好。该计划在15分钟前工作了很长时间。我剪切了这部分源代码并再次运行它但它仍然返回NULL。这是我削减的部分。

#include <windows.h>
#define IDI_MYICON 103

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

//font

/*hFont = CreateFont(40,0,0,0,700,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("a"));
hFontIpAdres = CreateFont(25,0,0,0,700,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("a"));
hFontKurbanSecimi = CreateFont(30,0,0,0,700,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("a"));
*/
  WNDCLASSEX wc;
  HWND hwnd;
  MSG msg;

  char *windowClassName = "class1";
  printf("%s\n", windowClassName);

  wc.cbSize        = sizeof(WNDCLASSEX);
  wc.style         = 0;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wc.lpszMenuName  =  NULL;//MAKEINTRESOURCE(IDR_MYMENU);
  wc.lpszClassName =  windowClassName;
  wc.hIconSm       =  NULL;//(HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

  if(!RegisterClassEx(&wc))
  {
    printf("window registration failed\n");
  }

   printf("%s\n", windowClassName);
  hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE, windowClassName,"TTr",WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL
      );
  if(hwnd == NULL){
    printf("could not create window hwnd %d\n", GetLastError());
  }
  ShowWindow(hwnd, SW_SHOW);
  UpdateWindow(hwnd);

  while(GetMessage(&msg, NULL, 0, 0) > 0){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

1 个答案:

答案 0 :(得分:3)

您的WndProc()函数不会返回任何内容。 CreateWindowEx()实际上将使用一些基于创建的消息调用窗口过程。

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