You are on page 1of 11

Ministerul Educatiei,Tineretului si Sportului al Republicii Moldova

Universitatea Tehnică a Moldovei


Catedra ATI

REFERAT
Lucrarera de laborator nr.7

Obiectul: Programarea in Windows

Tema: Ferestre descendente

A efectuat: studentul gr.TI-093

Nita Alexandr

A verificat: lector superior

Postaru Andrei

Chişinău 2011
1.Scopul lucrarii: De studiat metodele si principiile de lucru cu ferestrele descendente si
principiile de prelucrare a mesajelor parvenite de la ferestre descendente.

2.Codul sursa:
// lab7.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "lab7.h"
#include "math.h"

#define MAX_LOADSTRING 100


#define PI 3.141596
#define TIME1 1
static int x=2,y=2,k=0,color=0;
int iCheck[6]={0};
static TCHAR str[10];
POINT patrat1[4],patrat2[4];
double
lengthx=150,lengthx2=150,lengthy=50,lengthy2=50,angle_stepsize=0.01,angle=0.0,angle2=0.0
, step2=0.01;
double x_1,x_2,y_1,y_2,coordx=500,coordy=400,x_11,x_22,y_11,y_22;

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:


ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,


HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.


MSG msg;
HACCEL hAccelTable;

// Initialize global strings


LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LAB7, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:


if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB7));

// Main message loop:


while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;


}

//////
struct
{
LONG style;
TCHAR *name;
}

button[]=
{
BS_AUTORADIOBUTTON, L" CUB ",
BS_AUTORADIOBUTTON, L" PIRAMIDA",
BS_AUTORADIOBUTTON, L" EFECT",
BS_AUTOCHECKBOX, L"Rosu",
BS_AUTOCHECKBOX, L"Verde",
BS_AUTOCHECKBOX, L"Albastru",
BS_PUSHBUTTON, L" START",
BS_PUSHBUTTON, L" STOP"
};

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;


wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB7));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_LAB7);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED | \


/* WS_CAPTION | \*/
WS_SYSMENU ,// | \
/* WS_THICKFRAME | \
WS_MINIMIZEBOX | \
WS_MAXIMIZEBOX,*/
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//

static HWND hWndButton[6];


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
hdc=GetDC(hWnd);
switch (wParam)
{

case 0:if (iCheck[0]==1){


EnableWindow(hWndButton[1],TRUE);
EnableWindow(hWndButton[2],TRUE);
}else{
iCheck[0] = 1; iCheck[1] = 0; iCheck[2] = 0;
EnableWindow(hWndButton[1],FALSE);
EnableWindow(hWndButton[2],FALSE);}
InvalidateRect(hWnd,NULL,TRUE);
break;
case 1: if(iCheck[1]==1){
EnableWindow(hWndButton[0],TRUE);
EnableWindow(hWndButton[2],TRUE);
}else{
iCheck[0] = 0; iCheck[1] = 1; iCheck[2] = 0;
EnableWindow(hWndButton[0],FALSE);
EnableWindow(hWndButton[2],FALSE);}
InvalidateRect(hWnd,NULL,TRUE);
break;
case 2:if(iCheck[2]==1){
EnableWindow(hWndButton[0],TRUE);
EnableWindow(hWndButton[1],TRUE);
}else{
iCheck[0] = 0; iCheck[1] = 0; iCheck[2] = 1;
EnableWindow(hWndButton[0],FALSE);
EnableWindow(hWndButton[1],FALSE);}
InvalidateRect(hWnd,NULL,TRUE);
break;
case 3:

iCheck[3] = (int) SendMessage (hWndButton[3], BM_GETCHECK, 0, 0) ; //


verificarea starii butonului
if (iCheck[3] != 0) // se verifca daca butonul a fost apasat sau nu
{color=1;
SendMessage(hWndButton[4],BM_SETCHECK,0,0); // setarea starii
butonului
SendMessage(hWndButton[5],BM_SETCHECK,0,0);
EnableWindow(hWndButton[4],FALSE); // dezactivarea butonului
EnableWindow(hWndButton[5],FALSE);
}
else
{color=0;
EnableWindow(hWndButton[4],TRUE);
EnableWindow(hWndButton[5],TRUE); // activarea butonului
}
InvalidateRect(hWnd,NULL,TRUE); // invalidarea zonei client, stergind
totul din ea
break;
case 4:
iCheck[4]= (int) SendMessage (hWndButton[4], BM_GETCHECK, 0, 0) ;
if(iCheck[4] != 0)
{color=2;
SendMessage(hWndButton[3],BM_SETCHECK,0,0);
SendMessage(hWndButton[4],BM_SETCHECK,1,0);
EnableWindow(hWndButton[3],FALSE);
EnableWindow(hWndButton[5],FALSE);
}
else
{color=0;
EnableWindow(hWndButton[3],TRUE);
EnableWindow(hWndButton[5],TRUE);
}
InvalidateRect(hWnd,NULL,TRUE);
break;
case 5:
iCheck[5]= (int) SendMessage (hWndButton[5], BM_GETCHECK, 0, 0) ;
if (iCheck[5] !=0)
{color=3;
SendMessage(hWndButton[4],BM_SETCHECK,0,0);
SendMessage(hWndButton[5],BM_SETCHECK,1,0);
EnableWindow(hWndButton[4],FALSE);
EnableWindow(hWndButton[3],FALSE);
EnableWindow(hWndButton[5],TRUE);
}
else
{color=0;
EnableWindow(hWndButton[3],TRUE);
EnableWindow(hWndButton[4],TRUE);
}
InvalidateRect(hWnd,NULL,TRUE);
break;

case 6:
iCheck[6]= (int) SendMessage (hWndButton[6], BM_GETSTATE, 0, 0) ;
iCheck[7]=0;

if (iCheck[6]!=0)
{
EnableWindow(hWndButton[0],FALSE);
EnableWindow(hWndButton[1],FALSE);
EnableWindow(hWndButton[2],FALSE);
EnableWindow(hWndButton[7],TRUE);
EnableWindow(hWndButton[6],FALSE);

}
InvalidateRect(hWnd,NULL,TRUE);

break;
case 7:
iCheck[7]= (int) SendMessage (hWndButton[7], BM_GETSTATE, 0, 0) ;
iCheck[6]=0;

EnableWindow(hWndButton[0],TRUE);
EnableWindow(hWndButton[1],TRUE);
EnableWindow(hWndButton[2],TRUE);
if(iCheck[7]!=0)
{
EnableWindow(hWndButton[7],FALSE);
EnableWindow(hWndButton[6],TRUE);
}

break;
}

