Jump to content




Recent Status Updates

  • Photo
      15 Nov
    duzamucha

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Win32 Tutorial Part 2a- Creating Window


  • Please log in to reply
No replies to this topic

#1 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 23 February 2012 - 07:52 AM

Creating Window

In order to create window we must follow these steps. (To create Pre-defined window the steps are somewhat different. We will look at it later.)

Step 1) Define what type of window we need to create.
Step 2) Register our definition to Operating System.
Step 3) Now, Create Window based on our definition.
Step 4) Add functionality to our window.

One Look of how our complete code will look when we complete part 2 tutorial:-


#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
   // Register the window class.
   const wchar_t CLASS_NAME[]  = L"myClassname";

   WNDCLASS wc = { };

   
   wc.lpfnWndProc=WindowProc;
   wc.hInstance=hInstance;
   wc.lpszClassName=CLASS_NAME;
   
   if(!RegisterClass(&wc))
   {
      MessageBox(NULL,L"Cannot register window",L"Title Bar",MB_OK);
      return 0;
   }

   
   HWND hwnd = CreateWindow(CLASS_NAME,L"My Title Window",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,500,500,NULL,NULL,hInstance,NULL);

   if (hwnd == NULL)
   {
      MessageBox(NULL,L"CreateWindow Failed",L"Create Window problem",0);
      return 0;
   }

   

   MSG msg = { };
   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }

   return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   switch(uMsg)
   {
   
   case WM_DESTROY: PostQuitMessage(0);return 0;
      break;
   default: return DefWindowProc(hwnd,uMsg,wParam,lParam);
   }
   return 0;
}

Step 1)--->

WNDCLASS wc={}; /*We will use WNDCLASS structure to define our window. WNDCLASS structure have several members. We will only use those that are relative to this tutorial. And rest we have set to NULL*/

wc.hInstance = hInstance; // Here we pass the handle program instance.

wc.lpfnWndProc = WindowProc /*lpfnWndProc is a function pointer. windowproc is our function that will add functionality to our window.*/

wc.lpszClassName = L"myClassname"; // Unique string that names our WNDCLASS structure

For example:-

Operating system will take value from wc structure and will create something like this in memory:

struct WNDCLASS myClassname{
HINSTANCE hInstance = hinstance;
lpfnWndProc = windowproc;
style = NULL; // since we have set it to null
hIcon = NULL; // since we have set it to null
};
  • 0




Powered by binpress