Presentation is loading. Please wait.

Presentation is loading. Please wait.

The program in traditional OS

Similar presentations


Presentation on theme: "The program in traditional OS"— Presentation transcript:

1 The program in traditional OS
WinAPI Basics The program in traditional OS

2 WinAPI Basics The program in Windows
In Windows, the interaction between the program and the OS is a message-based interaction. It’s the Windows that calls the program continuously. In Windows the program consists of 2 parts: WinMain( ) is called first and interacts with the OS getting the Messages in Message Loop from Message Queue and sending them to the second part of program. Window CallBack Function ( ) processes the Messages. Here you write your program’s main body.

3 Message based interaction of Windows & Application
WinAPI Basics Message based interaction of Windows & Application

4 Windows Skeleton Program
WinAPI Basics WinMain ( ) { Define a window structure Register the window structure Create the window Display the window Run the message loop } WindowFunc ( ) Switch ( message ) case message 1 : _ _ _ case message 2 : default : /* A minimal windows skeleton program. */ #include <windows.h> LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { Windows Skeleton Program char szWinName[] = "MyWin"; /* name of windows class */ HWND hwnd; MSG msg; WNDCLASSEX wcl; /* Define a window class. */ wcl.cbSize = sizeof(WNDCLASSEX); wcl.hInstance = hThisInst; /* handle to this instance */ wcl.lpszClassName = szWinName; /* window class name */ wcl.lpfnWndProc = WindowFunc; /* window function */ wcl.style = 0; /* default style */ wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);/*std icon */ wcl.hIconSm= LoadIcon(NULL,IDI_WINLOGO);/*small icon */ wcl.hCursor= LoadCursor(NULL,IDC_ARROW);/*cursor style */ wcl.lpszMenuName = NULL; /* no menu */ wcl.cbClsExtra = 0; /* no extra */ wcl.cbWndExtra = 0; /* information needed */ /* Make the window background white. */ wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); if(!RegisterClassEx(&wcl)) return 0; /* Now that a window class has been registered, ** a window can be created. */ hwnd = CreateWindow( szWinName, /* name of window class */ "Windows Skeleton Program", /* title */ WS_OVERLAPPEDWINDOW, /* window style:normal */ CW_USEDEFAULT, /* X coordinate - let windows decide */ CW_USEDEFAULT, /* Y coordinate - let windows decide */ CW_USEDEFAULT, /* width - let windows decide */ CW_USEDEFAULT, /* height- let windows decide */ HWND_DESKTOP, /* no parent window */ NULL, hThisInst, /*handle of this instance of the program */ NULL /* no additional arguments */ ); /* This function is called by Windows OS and ** is passed message from the message queue. *********************************************/ LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: /* terminate the program */ PostQuitMessage(0); break; default: /* Let Windows NT process any messages ** not specified in the preceding switch ** statement. */ return DefWindowProc(hwnd, message, wParam,lParam); } return 0; If you compile the above program in VS2008 then you’ll get compile error related to Unicode usage of strings. Either you should change the project settings from Unicode to Multibyte Or, which is more desirable, redefine the strings to Unicode by Compiler directives TEXT, __T, L or using the types wchar_t, TCHAR instead of char. Example - wc.lpszClassName = L"minwindowsapp"; /* Display the window. */ ShowWindow(hwnd, nWinMode); // Setup to show window UpdateWindow(hwnd); // Request to update window /* Create the message loop. */ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); /* allow use of keyboard */ DispatchMessage(&msg); /*return control to windows */ } return msg.wParam;


Download ppt "The program in traditional OS"

Similar presentations


Ads by Google