I'm the story I was telling
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Vẽ bông hoa

Go down

Vẽ bông hoa Empty Vẽ bông hoa

Bài gửi by anbinhtrong Thu Apr 22, 2010 7:41 am

// BongHoa.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "math.h"
#define MAX_LOADSTRING 100
#define MAX_MARKERS 100
#define MARKER_SIZE 5
struct BongHoa{
POINT Tam;
POINT DiemDieuKhien;
COLORREF MauVien;
COLORREF MauTo;
};
void DrawMarker(HDC hdc,POINT pt,int size);
void DrawFlower(HDC hdc,BongHoa phoa,int SoCanh,int GocNghieng);
POINT QuayDiem(POINT Tam,POINT p,int GocQuay);
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BONGHOA, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BONGHOA);

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

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is 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)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BONGHOA);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_BONGHOA;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, 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_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

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

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
static BongHoa bh[100];
static int sbh=0;
static POINT Marker[MAX_MARKERS];
static int nMarker=0;
int i;
double pi=3.1415926;
switch(message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
Marker[nMarker].x=LOWORD(lParam);
Marker[nMarker].y=HIWORD(lParam);
if((nMarker%2)==1)
{
bh[sbh].Tam=Marker[nMarker-1];
bh[sbh].DiemDieuKhien=Marker[nMarker];
bh[sbh].MauTo=RGB(rand()%255,rand()%255,rand()%255);
bh[sbh].MauVien=RGB(rand()%255,rand()%255,rand()%255);
sbh++;
}
nMarker++;
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_RBUTTONDOWN:
sbh=0;
nMarker=0;
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
for(i=0;i<sbh;i++)
DrawFlower(hdc,bh[i],4,0);
for(i=0;i<nMarker;i++)
DrawMarker(hdc,Marker[i],MARKER_SIZE);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void DrawMarker(HDC hdc,POINT pt,int size)
{
SetPixel(hdc,pt.x,pt.y,RGB(255,0,0));
for(int i=0;i<size;i++)
{
SetPixel(hdc,pt.x+i,pt.y,RGB(255,0,0));
SetPixel(hdc,pt.x-i,pt.y,RGB(255,0,0));
SetPixel(hdc,pt.x,pt.y+i,RGB(255,0,0));
SetPixel(hdc,pt.x,pt.y-i,RGB(255,0,0));
}
}
//quay diem ko sai
POINT QuayDiem(POINT Tam,POINT p,int GocQuay)
{
double pi=3.1415926;
POINT a;
double R=sqrt(pow(p.x-Tam.x,2)+pow(p.y-Tam.y,2));
double goc=acos((double)(p.x-Tam.x)/R);
goc=(p.y<=Tam.y)?2*pi-goc:goc;
goc=goc+GocQuay*pi/180;
a.x=Tam.x+(long)(R*cos(goc));
a.y=Tam.y+(long)(R*sin(goc));
return a;
}
void DrawFlower(HDC hdc,BongHoa phoa,int SoCanh,int GocNghieng)
{
HBRUSH hbr;
HPEN hp;
int i;
hp=CreatePen(PS_SOLID,2,phoa.MauVien);
hbr=CreateSolidBrush(phoa.MauTo);
SelectObject(hdc,hp);
SelectObject(hdc,hbr);
BeginPath(hdc);
double pi=3.1415926;
POINT *b;
b=new POINT[3*SoCanh];
b[2]=phoa.Tam;
b[0]=phoa.DiemDieuKhien;
b[1].x=(2*phoa.Tam.x-phoa.DiemDieuKhien.x);
b[1].y=b[0].y;

int GocQuay=360/SoCanh;
int j=1;
for(i=3;i<3*SoCanh;i+=3,j++)
{
b[i]=QuayDiem(phoa.Tam,b[0],j*GocQuay);
b[i+1]=QuayDiem(phoa.Tam,b[1],j*GocQuay);
b[i+2]=QuayDiem(phoa.Tam,b[2],j*GocQuay);
}
for(i=0;i<3*SoCanh;i++)
b[i]=QuayDiem(phoa.Tam,b[i],GocNghieng);
MoveToEx(hdc,phoa.Tam.x,phoa.Tam.y,NULL);
PolyBezierTo(hdc,b,3*SoCanh);
CloseFigure(hdc);
EndPath(hdc);
StrokeAndFillPath(hdc);
DeleteObject(hp);
DeleteObject(hbr);
}
anbinhtrong
anbinhtrong
Admin
Admin

Tổng số bài gửi : 216
Join date : 05/11/2009
Age : 35
Đến từ : BT

https://ngoctho.forum-viet.net

Về Đầu Trang Go down

Vẽ bông hoa Empty Re: Vẽ bông hoa

Bài gửi by anbinhtrong Thu Apr 22, 2010 8:02 am

Trường hợp thêm góc nghiêng và tăng số cánh hoa, bạn bổ sung:
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
if(socanh>3) socanh--;
break;
case VK_RIGHT:
if(socanh<15) socanh++;
break;
case VK_UP:
GocNghieng+=10;
break;
case VK_DOWN:
GocNghieng-=10;
break;
}
InvalidateRect(hWnd,NULL,TRUE);
break;

Lưu ý: bạn nên sửa lại tham số hàm WM_PAINT cho phù hợp. Vẽ bông hoa Icon_exclaim
anbinhtrong
anbinhtrong
Admin
Admin

Tổng số bài gửi : 216
Join date : 05/11/2009
Age : 35
Đến từ : BT

https://ngoctho.forum-viet.net

Về Đầu Trang Go down

Về Đầu Trang


 
Permissions in this forum:
Bạn không có quyền trả lời bài viết