在Windows程序上绘图(Draw on a windows program)

编程入门 行业动态 更新时间:2024-10-15 10:13:39
在Windows程序上绘图(Draw on a windows program)

好吧,这就是:我正在为学校制作一个基本的乒乓球游戏,我可以设置任何东西。 但是在程序的初始化(开始)之后,我似乎无法更新窗口。 我已经尝试先制作一个后备缓冲器,但它没有做任何事情。 代码在这里,所以看起来很漂亮,并提前谢谢!

//============================================================================= //Seth Dark @2011 //Pong v1.0 //============================================================================= //============================================================================= //to do list // - get the ball moving // - figure out the AI // - figure out the ball vs paddle // Version 2 // - count time or points // - hiscore bord //============================================================================= // Pong.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 main window class name BackBuffer* gBackBuffer = 0; // The backbuffer we will render HDC hdc; // ------------------------------ gamble const int gClientWidth = 800; const int gClientHeight = 600; Vec2 playerPos(20.0f, 250.0f); Vec2 aiPos(780.0f,250.0f); Vec2 ballPos(400.0f, 250.0f); // 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 RUN //--------------------------------------------- int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. HDC bbDC = gBackBuffer->getDC(); //Clear backbuffer HBRUSH oldBrush = (HBRUSH)SelectObject(bbDC, GetStockObject(BLACK_BRUSH)); Rectangle(bbDC,0 ,0 ,800, 600); //Draw player SelectObject(hdc,GetStockObject(WHITE_BRUSH)); // drawing the thing Rectangle(bbDC, (int)playerPos.x -10, (int)playerPos.y - 50, (int)playerPos.x + 10, (int)playerPos.y + 50); MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_PONG, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PONG)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 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. // Creation of the window upon wich we will draw // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 20; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PONG)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PONG); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); wcex.hbrBackground = (HBRUSH)GetStockObject (BLACK_BRUSH); return RegisterClassEx(&wcex); } 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, 819, 600, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 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_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... //Draw player rectangle get class look page 237 - SelectObject(hdc,GetStockObject(WHITE_BRUSH)); Rectangle(hdc, (int)playerPos.x -10, (int)playerPos.y - 50, (int)playerPos.x + 10, (int)playerPos.y + 50); //Draw AI rectangle get class look page 237 - AI Rectangle(hdc, (int)aiPos.x -10, (int)aiPos.y - 50, (int)aiPos.x + 10, (int)aiPos.y + 50); //Draw the ball Ellipse(hdc, (int)ballPos.x -20, (int)ballPos.y - 20, (int)ballPos.x + 20, (int)ballPos.y + 20); EndPaint(hWnd, &ps); break; case WM_KEYDOWN: { switch(wParam) { case 'Z': playerPos.y -= 5; break; case 'S': playerPos.y += 5; break; } // up, down, enter to play or restart break; return 0; } case WM_DESTROY: PostQuitMessage(0); break; 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; }

我希望这是足够的信息! 从星期天开始,这件事让我发疯了。 任何帮助,为什么我不能绘制将是appriciated,我已经尝试把它放在运行和绘图和其他几件事情

Ok so here it is: I'm making a basic pong game for school, and I can get anything set up. But after the intialisation (start) of the program I can't seem to update the window. I've tried drawing a backbuffer first and yet it doesn't do anything. code is under here so have a look pretty please and thank you in advance!!

//============================================================================= //Seth Dark @2011 //Pong v1.0 //============================================================================= //============================================================================= //to do list // - get the ball moving // - figure out the AI // - figure out the ball vs paddle // Version 2 // - count time or points // - hiscore bord //============================================================================= // Pong.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 main window class name BackBuffer* gBackBuffer = 0; // The backbuffer we will render HDC hdc; // ------------------------------ gamble const int gClientWidth = 800; const int gClientHeight = 600; Vec2 playerPos(20.0f, 250.0f); Vec2 aiPos(780.0f,250.0f); Vec2 ballPos(400.0f, 250.0f); // 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 RUN //--------------------------------------------- int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. HDC bbDC = gBackBuffer->getDC(); //Clear backbuffer HBRUSH oldBrush = (HBRUSH)SelectObject(bbDC, GetStockObject(BLACK_BRUSH)); Rectangle(bbDC,0 ,0 ,800, 600); //Draw player SelectObject(hdc,GetStockObject(WHITE_BRUSH)); // drawing the thing Rectangle(bbDC, (int)playerPos.x -10, (int)playerPos.y - 50, (int)playerPos.x + 10, (int)playerPos.y + 50); MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_PONG, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PONG)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 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. // Creation of the window upon wich we will draw // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 20; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PONG)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PONG); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); wcex.hbrBackground = (HBRUSH)GetStockObject (BLACK_BRUSH); return RegisterClassEx(&wcex); } 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, 819, 600, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 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_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... //Draw player rectangle get class look page 237 - SelectObject(hdc,GetStockObject(WHITE_BRUSH)); Rectangle(hdc, (int)playerPos.x -10, (int)playerPos.y - 50, (int)playerPos.x + 10, (int)playerPos.y + 50); //Draw AI rectangle get class look page 237 - AI Rectangle(hdc, (int)aiPos.x -10, (int)aiPos.y - 50, (int)aiPos.x + 10, (int)aiPos.y + 50); //Draw the ball Ellipse(hdc, (int)ballPos.x -20, (int)ballPos.y - 20, (int)ballPos.x + 20, (int)ballPos.y + 20); EndPaint(hWnd, &ps); break; case WM_KEYDOWN: { switch(wParam) { case 'Z': playerPos.y -= 5; break; case 'S': playerPos.y += 5; break; } // up, down, enter to play or restart break; return 0; } case WM_DESTROY: PostQuitMessage(0); break; 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; }

I hope this is enough info! this thing is driving me crazy since sunday. Any help why I can't draw would be appriciated, I've tried putting it in the run and the drawing and a couple of other things

最满意答案

只要需要更新区域,就会将WM_PAINT发送到窗口。 按某个键时,只更新对象状态。

您可以在单独的函数中复制WM_PAINT代码,并在按下某个键时调用它 调用RedrawWindow ,它将发送绘制消息。

WM_PAINT is sent to a window whenever it's area needs to be updated. When you press a key, you only update objects state.

You can either copy WM_PAINT code in a separate function and call it when a key is pressed or call RedrawWindow, which will send paint messages.

更多推荐

本文发布于:2023-08-03 21:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1400336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   Windows   Draw   program   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!