change_colors.cpp

本文介绍了一个使用Windows API进行编程的应用实例,涉及窗口类注册、消息循环处理、位图操作及对话框处理等内容。通过具体代码展示了如何创建窗口、绘制位图以及响应用户命令。

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> 
#include <windows.h> 
#include "Change_Colors.h"


#if defined (WIN32)
 #define IS_WIN32 TRUE
#else
 #define IS_WIN32 FALSE
#endif

#define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32

HINSTANCE hInst;   // current instance

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application";

BOOL RegisterWin95( CONST WNDCLASS* lpwc );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
   MSG      msg;
   HWND     hWnd;
   WNDCLASS wc;

   // Register the main application window class.
   //............................................
   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;      
   wc.cbClsExtra    = 0;                     
   wc.cbWndExtra    = 0;                     
   wc.hInstance     = hInstance;             
   wc.hIcon         = LoadIcon( hInstance, lpszAppName );
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;             
   wc.lpszClassName = lpszAppName;             

   if ( IS_WIN95 )
   {
      if ( !RegisterWin95( &wc ) )
         return( FALSE );
   }
   else if ( !RegisterClass( &wc ) )
      return( FALSE );

   hInst = hInstance;

   // Create the main application window.
   //....................................
   hWnd = CreateWindow( lpszAppName,
                        lpszTitle,   
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0,
                        CW_USEDEFAULT, 0, 
                        NULL,             
                        NULL,             
                        hInstance,        
                        NULL              
                      );

   if ( !hWnd )
      return( FALSE );

   ShowWindow( hWnd, nCmdShow );
   UpdateWindow( hWnd );        

   while( GetMessage( &msg, NULL, 0, 0) )  
   {
      TranslateMessage( &msg );
      DispatchMessage( &msg ); 
   }

   return( msg.wParam );
}


BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
   WNDCLASSEX wcex;

   wcex.style         = lpwc->style;
   wcex.lpfnWndProc   = lpwc->lpfnWndProc;
   wcex.cbClsExtra    = lpwc->cbClsExtra;
   wcex.cbWndExtra    = lpwc->cbWndExtra;
   wcex.hInstance     = lpwc->hInstance;
   wcex.hIcon         = lpwc->hIcon;
   wcex.hCursor       = lpwc->hCursor;
   wcex.hbrBackground = lpwc->hbrBackground;
   wcex.lpszMenuName  = lpwc->lpszMenuName;
   wcex.lpszClassName = lpwc->lpszClassName;

   // Added elements for Windows 95.
   //...............................
   wcex.cbSize = sizeof(WNDCLASSEX);
   wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
                            IMAGE_ICON, 16, 16,
                            LR_DEFAULTCOLOR );
   
   return RegisterClassEx( &wcex );
}

#define ALIGNLONG(i) ((i+3)/4*4)
#define WIDTH        50
#define HEIGHT       50
#define COLORBITS    4


LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HBITMAP            hBitmap;
static LPBITMAPINFOHEADER lpbi;
static BITMAPINFOHEADER   bi;

   switch( uMsg )
   {
      case WM_CREATE :
               {
                  HDC    hDC, hMemDC;
                  LPTSTR lpstBitmap;

                  hDC = GetDC (hWnd) ;
                 
                  // Intialize BITMAPINFOHEADER data.
                  //.................................
                  bi.biSize     = sizeof (BITMAPINFOHEADER) ;
                  bi.biWidth    = WIDTH;    
                  bi.biHeight   = HEIGHT;
                  bi.biPlanes   = 1;
                  bi.biBitCount = COLORBITS;
                  bi.biCompression = BI_RGB;
                  bi.biSizeImage = ( ALIGNLONG( (WIDTH * COLORBITS)/8 )
                                         * HEIGHT);
                  bi.biXPelsPerMeter = 0;
                  bi.biYPelsPerMeter = 0;
                  bi.biClrUsed       = 0;
                  bi.biClrImportant  = 0;

                  // Create uninitialized DIB bitmap.
                  //.................................
                  hBitmap = CreateDIBitmap( hDC, &bi, 0L, NULL,
                                            NULL, 0);

                  // Allocate memory for BITMAPINFO structure.
                  //..........................................
                  lpbi = (BITMAPINFOHEADER*)GlobalAlloc( GPTR,
                                       sizeof(BITMAPINFOHEADER) +
                                       16 * sizeof (RGBQUAD) +
                                       (ALIGNLONG((WIDTH * COLORBITS)/8)
                                       * HEIGHT) );
                 
                  // Copy BITMAPINFOHEADER information.
                  //...................................
                  *lpbi = bi;
                 
                  // Initialize structure data with GetDIBits().
                  //............................................
                  GetDIBits( hDC, hBitmap, 0, 50, NULL,
                            (LPBITMAPINFO) lpbi, DIB_RGB_COLORS);
                 
                  // Create memory device context and select bitmap.
                  //................................................
                  hMemDC = CreateCompatibleDC( hDC );
                 
                  SelectObject( hMemDC, hBitmap );
                 
                  // Paint on memory device context.
                  //................................
                  SelectObject( hMemDC, GetStockObject(BLACK_BRUSH) );
                  Rectangle (hMemDC, 0, 0, WIDTH, HEIGHT);

                  SelectObject( hMemDC, GetStockObject(WHITE_BRUSH) ) ;
                  Ellipse( hMemDC, 0, 0, WIDTH, HEIGHT );
                  MoveToEx( hMemDC, 0, 0, NULL );
                  LineTo( hMemDC, WIDTH / 2, HEIGHT / 2 );
                  LineTo( hMemDC, WIDTH, 0 );
                 
                  // Set pointer to bitmap抯 bit data.
                  //..................................
                  lpstBitmap = (LPSTR)lpbi + (WORD)sizeof(BITMAPINFOHEADER) +
                                             (16 * sizeof(RGBQUAD));

                  GetDIBits( hDC, hBitmap, 0, HEIGHT, lpstBitmap,
                             (LPBITMAPINFO) lpbi, DIB_RGB_COLORS);

                  DeleteDC( hMemDC );
                  ReleaseDC( hWnd, hDC);
               }
               break;

      case WM_PAINT :
               {
                  PAINTSTRUCT ps;
                  HDC         hMemDC;

                  BeginPaint( hWnd, &ps );
                  hMemDC = CreateCompatibleDC( ps.hdc );

                  SelectObject( hMemDC, hBitmap );
                  BitBlt( ps.hdc, 10, 10, WIDTH, HEIGHT,
                          hMemDC, 0, 0, SRCCOPY );

                  DeleteDC( hMemDC );
                  EndPaint( hWnd, &ps );
               }
               break;

      case WM_COMMAND :
              switch( LOWORD( wParam ) )
              {
                 case IDM_TEST :
                        {
                           HDC    hDC;
                           int    nBytes, i;
                           LPTSTR lpstBitmap, lpstTemp;

                           hDC = GetDC( hWnd );

                           lpstBitmap = (LPSTR)lpbi +
                                        (WORD) sizeof(BITMAPINFOHEADER) +
                                         (16 * sizeof (RGBQUAD));

                           nBytes = ALIGNLONG((WIDTH * COLORBITS)/8)
                                    * HEIGHT;

                           lpstTemp = lpstBitmap;
                 
                           // Change 0 bytes to 0x2C in bitmap data.
                           //........................................        
                           for( i = 0; i < nBytes; i++ )
                           {
                              if ( 0 == *lpstTemp )
                                 *lpstTemp = 0x2C;

                              lpstTemp++;
                           }

                           // Make the changes to the bitmap.
                           //................................
                           SetDIBits( hDC, hBitmap, 0, HEIGHT, lpstBitmap,
                                      (LPBITMAPINFO) lpbi, DIB_RGB_COLORS );

                           ReleaseDC( hWnd, hDC );

                           InvalidateRect( hWnd, NULL, TRUE );
                           UpdateWindow( hWnd );
                        }
                        break;

                 case IDM_ABOUT :
                        DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
                        break;

                 case IDM_EXIT :
                        DestroyWindow( hWnd );
                        break;
              }
              break;
     
      case WM_DESTROY :
              GlobalFree( lpbi );
              PostQuitMessage(0);
              break;

      default :
            return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
   }

   return( 0L );
}


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, TRUE);       
                       return (TRUE);
               }
               break;
   }

   return (FALSE);
}

