Bài 1 quả banh di chuyển
3 posters
Trang 1 trong tổng số 1 trang
Bài 1 quả banh di chuyển
// Single_Bound_Version1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//-----------Khai bao Single Ball-----------
enum {ID_MOVE_TIMER=1, ID_CHANGE_COLOR=2};
// 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_SINGLE_BOUND_VERSION1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SINGLE_BOUND_VERSION1);
// 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_SINGLE_BOUND_VERSION1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_SINGLE_BOUND_VERSION1;
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);
//----------Khai bao cac bien trong bai Single Ball---------------
static int x=40, y=50, rBall=10, dx=5, dy=5, nCrIdx;
static COLORREF aColorTab[]={RGB(0xff,0xff,0xff),RGB(0,0xff,0),RGB(0xff,0,0),RGB(0,0,0xff),RGB(0xff,0xff,0),RGB(0xff,0,0xff),RGB(0,0xff,0xff)};
static int nColors= sizeof(aColorTab)/sizeof(aColorTab[0]);
//*********The End khai bao bien***************
switch (message)
{
case WM_CREATE:
SetTimer(hWnd,ID_MOVE_TIMER,10,NULL);
SetTimer(hWnd,ID_CHANGE_COLOR,1000,NULL);
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_TIMER:
switch(wParam)
{
case ID_MOVE_TIMER:
RECT rClient, r;
GetClientRect(hWnd, &rClient);
r.left=x-rBall-abs(dx);
r.right=x+rBall+abs(dx);
r.top=y-rBall-abs(dy);
r.bottom=y+rBall+abs(dy);
x=x+dx;
y=y+dy;
if(x+rBall > rClient.right && dx>0) dx = -dx;
if(x-rBall < 0 && dx<0) dx = -dx;
if(y+rBall > rClient.bottom && dy>0) dy=-dy;
if(y-rBall < 0 && dy<0) dy=-dy;
InvalidateRect(hWnd, &r,TRUE);
break;
case ID_CHANGE_COLOR:
++nCrIdx %=nColors;
break;
}
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);
//---------Ve qua banh----------
HBRUSH hbr;
hbr=CreateSolidBrush(aColorTab[nCrIdx]);
SelectObject(hdc,hbr);
Ellipse(hdc,x-rBall,y-rBall,x+rBall,y+rBall);
DeleteObject(hbr);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
KillTimer(hWnd, ID_MOVE_TIMER);
KillTimer(hWnd, ID_CHANGE_COLOR);
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;
}
Giả sử thay đổi code
//++nCrIdx %=nColors;
//InvalidateRect(hWnd, &r,TRUE);
break;
case ID_CHANGE_COLOR:
++nCrIdx %=nColors;
InvalidateRect(hWnd, &r,TRUE);
break;
Bạn sẽ không thấy trái banh đổi màu gì hết. Nhưng khi thu nhỏ cửa sổ xuống, rồi mở lại, nó ra màu khác. Mình không hiểu lắm.
Rect r vẫn thay đổi liên tục mà. Nhưng đáng lẽ theo cơ chế stack, quả banh sẽ đổi màu nhưng không thay đổi vi trí khi đổi màu?
Re: Bài 1 quả banh di chuyển
Vì sao mình run nó báo lỗi:
C:\Documents and Settings\ES\Desktop\bounce1\bounce1.cpp(15) : error C2199: syntax error : found '$S1 (' at global scope (was a declaration intended?)
ngay tại dòng
C:\Documents and Settings\ES\Desktop\bounce1\bounce1.cpp(15) : error C2199: syntax error : found '$S1 (' at global scope (was a declaration intended?)
ngay tại dòng
enum ( ID_MOVE_TIMER = 1, ID_CHANGE_COLOR = 2);
Re: Bài 1 quả banh di chuyển
bạn nth nè, mình thấy 2 cái ID này, bạn phải tạo trong rc trước, nếu bạn không tạo trước, nó sẽ báo là ID chưa được khai báo. Bạn thử xem nhé.
passreturn- Supper mem
- Tổng số bài gửi : 24
Số điểm : 56
Số lần được cám ơn : 15
Ngày đến diễn đàn: : 24/04/2010
Re: Bài 1 quả banh di chuyển
2 cái ID không sai. Hiểu đơn giản là chỗ nào bạn gõ ID_MOVE_TIMER1 thì khi biên dịch, nó sẽ thay bằng 1. Ah, mà enum đó được khai báo phạm vi là toàn cục.
Re: Bài 1 quả banh di chuyển
mình test bài của bạn anbinhtrong, bài mình ra 4 lỗi. Thứ nhất, nó nói enum nó không hiểu, thứ 2, nơi nào mình đặt 2 cái ID kia, nó đều báo lỗi, dù mình đã khai báo y như bạn passreturn nói.
Thử lần nữa
Bạn thử copy code của mình vào trong project example "Hello world" nhé. Sau đó đổi lần lượt 2 dòng:
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TênProject);
wcex.lpszMenuName = (LPCSTR)IDC_Tênproject;
Bạn có thể ở resource phần Menu & Icon để sửa lại cho tên sao cho phù hợp.
Chúc bạn thành công
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TênProject);
wcex.lpszMenuName = (LPCSTR)IDC_Tênproject;
Bạn có thể ở resource phần Menu & Icon để sửa lại cho tên sao cho phù hợp.
Chúc bạn thành công
Re: Bài 1 quả banh di chuyển
AH, lúc mình thử copy code này vào project mới của mình, chỉ xảy ra 2 lỗi phần menu và icon thôi. Có thể do bộ VS 98 portable bị lỗi chăng? Bạn kiểm tra lại thử xem.
Bạn có thể copy 4 lỗi đó lên diễn đàn được không? Kể cả vị trí báo lỗi trong đoạn code nữa.
Bạn có thể copy 4 lỗi đó lên diễn đàn được không? Kể cả vị trí báo lỗi trong đoạn code nữa.
Similar topics
» vẽ bánh xe quay
» Huyền thoại một tình yêu
» Chuyên đề toán học
» Những mẩu chuyện về Bác
» 10 lời khuyên của các chuyên gia cho người mới bắt đầu học PHP
» Huyền thoại một tình yêu
» Chuyên đề toán học
» Những mẩu chuyện về Bác
» 10 lời khuyên của các chuyên gia cho người mới bắt đầu học PHP
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|