Thứ Tư, 21 tháng 7, 2010

Giải đề thi Lập trình Win lần 1 (2009 - 2010)

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

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

COLORREF cl[7] = { RGB(255, 0, 255), RGB(0,255,0), RGB(0,0,255), RGB(255, 0, 0),
       RGB(255,255,0), RGB(0,0,0), RGB(255, 255, 255)
    };

static int c = 3;


// 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_BAI2, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);

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

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

 // 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_BAI2);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCSTR)IDC_BAI2;
 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
//
//

struct HINH
{
 int hinh;// xac dinh hinh vuong, hinh tron
 POINT tam;
 int size;
 HBRUSH hbr;
};
HINH CreateHinh(int hinh, POINT p, int size, HBRUSH hbr)
{
 HINH h;
 h.hinh = hinh;
 h.tam.x = p.x;
 h.tam.y = p.y;
 h.size = size;
 h.hbr = hbr;
 return h;
}
bool IsInside(int x, int y, HINH h)
{
 if(x >h.tam.x - h.size && x < h.tam.x +h.size)
  if(y >h.tam.y - h.size && y < n ; i++)
  if(IsInside(x, y, aHinh[i]))
   return i;
  return -1;
}
void Reset(HINH aHinh[], int nHinh)
{
 for(int i =0; i< nHinh ; i++)
  aHinh[i].hinh =0;
}
void InvertPixel (HDC hdc, int x, int y)
{
 SetPixel (hdc, x, y, cl[c]);
}
void Marker(HDC hdc, POINT pt, int size)
{
 int i, x = pt.x, y = pt.y;
 InvertPixel ( hdc, x, y);
 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);
 }
}

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 POINT apt[100];
 static int nPoint =0;
 static HINH aHinh[100];
 static int nHinh = 0;
 int i;
 static HBRUSH hbr1;
 static HBRUSH hbr2;
 static int x, y;
 static int Index =-1;
 static int size = 30;
 static HPEN hpen;

 switch (message) 
 {
  case WM_CREATE:
   hbr1 = CreateHatchBrush(1,cl[6]);
   hbr2 = CreateHatchBrush(4,cl[c]);// hinh hien hanh
   hpen = CreatePen(2,1,RGB(0,255,255));
   break;
  case WM_LBUTTONDOWN:
   if(nPoint <100)
   {
    x = apt[nPoint].x = LOWORD(lParam);
    y = apt[nPoint].y = HIWORD(lParam);
   }
   Index = kiemtra(aHinh, nHinh, x, y);

   if(Index >=0 )
   {
    Reset(aHinh, nHinh);
    aHinh[Index].hinh=1;
    //------------
    SetCursor(LoadCursor(NULL, IDC_SIZEALL));
    SetCapture(hWnd);
    SetROP2(hdc = GetDC(hWnd), R2_NOTXORPEN);
    SelectObject(hdc, hpen);
    Ellipse(hdc, aHinh[Index].tam.x - aHinh[Index].size, 
     aHinh[Index].tam.y - aHinh[Index].size,aHinh[Index].tam.x + aHinh[Index].size,
     aHinh[Index].tam.y + aHinh[Index].size);
            
    //
   }
   else
   {
    Reset(aHinh, nHinh);
    aHinh[nHinh] = CreateHinh(1,apt[nPoint], size, hbr2);
    Index = nHinh;
    nHinh ++;
   }
   
   nPoint++;
   InvalidateRect(hWnd, NULL, true);
   break;
  case WM_LBUTTONUP:
   if(GetCapture() == hWnd)
   {
    ReleaseCapture();
    InvalidateRect(hWnd, NULL, true);
   }
   break;
  case WM_MOUSEMOVE:
    x = LOWORD(lParam);
    y  = HIWORD(lParam);
   if(GetCapture() == hWnd)
   {
    SetROP2(hdc = GetDC(hWnd), R2_NOTXORPEN);
    SelectObject(hdc, hpen);
    Ellipse(hdc, aHinh[Index].tam.x - aHinh[Index].size, 
     aHinh[Index].tam.y - aHinh[Index].size,aHinh[Index].tam.x + aHinh[Index].size,
     aHinh[Index].tam.y + aHinh[Index].size);// xoa
    aHinh[Index].tam.x =x ;
    aHinh[Index].tam.y =y;
    Ellipse(hdc, aHinh[Index].tam.x - aHinh[Index].size, 
     aHinh[Index].tam.y - aHinh[Index].size,aHinh[Index].tam.x + aHinh[Index].size,
     aHinh[Index].tam.y + aHinh[Index].size);

   }
   break;
  case WM_RBUTTONDOWN:
   break;
  case WM_KEYDOWN: 
 
  switch(LOWORD(wParam))
  {
   case VK_TAB:   break;
   case VK_ESCAPE:      break;
   case VK_LEFT: 
    aHinh[Index].tam.x -=10;    
    break;
   case VK_RIGHT:
    aHinh[Index].tam.x +=10;
    break;
   case VK_UP: 
    aHinh[Index].tam.y -=10;
    break;
   case VK_DOWN: 
    aHinh[Index].tam.y +=10;
    break;
  }  
  InvalidateRect(hWnd, NULL, true);
  break;

  case WM_CHAR:
  switch(wParam)
  {
   case '+':
    aHinh[Index].size +=10;
    break;
   case '-':   
    aHinh[Index].size-=10;
     break;
   case 'c':
   case 'C':
    if (c < 7)
    {
     c++;
     hbr2 = CreateHatchBrush(4,cl[c]);
     aHinh[Index].hbr = hbr2;
    }
    else c = 0;
    InvalidateRect(hWnd, NULL, true);
     break;
   case 's':
   case 'S':
    if (c > 0)
    {
     c--;
     hbr2 = CreateHatchBrush(4,cl[c]);
     aHinh[Index].hbr = hbr2;
    }
    else c = 6;
    InvalidateRect(hWnd, NULL, true);
     break;
  }
  InvalidateRect(hWnd, NULL, true);
  break;

  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_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< nHinh ; i++)
   {
    SelectObject(hdc, aHinh[i].hbr);
    if(aHinh[i].hinh == 0)
    {
     Marker(hdc, aHinh[i].tam, aHinh[i].size);
    }
    else
     Ellipse(hdc, aHinh[i].tam.x - aHinh[i].size, aHinh[i].tam.y - aHinh[i].size,aHinh[i].tam.x + aHinh[i].size,aHinh[i].tam.y + aHinh[i].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;
}

1 nhận xét:

  1. vì một số bạn yêu cầu, nên mình đổi hình vuông thành chữ thập. nếu cần làm các hình khác thì làm tương tự.

    Trả lờiXóa

Hãy để lại tin nhắn của bạn nhé. Mình luôn muốn nghe ý kiến của bạn. Cám ơn bạn đã ghé thăm blog nha. See you