void ImuProcess::set_extrinsic(const MD(4,4) &T) { Lidar_T_wrt_IMU = T.block<3,1>(0,3); Lidar_R_wrt_IMU = T.block<3,3>(0,0); } void ImuProcess::set_extrinsic(const V3D &transl) { Lidar_T_wrt_IMU = transl; Lidar_R_wrt_IMU.setIdentity(); } void ImuProcess::set_extrinsic(const V3D &transl, const M3D &rot) { Lidar_T_wrt_IMU = transl; Lidar_R_wrt_IMU = rot; } void ImuProcess::set_gyr_cov(const V3D &scaler) { cov_gyr_scale = scaler; } void ImuProcess::set_acc_cov(const V3D &scaler) { cov_acc_scale = scaler; } void ImuProcess::set_gyr_bias_cov(const V3D &b_g) { cov_bias_gyr = b_g; } void ImuProcess::set_acc_bias_cov(const V3D &b_a) { cov_bias_acc = b_a; } /* Modified by XinweiCC 2025-04-06 13:37:05 */ void ImuProcess::set_init_state(const bool need_modify_init_state, const V3D &init_pos, const double &init_yaw) { need_modify_init_state_ = need_modify_init_state; init_pos_ = init_pos; init_yaw_ = init_yaw; } /* ======================================== */ void ImuProcess::IMU_init(const MeasureGroup &meas, esekfom::esekf<state_ikfom, 12, input_ikfom> &kf_state, int &N) { /** 1. initializing the gravity, gyro bias, acc and gyro covariance ** 2. normalize the acceleration measurenments to unit gravity **/ // EASY_FUNCTION(profiler::colors::Red300);//debug V3D cur_acc, cur_gyr; if (b_first_frame_) { Reset(); N = 1; b_first_frame_ = false; const auto &imu_acc = meas.imu.front()->linear_acceleration; const auto &gyr_acc = meas.imu.front()->angular_velocity; mean_acc << imu_acc.x, imu_acc.y, imu_acc.z; mean_gyr << gyr_acc.x, gyr_acc.y, gyr_acc.z; first_lidar_time = meas.lidar_beg_time; } for (const auto &imu : meas.imu) { const auto &imu_acc = imu->linear_acceleration; const auto &gyr_acc = imu->angular_velocity; cur_acc << imu_acc.x, imu_acc.y, imu_acc.z; cur_gyr << gyr_acc.x, gyr_acc.y, gyr_acc.z; mean_acc += (cur_acc - mean_acc) / N; mean_gyr += (cur_gyr - mean_gyr) / N; cov_acc = cov_acc * (N - 1.0) / N + (cur_acc - mean_acc).cwiseProduct(cur_acc - mean_acc) * (N - 1.0) / (N * N); cov_gyr = cov_gyr * (N - 1.0) / N + (cur_gyr - mean_gyr).cwiseProduct(cur_gyr - mean_gyr) * (N - 1.0) / (N * N); // cout<<"acc norm: "<<cur_acc.norm()<<" "<<mean_acc.norm()<<endl; N ++; } state_ikfom init_state = kf_state.get_x(); init_state.grav = S2(- mean_acc / mean_acc.norm() * G_m_s2); /* Modified by XinweiCC 2025-07-07 23:41:08 */ // Set the initial position for re-localization // Set the initial rotation to align the gravity vector with the Z-axis and yaw for re-localization Eigen::Vector3d grav_body = -mean_acc.normalized(); Eigen::Quaterniond quat_align = Eigen::Quaterniond::FromTwoVectors(grav_body, Eigen::Vector3d(0, 0, -1)); Eigen::Quaterniond quat_final = quat_align; if (need_modify_init_state_) { init_state.pos = init_pos_; Eigen::Quaterniond quat_yaw(Eigen::AngleAxisd(init_yaw_, Eigen::Vector3d(0, 0, 1))); quat_final = quat_yaw * quat_align; } init_state.rot = SO3(quat_final); init_state.grav = S2(Eigen::Vector3d(0, 0, -1) * G_m_s2); std::cout << "IMU init gyr: " << mean_gyr.transpose() << std::endl; std::cout << "IMU init acc: " << mean_acc.transpose() << std::endl; std::cout << "IMU init pos: " << init_state.pos.transpose() << std::endl; std::cout << "IMU init euler angle: " << quat_final.toRotationMatrix().eulerAngles(0, 1, 2).transpose() << std::endl; /* ======================================== */ //state_inout.rot = Eye3d; // Exp(mean_acc.cross(V3D(0, 0, -1 / scale_gravity))); init_state.bg = mean_gyr; init_state.offset_T_L_I = Lidar_T_wrt_IMU; init_state.offset_R_L_I = Lidar_R_wrt_IMU; kf_state.change_x(init_state); esekfom::esekf<state_ikfom, 12, input_ikfom>::cov init_P = kf_state.get_P(); init_P.setIdentity(); init_P(6,6) = init_P(7,7) = init_P(8,8) = 0.00001; init_P(9,9) = init_P(10,10) = init_P(11,11) = 0.00001; init_P(15,15) = init_P(16,16) = init_P(17,17) = 0.0001; init_P(18,18) = init_P(19,19) = init_P(20,20) = 0.001; init_P(21,21) = init_P(22,22) = 0.00001; kf_state.change_P(init_P); last_imu_ = meas.imu.back(); } 详细注释上述代码,函数前加说明,详细到参数
10-01
在c++图形学中我已经配置好openGL环境,补充代码然后添加一个按6键出来一个6个颜色不同的六边形功能,修改我给你的main函数代码#include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> #include "Lab3_2.h" #include "Demo3_2.h" #include "Demo3_3.h" #include "Demo3_4.h" #include "Demo_5.h" // 包含新增的Demo_5头文件 using namespace std; // 设置 const unsigned int SCR_WIDTH = 1200; const unsigned int SCR_HEIGHT = 800; GLFWwindow* window; void framebuffer_size_callback(GLFWwindow* window, int width, int height); void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); void processInput(GLFWwindow* window); void render(); void init(); void cleanup(); Lab3_2* lab3_2 = nullptr; // 实验室3_2场景对象指针 Demo3_2* demo3_2 = nullptr; // 演示3_2场景对象指针 Demo3_3* demo3_3 = nullptr; // 演示3_3场景对象指针 Demo3_4* demo3_4 = nullptr; // 演示3_4场景对象指针 Demo_5* demo_5 = nullptr; // 新增:Demo_5场景对象指针 int sceneType = 1; // 当前场景类型 bool enableCullFace = false; // 是否启用面剔除 GLenum cullMode = GL_FRONT; // 剔除模式,默认为剔除正面 GLenum frontFace = GL_CCW; // 正面绕向,默认为逆时针 int main() { // 初始化glfw窗口 glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 创建窗口对象 window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Demo3", NULL, NULL); if (window == NULL) //窗口创建失败提示 { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); //将窗口的上下文设置为当前线程的主上下文 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //设置窗口大小改变时的回调函数 glfwSetKeyCallback(window, key_callback); //设置键盘按键回调函数 // 初始化glad if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } int nrAttributes; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes); //获取支持的最大顶点属性数量 std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl; init(); // 窗口消息循环 while (!glfwWindowShouldClose(window)) { processInput(window); // 处理输入 render(); // 渲染 glfwSwapBuffers(window); //交换前后缓冲 glfwPollEvents(); } cleanup(); //释放内存 glfwTerminate(); //终止GLFW return 0; } void cleanup() { if (lab3_2) delete lab3_2; if (demo3_2) delete demo3_2; if (demo3_3) delete demo3_3; if (demo3_4) delete demo3_4; if (demo_5) delete demo_5; // 新增:清理Demo_5资源 } void init() { lab3_2 = new Lab3_2(); lab3_2->setRenderParameter(); demo3_2 = new Demo3_2(); demo3_2->setRenderParameter(); demo3_3 = new Demo3_3(); demo3_3->setRenderParameter(); demo3_4 = new Demo3_4(); demo3_4->setRenderParameter(); demo_5 = new Demo_5(); // 新增:初始化Demo_5 demo_5->setRenderParameter(); } void render() { glClearColor(0.2, 0.3, 0.2, 1); //设置清屏颜色 glClear(GL_COLOR_BUFFER_BIT); //清除颜色缓冲 // 根据场景类型渲染不同场景 switch (sceneType) { case 1: if (lab3_2) lab3_2->render(); break; case 2: if (demo3_2) demo3_2->render(); break; case 3: if (demo3_3) demo3_3->render(); break; case 4: if (demo3_4) demo3_4->render(); break; case 5: if (demo_5) demo_5->render(); break; // 新增:渲染六边形场景 } } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { // 判断按键是否被释放 if (action == GLFW_RELEASE) { // 场景切换按键 if (key >= GLFW_KEY_1 && key <= GLFW_KEY_5) // 修改:添加5键支持 sceneType = key - GLFW_KEY_1 + 1; // 其他功能键处理保持不变... else if (key == GLFW_KEY_E) { enableCullFace = !enableCullFace; //切换面剔除启用状态 if (sceneType == 1 && lab3_2) { lab3_2->setRenderParameter(enableCullFace, cullMode, frontFace); } } else if (key == GLFW_KEY_C) { //按C键切换绕向 frontFace = (frontFace == GL_CCW) ? GL_CW : GL_CCW; if (sceneType == 1 && lab3_2) { lab3_2->setRenderParameter(enableCullFace, cullMode, frontFace); } } else if (key == GLFW_KEY_F) { //按键F切换裁剪面 cullMode = (cullMode == GL_FRONT) ? GL_BACK : GL_FRONT; if (sceneType == 1 && lab3_2) { lab3_2->setRenderParameter(enableCullFace, cullMode, frontFace); } } } } // 其他函数保持不变... void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } 给出完整的代码,我一共需要3个文件,Dome3_6.h,Dome3_6.cpp
10-16
六自由度机械臂ANN人工神经网络设计:正向逆向运动学求解、正向动力学控制、拉格朗日-欧拉法推导逆向动力学方程(Matlab代码实现)内容概要:本文档围绕六自由度机械臂的ANN人工神经网络设计展开,详细介绍了正向与逆向运动学求解、正向动力学控制以及基于拉格朗日-欧拉法推导逆向动力学方程的理论与Matlab代码实现过程。文档还涵盖了PINN物理信息神经网络在微分方程求解、主动噪声控制、天线分析、电动汽车调度、储能优化等多个工程与科研领域的应用案例,并提供了丰富的Matlab/Simulink仿真资源和技术支持方向,体现了其在多学科交叉仿真与优化中的综合性价值。; 适合人群:具备一定Matlab编程基础,从事机器人控制、自动化、智能制造、电力系统或相关工程领域研究的科研人员、研究生及工程师。; 使用场景及目标:①掌握六自由度机械臂的运动学与动力学建模方法;②学习人工神经网络在复杂非线性系统控制中的应用;③借助Matlab实现动力学方程推导与仿真验证;④拓展至路径规划、优化调度、信号处理等相关课题的研究与复现。; 阅读建议:建议按目录顺序系统学习,重点关注机械臂建模与神经网络控制部分的代码实现,结合提供的网盘资源进行实践操作,并参考文中列举的优化算法与仿真方法拓展自身研究思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值