Write down the steps involved in registering a window class mentioning the funct
ID: 3610438 • Letter: W
Question
Write down the steps involved in registering a window class mentioning the function names involved in each step.Explanation / Answer
We will make a full fledge window application in four steps: 1. In first step, we will register a windows class. 2. In second step, we will create a window 3. In third step, we will make a message loop and message handlingprocedure 4. In our fourth step, we will write WinMain function and will makethe application running. Step 1 (Registering a Window Class) We write our own function RegisterAppWindow which will register thewindow and return true or false in case of success or failure. bool RegisterAppWindow(HINSTANCE hInstance) { /* Before registering class, we will have to fill the WNDCLASSstructure. */ WNDCLASS wc; wc.style = 0; wc.lpfnWndProc=MyWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra =0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, LoadCursor(IDC_ARROW)); wc.hbrBackground= (HBRUSH)GetStockObject(GRAY_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=”MyFirstWindowClass”; /* We have discussed almost all the parameters of WNDCLASS structurein our previous lecture. After filling the WNDCLASS structure, we will pass this as areference to RegisterClass function. Because RegisterClass function take pointer to thestructure WNDLCASS as a parameter. */ ATOM cAtom=RegisterClass(&wc); if(!cAtom) { MessageBox(NULL,”Error Register Window Class”,”Virtual Uinversity”,0); return 0; } return true; }//End of Function RegisterAppWindow Step 2 (Creating Window) For Creation of window, we will use Win32 API CreateWindow. We havestudied CreateWindow function in our previous lecture. Here we will usethis function for creating window. For creating window and checking return values, we will define ourown function InitApplication which will internally call CreateAppWindow andreturns true or false. It will return true in case of success and false in case failure. bool InitApplication(HINSTANCE hInstance) { HWND hWnd=NULL; /* Call RegisterAppWindow Function and returns false if this functionreturns false because we need not to proceed forward unless our windows is not registeredproperly. */ if(!RegisterAppWindow(hInstance)) { return false; } /* We already know that CreateWindow function returns handle towindow, we will save this return handle in our hWnd variable. */ hWnd = CreateWindow(“MyFirstWindowClass”,”VirtualUinversity”, WS_OVERLAPPEDWINDOW|WS_VISIBLE, 100, 50, CW_USEDDEFAULT, CW_USEDDEFAULT, NULL, NULL, hInstance, //Optional or not used in Window 2000 or above NULL); /* After receiving the return handling of the window, we areinterested to know that what information are in return handle. hWnd can have either handle towindow or Null value. Null value shows that CreateWindow function has not been successfuland no window could be created or window has successfully created otherwise. */ if (hWnd == NULL) { MessageBox(NULL,”Cannot Create Window”,”VirtualUinversity”,0); return false; } /* After Successful creation of window, we have initially hiddenwindow. For showing window on the screen, we will use following API: ShowWindow */ ShowWindow(hWnd,SW_SHOWNORMAL); /* ShowWindow take second parameter which indicates windows show orhidden states. */ return true; } //End of InitApplication Step 3 (Fetching messages and Message Procedure) For Retrieving message from message queue we are going to writefunction which will have a message loop and TranslateMessage and DispatchMessageAPI’s will be enclosed in the message loop. We will call this function RunApplication. int RunApplication() { MSG msg; /*Main Application Message Loop*/ while(GetMessage (&msg, NULL, 0, 0) > 0) { /* translate virtual-key messages into character messages */ TranslateMessage (&msg); /* dispatch message to windows procedure*/ DispatchMessage (&msg); }//End of Message Loop (while) return (int)msg.wParam; }//End of RunApplication This type of message loop, we will be using in our windowsapplications. Now we will create Window Message Procedure. This message procedure will havethe same name as given in WNDLCASS structure. LRESULT CALLBACK MyWindowProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_LBUTTONDOWN: { MessageBox(hWnd, “Left Mouse ButtonPressed”,”Virtual University”,0); return 0; } case WM_DESTROY: { /* We have recieve WM_DESTROY message, this message removes windowfrom screen and release resource, it allocated. Now at this time, we shouldpost WM_QUIT message in a message queue so that message loop terminates. For postingWM_QUIT message we use PostQuitMessage API. */ PostQuitMessage(0); return 0; } } /* DefWindowProc(hWnd, message, wParam, lParam). This function callsthe default window procedure to provide default processing for any windowmessages that an application does not process. This function ensures that everymessage is processed. DefWindowProc () is called with the same parameters received by thewindow procedure. */ DefWindowProc(hWnd,message,wParam,lParam); } // End of MyWindowProc Step 4 (WinMain Function) In this step we will write Windows starting point WinMainfunction. int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCEhPrevInstance,LPSTR lpCmdLine,int nCmdShow) { /* Call InitApplication Function and returns false if this functionreturns false because we need not to proceed forward unless our windows is not registeredand created properly. */ if(!InitApplication(hInstance)) { MessageBox(NULL,”Application could not be initializedproperly”,”Virtual University”,MB_ICONHAND|MB_OK); return 0; } /* Finally run the application until user press the close button andchoose close from system menu. */ return RunApplication(); } extract the code from it and run in dev C++ on windows applicationas a project, you can find out your answer in a way that youunderstand the whole scenario of window registering andcreating
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.