// Parse the menu selections:


switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SetTimer(hWnd,TIME1 , 10,NULL);

hdc=GetDC(hWnd);
ReleaseDC(hWnd,hdc);
for(int i=0;i<=7;i++)
{
if(i<3){
hWndButton[i]=CreateWindow(L"button",button[i].name,WS_CHILD|WS_VISIBLE|
button[i].style,
100,0+20*y*i,200,20,hWnd,(HMENU) i,
((LPCREATESTRUCT)lParam)-
>hInstance,NULL);
}
if((i>=3)&&(i<6)){
hWndButton[i]=CreateWindow(L"button",button[i].name,WS_CHILD|WS_VISIBLE|
button[i].style,
700,0+20*y*k,200,20,hWnd,(HMENU) i,
((LPCREATESTRUCT)lParam)-
>hInstance,NULL);k++;
}
if (i==6){
hWndButton[i]=CreateWindow(L"button",button[i].name,WS_CHILD|
WS_VISIBLE|button[i].style,
250,0+20*y*k,200,20,hWnd,(HMENU) i,
((LPCREATESTRUCT)lParam)-
>hInstance,NULL);
}
if (i==7){
hWndButton[i]=CreateWindow(L"button",button[i].name,WS_CHILD|
WS_VISIBLE|button[i].style,
550,0+20*y*k,200,20,hWnd,(HMENU) i,
((LPCREATESTRUCT)lParam)-
>hInstance,NULL);
}
}

break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd,&rect);
HPEN pen;
if(color==1) pen=CreatePen(PS_SOLID,2,RGB(255,0,0));
if(color==2) pen=CreatePen(PS_SOLID,2,RGB(0,255,0));
if(color==3) pen=CreatePen(PS_SOLID,2,RGB(0,0,255));
SelectObject(hdc,pen);

x_1=lengthx*cos(angle);
y_1=lengthy*sin(angle);
x_2=lengthx*cos(angle+PI/2);
y_2=lengthy*sin(angle+PI/2);

x_11=lengthx2*cos(angle2);
y_11=lengthy2*sin(angle2);
x_22=lengthx2*cos(angle2+PI/2);
y_22=lengthy2*sin(angle2+PI/2);

////////////////////////////// CUBUL ///////////////////////////


if (iCheck[0]==1){
// cub - fata de jos
patrat1[0].x=coordx-x_1; patrat1[0].y=coordy-y_1;
patrat1[1].x=coordx+x_2; patrat1[1].y=coordy+y_2;
patrat1[2].x=coordx+x_1; patrat1[2].y=coordy+y_1;
patrat1[3].x=coordx-x_2; patrat1[3].y=coordy-y_2;
//cub - fata de sus
patrat2[0].x=patrat1[0].x; patrat2[0].y=patrat1[0].y-100;
patrat2[1].x=patrat1[1].x; patrat2[1].y=patrat1[1].y-100;
patrat2[2].x=patrat1[2].x; patrat2[2].y=patrat1[2].y-100;
patrat2[3].x=patrat1[3].x; patrat2[3].y=patrat1[3].y-100;
Polygon(hdc,patrat1,4);

//fetile laterale
MoveToEx(hdc,patrat1[0].x,patrat1[0].y,NULL);
LineTo(hdc,patrat2[0].x,patrat2[0].y);

MoveToEx(hdc,patrat1[1].x,patrat1[1].y,NULL);
LineTo(hdc,patrat2[1].x,patrat2[1].y);

MoveToEx(hdc,patrat1[2].x,patrat1[2].y,NULL);
LineTo(hdc,patrat2[2].x,patrat2[2].y);

MoveToEx(hdc,patrat1[3].x,patrat1[3].y,NULL);
LineTo(hdc,patrat2[3].x,patrat2[3].y);
Polygon(hdc,patrat2,4);
}

