Total Pageviews

Tuesday, February 1, 2011

Windows Programming Using C++(Part1)


                                    
Introduction
<><><><><><><><><><><><><><><><><><><><><><><><><><> 
            This documentation provides information about developing WindowApplication using C++.Here iam used to develop window programming using VisualStudio2008.
Steps
Following are the steps for creating WindowsProgramming
>> Open Visualstudio2008
>> File >>New >>Project
>>ProjectTypes
>>VisualC++
>>Select WIN32 project

GetStarted
In this section we will write a minimal Windows Program. Create a New WIN32 Project and Given your application name and Click Ok.We can see that from the Solution Explorer there are 3 Folders named Header Files,Resource Files and Source Files.From the Header Files folder Select your ApplicationName.h (Your Application name).The we can get ApplicationName.cpp.Copy and Paste the Below code into this.
Note that:The Previous codes in that page are delete and then paste the below code 
--------------------------------------------------------------------------------------------- 
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#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)
{
const wchar_t CLASS_NAME[]=L"Simple Window Class";
WNDCLASS wc={};
wc.lpfnWndProc=WindowProc;
wc.hInstance=hInstance;
wc.lpszClassName=CLASS_NAME;
RegisterClass(&wc);
HWND hwnd=CreateWindowEx(
0,
CLASS_NAME,
L"This is My First Window Programming",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(hwnd==NULL)
{
return 0;
}
ShowWindow(hwnd,nCmdShow);
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)
{c
ase WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc=BeginPaint(hwnd,&ps);
FillRect(hdc,&ps.rcPaint,(HBRUSH)(COLOR_WINDOW+1));
EndPaint(hwnd,&ps);
}
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
-------------------------------------------------------------------------------------------- 
After this Build the project and Run(Press F5).A window has been Displayed.
Code Explanation
     Window Classes
A window class defines a set of behaviors that several windows might have in common.Every window must be associated with a window class, even if your program only ever creates one instance of that class. It is important to understand that a window class is not a "class" in the C++ sense. Rather, it is a data structure used internally by the operating system. Window classes are registered with the system at run time. To register a new window class, start by filling in a WNDCLASS structure.
------------------------------------------------------------------------------------------
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
------------------------------------------------------------------------------------------
You must set the following structure members:
>>lpfnWndProc is a pointer to an application-defined function called the window procedure or "window proc." The window procedure defines most of the behavior of the window. We'll examine the window procedure in detail later. For now, just treat this as a forward reference.
>>hInstance is the handle to the application instance. Get this value from the hInstance parameter of wWinMain.
>>lpszClassName is a string that identifies the window class. Next, pass the address of the WNDCLASS structure to the RegisterClass function. This function registers the window class with the operating system.
--------------------------------------------------------------------------------------
RegisterClass(&wc);
--------------------------------------------------------------------------------------
Creating the Window
To create a new instance of a window, call the CreateWindowEx function:
--------------------------------------------------------------------------------------
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
------------------------------------------------------------------------------------
> The first parameter lets you specify some optional behaviors for the window (for example, transparent windows). Set this parameter to zero for the default behaviors.
> CLASS_NAME is the name of the window class. This defines the type of window you are creating.
> The window text is used in different ways by different types of windows. If the window has a title bar, the text is displayed in the title bar.
> The window style is a set of flags that define some of the look and feel of a window. The constant WS_OVERLAPPEDWINDOW is actually several flags combined with a bitwise OR. Together these flags give the window a title bar, a border, a system menu, and Minimize and Maximize
buttons. This set of flags is the most common style for a top-level application window.
> For position and size, the constant CW_USEDEFAULT means to use default values. The next parameter sets a parent window or owner window for the new window. Set the parent if you are creating a child window. For a top-level window, set this to NULL.
> For an application window, the next parameter defines the menu for the window. This example does not use a menu, so the value is NULL.
> hInstance is the instance handle, described previously. (See WinMain: The Application Entry Point.)
> The last parameter is a pointer to arbitrary data of type void*. You can use this value to pass a data structure to your window procedure. We'll show one possible way to use this parameter in the
section Managing Application State.
              CreateWindowEx returns a handle to the new window, or zero if the function fails. To show the
window—that is, make the window visible —pass the window handle to the ShowWindow function
--------------------------------------------------------------------------------------------
ShowWindow(hwnd, nCmdShow);
--------------------------------------------------------------------------------------------
            The hwnd parameter is the window handle returned by CreateWindowEx. The nCmdShow parameter can be used to minimize or maximize a window. The operating system passes this value to the program through the wWinMain function.

No comments:

Post a Comment