《Eclipse.Rich.Client.Platform》4章 The Hyperbola Application

chapter 4 The Hyperbola Application  

本章主要讲解应用eclipse生成 rich client的模板代码


4.1 Hyperbola “Hello,world”

       File> New >Project   Choose Plug-in Project clickNext ,enter project name "org.eclipsercp.hyperbola" ,clickNext

        You will see the page of "New Plug-in Project"  这个页面默认设置其实就可以,有两个注意项

          1.Make sure to uncheck the Generate an activator,a Java class that controls the plug-in's life cycleoption;这个选项在以后会用到

          2.Select the Yes radio button in the Rich Client Applicationarea 这个选项意思是建立一个富客户端,而不仅仅是个plug-in插件

      Click Next and the wizard moves to the RCP Templates Page  这部分书里让选择 hello RCP 但是在我滴版本里没有,所以我选择 RCP 3.x application(minimal)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要定义一个API结构来实现绘制抛物线、双曲线、放大缩小和移动功能。 ```c++ #include <Windows.h> // 定义抛物线结构体 struct Parabola { int a; // 抛物线系数 int b; // 抛物线系数 int c; // 抛物线系数 }; // 定义双曲线结构体 struct Hyperbola { int a; // 双曲线系数 int b; // 双曲线系数 int c; // 双曲线系数 }; // 定义窗口类 class Window { public: Window(); ~Window(); bool Create(LPCTSTR lpWindowName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent = NULL, HMENU hMenu = NULL); void Show(int nCmdShow); private: static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); HWND m_hWnd; Parabola m_parabola; Hyperbola m_hyperbola; int m_zoom; int m_xOffset; int m_yOffset; void DrawParabola(HDC hdc); void DrawHyperbola(HDC hdc); void ZoomIn(); void ZoomOut(); void MoveLeft(); void MoveRight(); void MoveUp(); void MoveDown(); }; ``` 然后,我们需要在窗口类中实现绘制抛物线、双曲线、放大缩小和移动功能。 ```c++ #include <math.h> // 窗口类构造函数 Window::Window() : m_hWnd(NULL), m_zoom(1), m_xOffset(0), m_yOffset(0) { m_parabola.a = 1; m_parabola.b = 0; m_parabola.c = 0; m_hyperbola.a = 1; m_hyperbola.b = 0; m_hyperbola.c = 0; } // 窗口类析构函数 Window::~Window() { } // 创建窗口 bool Window::Create(LPCTSTR lpWindowName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = GetModuleHandle(NULL); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = TEXT("WindowClass"); wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wcex)) { return false; } m_hWnd = CreateWindowEx(dwExStyle, TEXT("WindowClass"), lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this); if (!m_hWnd) { return false; } return true; } // 显示窗口 void Window::Show(int nCmdShow) { ShowWindow(m_hWnd, nCmdShow); UpdateWindow(m_hWnd); } // 窗口消息处理函数 LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { Window* pWindow; if (message == WM_NCCREATE) { pWindow = static_cast<Window*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams); SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWindow)); pWindow->m_hWnd = hWnd; } else { pWindow = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA)); } if (pWindow) { switch (message) { case WM_PAINT: HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hWnd, &ps); // 绘制抛物线和双曲线 pWindow->DrawParabola(hdc); pWindow->DrawHyperbola(hdc); EndPaint(hWnd, &ps); break; case WM_KEYDOWN: switch (wParam) { case VK_ADD: // 放大 pWindow->ZoomIn(); InvalidateRect(hWnd, NULL, TRUE); break; case VK_SUBTRACT: // 缩小 pWindow->ZoomOut(); InvalidateRect(hWnd, NULL, TRUE); break; case VK_LEFT: // 左移 pWindow->MoveLeft(); InvalidateRect(hWnd, NULL, TRUE); break; case VK_RIGHT: // 右移 pWindow->MoveRight(); InvalidateRect(hWnd, NULL, TRUE); break; case VK_UP: // 上移 pWindow->MoveUp(); InvalidateRect(hWnd, NULL, TRUE); break; case VK_DOWN: // 下移 pWindow->MoveDown(); InvalidateRect(hWnd, NULL, TRUE); break; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } else { return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // 绘制抛物线 void Window::DrawParabola(HDC hdc) { int x, y; HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); for (x = -100; x <= 100; x++) { y = m_parabola.a * x * x + m_parabola.b * x + m_parabola.c; y = -y; x = m_zoom * x + m_xOffset; y = m_zoom * y + m_yOffset; if (x >= 0 && x <= 800 && y >= 0 && y <= 600) { SetPixel(hdc, x, y, RGB(0, 0, 255)); } } SelectObject(hdc, hOldPen); DeleteObject(hPen); } // 绘制双曲线 void Window::DrawHyperbola(HDC hdc) { int x, y; HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); for (x = -100; x <= 100; x++) { y = m_hyperbola.a * x * x + m_hyperbola.b * x + m_hyperbola.c; y = -y; x = m_zoom * x + m_xOffset; y = m_zoom * y + m_yOffset; if (x >= 0 && x <= 800 && y >= 0 && y <= 600) { SetPixel(hdc, x, y, RGB(255, 0, 0)); } } SelectObject(hdc, hOldPen); DeleteObject(hPen); } // 放大 void Window::ZoomIn() { m_zoom++; } // 缩小 void Window::ZoomOut() { if (m_zoom > 1) { m_zoom--; } } // 左移 void Window::MoveLeft() { m_xOffset -= 10; } // 右移 void Window::MoveRight() { m_xOffset += 10; } // 上移 void Window::MoveUp() { m_yOffset -= 10; } // 下移 void Window::MoveDown() { m_yOffset += 10; } ``` 最后,我们需要在窗口的消息循环中处理消息。 ```c++ int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { Window window; if (!window.Create(TEXT("API Demo"), WS_OVERLAPPEDWINDOW, 0, CW_USEDEFAULT, 0, 800, 600)) { return 0; } window.Show(nCmdShow); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ``` 现在,我们就成功地实现了一个基于API结构的windows应用程序,并使用GDI绘制抛物线和双曲线,并实现了放大缩小和移动功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值