///////////////////////////// PIRAMIDA ///////////////


if (iCheck[1]==1){
// piramida - fata de jos
patrat1[0].x=coordx-x_1; patrat1[0].y=coordy-y_1;
patrat1[1].x=coordx+x_2; patrat1[1].y=coordy+y_2;
patrat1[2].x=coordx+x_1; patrat1[2].y=coordy+y_1;
patrat1[3].x=coordx-x_2; patrat1[3].y=coordy-y_2;
Polygon(hdc,patrat1,4);
// fetele laterale
MoveToEx(hdc,patrat1[0].x,patrat1[0].y,NULL);
LineTo(hdc,500,250);

MoveToEx(hdc,patrat1[1].x,patrat1[1].y,NULL);
LineTo(hdc,500,250);

MoveToEx(hdc,patrat1[2].x,patrat1[2].y,NULL);
LineTo(hdc,500,250);

MoveToEx(hdc,patrat1[3].x,patrat1[3].y,NULL);
LineTo(hdc,500,250);

}
/////////////////////////// TRANSFORMARE ////////////
if (iCheck[2]==1)
{// fata de sus
patrat1[0].x=coordx-x_1; patrat1[0].y=coordy-y_1;
patrat1[1].x=coordx+x_2; patrat1[1].y=coordy+y_2;
patrat1[2].x=coordx+x_1; patrat1[2].y=coordy+y_1;
patrat1[3].x=coordx-x_2; patrat1[3].y=coordy-y_2;
Polygon(hdc,patrat1,4);
// fata de jos
patrat2[0].x=coordx-x_11; patrat2[0].y=coordy-100-y_11;
patrat2[1].x=coordx+x_22; patrat2[1].y=coordy-100+y_22;
patrat2[2].x=coordx+x_11; patrat2[2].y=coordy-100+y_11;
patrat2[3].x=coordx-x_22; patrat2[3].y=coordy-100-y_22;

MoveToEx(hdc,patrat1[0].x,patrat1[0].y,NULL);
LineTo(hdc,patrat2[0].x,patrat2[0].y);

MoveToEx(hdc,patrat1[1].x,patrat1[1].y,NULL);
LineTo(hdc,patrat2[1].x,patrat2[1].y);

MoveToEx(hdc,patrat1[2].x,patrat1[2].y,NULL);
LineTo(hdc,patrat2[2].x,patrat2[2].y);

MoveToEx(hdc,patrat1[3].x,patrat1[3].y,NULL);
LineTo(hdc,patrat2[3].x,patrat2[3].y);

Polygon(hdc,patrat2,4);

}
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_TIMER:
switch (wParam)
{
case TIME1:
if((iCheck[0]==1)||(iCheck[1]==1)||(iCheck[2]==1))
if(iCheck[6]!=0)
{ // zona de redesenare
rect.bottom=500;
rect.left=250;
rect.right=750;
rect.top=200;
//coordonatele patrulaterului de jos
x_1=lengthx*cos(angle);
y_1=lengthy*sin(angle);
x_2=lengthx*cos(angle+PI/2);
y_2=lengthy*sin(angle+PI/2);
angle=angle+angle_stepsize;

//coordonatele patrulaterului de jos


if((iCheck[0]!=1)&&(iCheck[1]!=1)&&(iCheck[2]==1))
{ x_11=lengthx*cos(angle2);
y_11=lengthy*sin(angle2);
x_22=lengthx*cos(angle2+PI/2);
y_22=lengthy*sin(angle2+PI/2);
angle2=angle2-step2;}
InvalidateRect(hWnd,&rect,TRUE);
}

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.


INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
3. Rezultatul final:
3.1 Cubul
3.2 Piramida:

3.2 Efectul (aici, baza de sus a cubului se roteste in sens opus in comparative cu baza de jos):
4. Concluzie: În timpul efectuării acestei lucrări am facut cunostinta cu principiile
de baza al lucrului cu ferestrele descendente. Am studiat functiile si procedurile
principale(EnableWindow, SendMessage,) care raspund de functionalitatea acestor ferestre.

5. Bibliografie:
1. http://msdn.microsoft.com/;

2. „Programarea in Windows 95” A. C. Petzold;

3. Conspect disciplina „Programarea in Windows” Victor Besliu;

4. „Indrumar de laborator PW, editura Chisinau UTM 2006” Victor Besliu, Cristina Panu.

You might also like