Lập trình Win bài Marker

// marker.cpp : Defines the entry point for the application.
//
//=========================== yeu cau
// viet chuong trinh ve dau marker cho moi thao tac nhap chuot
// Thao tac marker lan luot la cac dau x, o vuong, hinh thang, hinh tron
// hien thi tren cua so co dau marker hien dang co tren man hinh
//=============================

//===============================
// nhap 1 cai ra 1 hinh:  xu ly su kien
// wm_lbuttondown
// nhap 5 diem la luu toa do vao mang
// trong bai marker, mang apt la mang diem

#include “stdafx.h”
#include “resource.h”

#define MAX_LOADSTRING 100
#define MAX_MARKERS 100

#define nColors 3
const COLORREF tablecolor[] = {RGB(255,0,0), RGB(0,255,0), RGB(0,0,255)};

// int MARKER_SIZE = 5;
int iColor = 0;

void DrawMarker(HDC hdc, POINT pt, int size);
// 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_MARKER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

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

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

// 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_MARKER);
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName    = (LPCSTR)IDC_MARKER;
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)
{
// khi ban nhap chuot, no xoa het roi ve lai.
// 1. luu toa do nhap chuot vao mang
// 2. xoa het va ve lai tat ca marker
// 3. ve lai toan bo cac dau marker, dung ham for
// ham cham diem la ham setpixel

TCHAR szHello[MAX_LOADSTRING];
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
int i;
static POINT apt[MAX_MARKERS]; // POINT la co san
// apt la co san, dung marker max
static int nMarkers = 0;    // so diem minh dang dung, minh khong dung het 100 diem, ma chi
// dung so diem minh dang dung
static int sz = 5;
// tai sao phai dung static. vi ham winproc goi nhiu lan.
// neu k dung static, lan goi winpproc thu 2 se gi khoi tao lai. va nhu vay
// cai dau tien mat. neu dung static, chi khoi tao lan dau tien, lan goi thu 2 no se k
// khoi tao lai nua.
switch (message)
{
case WM_LBUTTONDOWN: // luu toa do nhap chuot vao mang
if (nMarkers <= MAX_MARKERS)
{
apt[nMarkers].x=LOWORD(lParam); // LOWORD: toa do x xua vi tri
apt[nMarkers].y=HIWORD(lParam); // HIWORD: tao do y cua vi tri
nMarkers++;
InvalidateRect(hWnd, NULL, TRUE); // ham dung de xoa het va ve lai tat ca cac diem
// tham so truyen vao la null va true
// xoa het man hinh la null
// k dung null la dung RECT rt, co nghia la hinh chu nhat
// no co 3 tham so rt.left, rt.right, rt.top, rt.bottom
// nhung bai nay se hoc sau
// ve lai la true -> goi su kien wm_paint
// false la k ve lai
}
break;

case WM_RBUTTONDOWN:
nMarkers = 0; // man hinh, sau do phai ve lai
InvalidateRect(hWnd, NULL, TRUE);//ve lai NULL:toan man hinh,TRUE: xoa xong truoc khi ve
break;

//==============sua doi=================
case WM_CHAR:
{
switch(wParam)
{
case ‘+’:
sz ++; // tang gia tri
InvalidateRect(hWnd, NULL, TRUE);
break;
case ‘-‘:
sz –;
InvalidateRect(hWnd, NULL, TRUE);
break;
case ‘c’:
case ‘C’:
iColor = (iColor+1)%nColors;
InvalidateRect(hWnd, NULL, TRUE);
break;
}
break;
}
//========================================

// yeu cau click trai chuot vo thi ve
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
wsprintf(szHello, “So Marker: %d”, nMarkers);
DrawText(hdc, szHello, strlen(szHello),&rt,DT_CENTER);
for(i = 0; i < nMarkers; i++)
DrawMarker(hdc, apt[i], sz);
// TODO: Add any drawing code here…
EndPaint(hWnd, &ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

//======================== neu n < MAXPOINT, neu nhu no con chua dc
// ham xu ly mau, lay mau doi nguoc
void InvertPixel(HDC hdc, int x, int y)
{
COLORREF crColor=GetPixel(hdc,x,y);
SetPixel (hdc, x, y,tablecolor[iColor]);
}
void DrawMarker(HDC hdc, POINT pt, int size)
{
int i, x=pt.x, y=pt.y, j;
InvertPixel(hdc,x,y);
//======================= ve dau cong
/*    for(i=1;i<=size;i++)
{
InvertPixel(hdc,x-i,y);
InvertPixel(hdc,x+i,y);
InvertPixel(hdc,x,y-i);
InvertPixel(hdc,x,y+i);
}*/
//==============================Dau X
// if(i==0 || j==0 || i==size || j==size-1) : hinh vuong
//
/*for(i=1; i<=size;i++)
{
for(j=1; j<=size; j++)
{    if(i==j || i+j==size-1)
InvertPixel(hdc, x-size+i, y-size+j);
}
}*/
//=============================Dau X cach 2
/*for(i=1;i<=size;i++)
{
InvertPixel(hdc,x-i,y-i);
InvertPixel(hdc,x+i,y+i);
InvertPixel(hdc,x-i,y+i);
InvertPixel(hdc,x+i,y-i);
}*/
//================ ve hinh tron
/*    for(i=1; i<=size; i++)
{
Ellipse (hdc, x-size, y-size, x+size, y+size);
}*/
//================= ve tam giac
/*    for(i=1; i<=size; i++)
{
MoveToEx(hdc, x, y-size, NULL);
LineTo(hdc, x+size, y+size);
LineTo(hdc, x-size, y+size);
LineTo(hdc, x, y-size);
}*/

//==================hinh thoi
for(i=1; i<=size; i++)
{
MoveToEx(hdc, x, y-size, NULL);
LineTo(hdc, x-size, y);
LineTo(hdc, x, y+size);
LineTo(hdc, x+size, y);
LineTo(hdc, x, y-size);
}
//===================hinh vuong ?
/*    for(i=1; i<=size;i++)
{
for(j=1; j<=size; j++)
{    if(i==0 || j==0 || i==size-1 || j==size-1)
InvertPixel(hdc, x-size+i, y-size+j);

}
}*/
}
// 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;
}

// ve hinh tam giac
/*
MoveToEx(hdc, x, y+size, NULL);
LineTo(hdc, x-size, y-size);a
LineTo(hdc, x+size, y-size);
LineTo(hdc, x, y+size);
*/

Advertisements

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s

%d bloggers like this: