Total Pageviews

Sunday, February 6, 2011

Windows Programming in C++ (Part2)

                                 Painting the window 
                Previous Documentation we study about WindowProcedure.In this Documentation explain about Painting the window.
You have created your window.Now you want to show something inside in it this is called Painting the window.
                  Sometimes your program will initiate painting to update the appearance of the window. At
other times, the operating system will notify you that you must repaint a portion of the window. When this
occurs, the operating system sends the window a WM_PAINT message. The portion of the window that must
be painted is called the update region.
                The first time a window is shown, the entire client area of the window must be painted. Therefore, you will always receive at least one WM_PAINT message when you show a window.
                             Illustration showing the update region of a window
                     You are only responsible for painting the client area. The surrounding frame, including the title bar, is automatically painted by the operating system. After you finish painting the client area, you clear the update region, which tells the operating system that it does not need to send another WM_PAINT message until something changes.
                  Now suppose the user moves another window so that it obscures a portion of your window.When the obscured portion becomes visible again, that portion is added to the update region, and your window receives another WM_PAINT message.
              Illustration showing how the update region changes when two windows overlap
                      The update region also changes if the user stretches the window. In the following diagram, the user stretches the window to the right. The newly exposed area on the right side of the window is added to the update region:
              Illustration showing how the update region changes when a window is resized
                     In our first example program, the painting routine is very simple. It just fills the entire client area with a solid color. Still, this example is enough to demonstrate some of the important concepts.
--------------------------------------------------------------------------------------------------
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
---------------------------------------------------------------------------------------------------- 

                 Start the painting operation by calling the BeginPaint function. This function fills in the PAINTSTRUCT structure with information on the repaint request. The current update region is given in the rcPaint member of PAINTSTRUCT. This update region is defined relative to the client area:
                            Illustration showing the origin of the client area
                   In your painting code, you have two basic options:
• Paint the entire client area, regardless of the size of the update region. Anything that falls outside of the update region is clipped. That is, the operating system ignores it.
• Optimize by painting just the portion of the window inside the update region.
                  If you always paint the entire client area, the code will be simpler. If you have complicated painting logic, however, it can be more efficient to skip the areas outside of the update region.
                     The following line of code fills the update region with a single color, using the system-defined window background color (COLOR_WINDOW). The actual color indicated by COLOR_WINDOW depends on the user's current color scheme.
                               FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
                       The details of FillRect are not important for this example, but the second parameter gives th coordinates of the rectangle to fill. In this case, we pass in the entire update region (the rcPaint member of PAINTSTRUCT). On the first WM_PAINT message, the entire client area needs to be painted, so rcPaint will contain the entire client area. On subsequent WM_PAINT messages, rcPaint might contain a smaller rectangle.
                       The FillRect function is part of the Graphics Device Interface (GDI), which has powered Windows graphics for a very long time. In Windows 7, Microsoft introduced a new graphics engine, named Direct2D, which supports high-performance graphics operations, such as hardware acceleration. Direct2D is also available for Windows Vista through the Platform Update for Windows Vista and for Windows Server 2008 through the Platform Update for Windows Server 2008. (GDI is still fully supported.) After you are done painting, call the EndPaint function. This function clears the update region,
which signals to Windows that the window has completed painting itself.


  


Tuesday, February 1, 2011

Windows Programming in C++(Part3)


                  
          Previous Documentation we study how we can create a window,paint the window and Close the window.In this documentation explain about detail.
Window Messages
A GUI application must respond to events from the user and from the operating system.
  •  Event from the User include all of the ways that someone can interact with your program      Eg:MouseClick,KeyStrokes,TouchScreen,Gestures etc..
  •  Event from the OS include anything "outside" of the program that can affect how the program behaves.Eg : user might plug in a new hardware device, or Windows might enter a lower-power state (sleep or hibernate).
           These events can occur at any time while the program is running, in almost any order. How do you structure a program whose flow of execution cannot be predicted in advance?
          To solve this problem, Windows uses a message-passing model. The operating system communicates with your application window by passing messages to it. A message is simply a numeric code that designates a particular event. For example, if the user presses the left mouse button, the window receives
a message with the following message code.
--------------------------------------------------------------------------------------------------
#define WM_LBUTTONDOWN 0x0201
--------------------------------------------------------------------------------------------------
           Some messages have data associated with them. For example, the WM_LBUTTONDOWN
message includes the x-coordinate and y-coordinate of the mouse cursor. 
          To pass a message to a window, the operating system calls the window procedure registered for that window. (And now you know what the window procedure is for.)
