How do I create window using WinMain

643 views Asked by At

I cant create a Window using WinMain.It doesnt give any errors,it just doesnt work.I even tried to copy Source code in tutorials but it didnt work.I am using :

MinGW32 ,Eclipse CDT Version: 8.1.2.201302132326

The code:

#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:The tutorial i was folowing was this:https://www.youtube.com/watch?v=ikc_I0escqU

0

There are 0 answers