如何使用WinMain创建窗口

时间:2014-11-15 17:08:46

标签: c++ eclipse windows mingw32 winmain

我无法使用WinMain创建一个窗口。它不会给出任何错误,它只是不起作用。我甚至尝试在教程中复制源代码但它没有用。我正在使用:

MINGW32    ,Eclipse CDT版本:8.1.2.201302132326

代码:

#define WIN32_LEAN_AND_MEAN
#include<iostream>
#include<Windows.h>

//Forward declarations
bool InitMainWindow(HINSTANCE,int);
LRESULT CALLBACK MsgProc(HWND,UINT,WPARAM,LPARAM);

//Constants
const int width=800;
const int height=600;

HWND hwnd =NULL;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
//initialize window
if(!InitMainWindow(hInstance,nCmdShow))

return 1;
//Main message loop
MSG msg ={0};
while(WM_QUIT != msg.message){
    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
return static_cast<int>(msg.wParam);
}
bool InitMainWindow(HINSTANCE hInstance, int nCmdShow)
{
//Create class window
WNDCLASSEX wcex;
WNDCLASS wc;
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(NULL_BRUSH);
wcex.lpszClassName="LearningWinMain";
wcex.lpszMenuName=NULL;
wcex.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
//Register window class
if(!RegisterClassEx(&wcex))
    return false;

hwnd =CreateWindow(
    "TutorialClassName" ,
    "Tutorial Window",
    WS_OVERLAPPED|WS_SYSMENU|WS_CAPTION,
    GetSystemMetrics(SM_CXSCREEN)/2 -width/2,
    GetSystemMetrics(SM_CYSCREEN)/2 -height/2,
    width,
    height,
    (HWND)NULL,
    (HMENU)NULL,
    hInstance,
    (LPVOID*)NULL);
    if(!hwnd)
      return false;

    ShowWindow(hwnd,nCmdShow);
     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);
       }

PS:我接下来的教程是:https://www.youtube.com/watch?v=ikc_I0escqU

0 个答案:

没有答案