The Message Loop
          An application will receive thousands of messages while it runs. (Consider that every keystrokeand mouse-button click generates a message.) Furthermore, an application can have several windows, each with its own window procedure. How does the program receive all of these messages and deliver them to the right window procedure? The application needs a loop to get the messages and distpatch them to the correct
windows.
         For each thread that creates a window, the operating system creates a queue for windowmessages. This queue holds messages for all of the windows that are created on that thread. The queue itself is hidden from your progam. You can't manipulate the queue directly, but you can pull a message from the queue
by calling the GetMessage function.
--------------------------------------------------------------------------------------------------
MSG msg;
GetMessage(&msg, NULL, 0, 0);
--------------------------------------------------------------------------------------------------
         This function removes the first message from the head of the queue. If the queue is empty, the function blocks until another message is queued. The fact that GetMessage blocks will not make your program unresponsive. If there are no messages, there is nothing for the program to do. If you need to perform background processing, you can create additional threads that continue to run while GetMessage waits for another message.
Parameter Definition of GetMessage function
         The first parameter of GetMessage is the address of a MSG structure. If the function succeeds,it fills in the MSG structure with information about the message, including the target window and the message code. The other three parameters give you the ability to filter which messages you get from the queue. In almost all cases, you will set these parameters to zero.
         Although the MSG structure contains information about the message, you will almost never examine this structure directly. Instead, you will pass it directly to two other functions.
----------------------------------------------------------------------------------------------------
TranslateMessage(&msg);
DispatchMessage(&msg);
----------------------------------------------------------------------------------------------------
          The TranslateMessage function is related to keyboard input; it translates keystrokes (key down, key up) into characters. You don't really need to know how this function works; just remember to call it right before DispatchMessage..
          The DispatchMessage function tells the operating system to call the window procedure of the window that is the target of the message. In other words, the operating system looks up the window handle in its table of windows, finds the function pointer associated with the window, and invokes the function.For example, suppose the user presses the left mouse button. This causes a chain of events:
1. The operating system places a WM_LBUTTONDOWN message on the message queue.
2. Your program calls the GetMessage function.
3. GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure.
4. Your program calls the TranslateMessage and DispatchMessage functions.
5. Inside DispatchMessage, the operating system calls your window procedure.
6. Your window procedure can either respond to the message or ignore it.
           When the window procedure returns, it returns back to DispatchMessage, which returns to the message loop for the next message. As long as your program is running, messages will continue to arrive on the queue. Therefore, you need a loop that continually pulls messages from the queue and dispatches them. You can think of the loop as doing the following:
--------------------------------------------------------------------------------------------------
// WARNING: Don't actually write your loop this way.
while (1)
{
GetMessage(&msg, NULL, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
--------------------------------------------------------------------------------------------------
            As written, of course, this loop would never end. That's where the return value for the GetMessage function comes in. Normally, GetMessage returns a non-zero value. Whenever you want to quit the application and break out of the message loop, simply call the PostQuitMessage function.
--------------------------------------------------------------------------------------------------
PostQuitMessage(0);
--------------------------------------------------------------------------------------------------
The PostQuitMessage function puts a WM_QUIT message on the message queue. WM_QUIT is
a special message: It causes GetMessage to return zero, signaling the end of the message loop. Here is the
revised message loop.
--------------------------------------------------------------------------------------------------
// Correct.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
---------------------------------------------------------------------------------------------------
              As long as GetMessage returns a non-zero value, the expression in the while loop evaluates to true. After you call PostQuitMessage, the expression becomes false and the program breaks out of the loop. (One interesting consequence of this behavior is that your window procedure never receives a WM_QUIT message, so do not need a case statement for this message in your window procedure.)
             The next obvious question is: When should you call PostQuitMessage? We'll return to this question in the topic Closing the Window, but first we need to write our window procedure. Posted Messages versus Sent Messages
            The previous section talked about messages going onto a queue. In some situations, the operating system will call a window procedure directly, bypassing the queue.
The terminology for this distinction can be confusing:
  • Posting a message means the message goes on the message queue, and is dispatched through the  message loop (GetMessage and DispatchMessage).
  •  Sending a message means the message skips the queue, and the operating system calls the window    procedure directly.
              For now, the distinction is not very important. The window procedure handles all messages, but some messages bypass the queue and go directly to your window procedure. However, it can make a difference
               if your application communicates between windows. You can find a more thorough discussion of this issue in the topic About Messages and Message Queues.

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.