The Color Changer

Introduction

In this exercise, we will use track bars to change the color of a static control.

Prerequisites:

There are various types of objects you can use to preview a color. The static control is convenient because it provides a ready made rectangle although you still have to configure it because it is not natively equip to handle the change of colors. To do this, you can consider it simply as a platform with a client area. When painting this control, you mostly use the properties of the CWnd class. For this reason, you could as well use a list box, a tree view, or a list view, etc.

To allow the user to set the variances of a color, you can use sliders, scroll bars, or spin buttons, etc. In this exercise, we will use the sliders simply because we decide to use them. They don't necessarily provide more or less functionality than scroll bars or spin buttons.

Giving live and necessary feedback to the user is one of the greatest assets of a friendly application. On this application, we will use static text controls to show the red, green, and blue values of a color.

Creating the Application

The simplest static control is considered a label. It consists of a simple object that displays text. The user cannot directly change this text unless you programmatically allow it. This form of the static control is the simplest control you can use in an MFC application.

To add a simple label to a dialog box, you can click the Static Text button on the Controls toolbar and click on the dialog box. The only thing you need to do is to change the Caption property on the Properties window.

If you cannot add a label to the dialog box when designing it, you can programmatically create one. This is done using the CStatic class and calling the Create() method.

By default, you cannot change the properties of a static control unless you state this explicitly. This can be done in various ways. For example you can declare a CString variable, set its value using the Format method, then call the SetDlgItemText() function and change the text of the static control using its identifier.

 

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>

 

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++
  2. Create a new project named ColorChanger1
  3. Create the project as a Dialog Based and set the Dialog Title as Color Changer
  4. Design the dialog box as follows:
     
    ControlIDCaptionAdditional Properties
    Picture ControlIDC_PREVIEWAREA   
    Slider ControlIDC_RED_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Slider ControlIDC_GREEN_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Slider ControlIDC_BLUE_TRACK Auto Ticks: True
    Orientation: Vertical
    Tick Marks: True
    Static TextIDC_RED_VALUE128Align Text: Center
    Static TextIDC_GREEN_VALUE128Align Text: Center
    Static TextIDC_BLUE_VALUE128Align Text: Center
  5. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls (the Static Text controls are created/declared as CString variables):
     
    IdentifierValue VariableControl Variable
    IDC_PREVIEWAREA m_PreviewArea
    IDC_RED_TRACK m_RedTrack
    IDC_GREEN_TRACK m_GreenTrack
    IDC_BLUE_TRACK m_BlueTrack
    IDC_RED_VALUEm_RedValue 
    IDC_GREEN_VALUEm_GreenValue 
    IDC_BLUE_VALUEm_BlueValue 
  6. In the header file of the dialog, declare a private COLORREF variable as follows:
     
    private:
    	COLORREF brColor;
    };
  7. Initialize the colors and variable in the OnInitDialog() event of the dialog box as follows:
     
    BOOL CColorChanger1Dlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    	m_RedTrack.SetRange(0, 255);
    	m_RedTrack.SetTicFreq(15);
    	m_RedTrack.SetPos(128);
    
    	m_GreenTrack.SetRange(0, 255);
    	m_GreenTrack.SetTicFreq(15);
    	m_GreenTrack.SetPos(128);
    
    	m_BlueTrack.SetRange(0, 255);
    	m_BlueTrack.SetTicFreq(15);
    	m_BlueTrack.SetPos(128);
    
    	brColor = RGB(128, 128, 128);
    
    	SetTimer(0x124, 20, NULL);
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
  8. When the user changes the value of one of the sliders, we will retrieve its value, combine it with the values of the other two sliders, create a color from these values and apply it to the picture control. To implement this, initiate the OnTimer() event of the dialog and implement it as follows:
     
    void CColorChanger1Dlg::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    	CClientDC dc(this);
        
    	// This is the rectangle that includes the big Static rectangle
    	CRect Recto;
    
    	// Store the dimensions of the Static control in the Recto object
    	m_PreviewArea.GetWindowRect(&Recto);
    	ScreenToClient(&Recto);
    
    	// Create a brush that will be used to paint the Static
    	CBrush BrushColor(brColor);
    	// Select the brush
    	CBrush *pBrush = dc.SelectObject(&BrushColor);
    
    	// Draw the background of the Static object
    	dc.Rectangle(&Recto);
    
    	// Give the brush back to the device context
    	dc.SelectObject(pBrush);
    	CDialog::OnTimer(nIDEvent);
    }
  9. Initiate the WM_VSCROLL message for the dialog and implement its OnVScroll() event as follows:
     
    void CColorChanger1Dlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
    	// TODO: Add your message handler code here and/or call default
    	UpdateData();
    	// The actual value is the position of the slider CSliderCtrl::GetPos()
    	// We subtract it from 255 because of the orientation of the slider
    	// and the navigation of the thumb box
    	int nRed   = 255 - m_RedTrack.GetPos();
    	int nGreen = 255 - m_GreenTrack.GetPos();
    	int nBlue  = 255 - m_BlueTrack.GetPos();
    
    	CString PosRed, PosGreen, PosBlue;
    
    	m_RedValue.Format("%d", nRed);
    	m_GreenValue.Format("%d", nGreen);
    	m_BlueValue.Format("%d", nBlue);
    
    	CRect Recto;
    	brColor = RGB(nRed, nGreen, nBlue);
    
    	m_PreviewArea.GetWindowRect(&Recto);
    	ScreenToClient(&Recto);
    
    	UpdateData(FALSE);
    	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    }
  10. Test the application
  11. After using it, close it and return to your programming environment.
但是为什么换了一个函数之后就可以正常运行, void lv_demo_widgets(void) { if(LV_HOR_RES <= 320) disp_size = DISP_SMALL; else if(LV_HOR_RES < 720) disp_size = DISP_MEDIUM; else disp_size = DISP_LARGE; font_large = LV_FONT_DEFAULT; font_normal = LV_FONT_DEFAULT; lv_coord_t tab_h; if(disp_size == DISP_LARGE) { tab_h = 70; #if LV_FONT_MONTSERRAT_24 font_large = &lv_font_montserrat_24; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_24 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif #if LV_FONT_MONTSERRAT_16 font_normal = &lv_font_montserrat_16; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_16 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif } else if(disp_size == DISP_MEDIUM) { tab_h = 45; #if LV_FONT_MONTSERRAT_20 font_large = &lv_font_montserrat_20; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_20 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif #if LV_FONT_MONTSERRAT_14 font_normal = &lv_font_montserrat_14; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_14 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif } else { /* disp_size == DISP_SMALL */ tab_h = 45; #if LV_FONT_MONTSERRAT_18 font_large = &lv_font_montserrat_18; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_18 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif #if LV_FONT_MONTSERRAT_12 font_normal = &lv_font_montserrat_12; #else LV_LOG_WARN("LV_FONT_MONTSERRAT_12 is not enabled for the widgets demo. Using LV_FONT_DEFAULT instead."); #endif } #if LV_USE_THEME_DEFAULT lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK, font_normal); #endif lv_style_init(&style_text_muted); lv_style_set_text_opa(&style_text_muted, LV_OPA_50); lv_style_init(&style_title); lv_style_set_text_font(&style_title, font_large); lv_style_init(&style_icon); lv_style_set_text_color(&style_icon, lv_theme_get_color_primary(NULL)); lv_style_set_text_font(&style_icon, font_large); lv_style_init(&style_bullet); lv_style_set_border_width(&style_bullet, 0); lv_style_set_radius(&style_bullet, LV_RADIUS_CIRCLE); tv = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, tab_h); lv_obj_set_style_text_font(lv_scr_act(), font_normal, 0); if(disp_size == DISP_LARGE) { lv_obj_t * tab_btns = lv_tabview_get_tab_btns(tv); lv_obj_set_style_pad_left(tab_btns, LV_HOR_RES / 2, 0); lv_obj_t * logo = lv_img_create(tab_btns); LV_IMG_DECLARE(img_lvgl_logo); lv_img_set_src(logo, &img_lvgl_logo); lv_obj_align(logo, LV_ALIGN_LEFT_MID, -LV_HOR_RES / 2 + 25, 0); lv_obj_t * label = lv_label_create(tab_btns); lv_obj_add_style(label, &style_title, 0); lv_label_set_text(label, "LVGL v8"); lv_obj_align_to(label, logo, LV_ALIGN_OUT_RIGHT_TOP, 10, 0); label = lv_label_create(tab_btns); lv_label_set_text(label, "Widgets demo"); lv_obj_add_style(label, &style_text_muted, 0); lv_obj_align_to(label, logo, LV_ALIGN_OUT_RIGHT_BOTTOM, 10, 0); } lv_obj_t * t1 = lv_tabview_add_tab(tv, "Profile"); lv_obj_t * t2 = lv_tabview_add_tab(tv, "Analytics"); lv_obj_t * t3 = lv_tabview_add_tab(tv, "Shop"); profile_create(t1); analytics_create(t2); shop_create(t3); color_changer_create(tv); } void lv_demo_benchmark(void) { benchmark_init(); /*Manually start scenes*/ next_scene_timer_cb(NULL); } static void benchmark_init(void) { lv_disp_t * disp = lv_disp_get_default(); disp->driver->monitor_cb = monitor_cb; /*Force to run at maximum frame rate*/ if(run_max_speed) { if(disp->refr_timer) { disp_ori_timer_period = disp->refr_timer->period; lv_timer_set_period(disp->refr_timer, 1); } lv_timer_t * anim_timer = lv_anim_get_timer(); anim_ori_timer_period = anim_timer->period; lv_timer_set_period(anim_timer, 1); } lv_obj_t * scr = lv_scr_act(); lv_obj_remove_style_all(scr); lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0); title = lv_label_create(scr); lv_obj_set_pos(title, LV_DPI_DEF / 30, LV_DPI_DEF / 30); subtitle = lv_label_create(scr); lv_obj_align_to(subtitle, title, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0); scene_bg = lv_obj_create(scr); lv_obj_remove_style_all(scene_bg); lv_obj_set_size(scene_bg, lv_obj_get_width(scr), lv_obj_get_height(scr) - subtitle->coords.y2 - LV_DPI_DEF / 30); lv_obj_align(scene_bg, LV_ALIGN_BOTTOM_MID, 0, 0); lv_style_init(&style_common); lv_obj_update_layout(scr); } static void next_scene_timer_cb(lv_timer_t * timer) { LV_UNUSED(timer); lv_obj_clean(scene_bg); next_scene_timer = NULL; if(opa_mode) { if(scene_act >= 0) { if(scenes[scene_act].time_sum_opa == 0) scenes[scene_act].time_sum_opa = 1; scenes[scene_act].fps_opa = (1000 * scenes[scene_act].refr_cnt_opa) / scenes[scene_act].time_sum_opa; if(scenes[scene_act].create_cb) scene_act++; /*If still there are scenes go to the next*/ } else { scene_act++; } opa_mode = false; } else { if(scenes[scene_act].time_sum_normal == 0) scenes[scene_act].time_sum_normal = 1; scenes[scene_act].fps_normal = (1000 * scenes[scene_act].refr_cnt_normal) / scenes[scene_act].time_sum_normal; opa_mode = true; } if(scenes[scene_act].create_cb) { lv_label_set_text_fmt(title, "%"LV_PRId32"/%"LV_PRId32": %s%s", scene_act * 2 + (opa_mode ? 1 : 0), (int32_t)(dimof(scenes) * 2) - 2, scenes[scene_act].name, opa_mode ? " + opa" : ""); if(opa_mode) { lv_label_set_text_fmt(subtitle, "Result of \"%s\": %"LV_PRId32" FPS", scenes[scene_act].name, scenes[scene_act].fps_normal); } else { if(scene_act > 0) { lv_label_set_text_fmt(subtitle, "Result of \"%s + opa\": %"LV_PRId32" FPS", scenes[scene_act - 1].name, scenes[scene_act - 1].fps_opa); } else { lv_label_set_text(subtitle, ""); } } rnd_reset(); scenes[scene_act].create_cb(); next_scene_timer = lv_timer_create(next_scene_timer_cb, SCENE_TIME, NULL); lv_timer_set_repeat_count(next_scene_timer, 1); } /*Ready*/ else { /*Restore original frame rate*/ if(run_max_speed) { lv_timer_t * anim_timer = lv_anim_get_timer(); lv_timer_set_period(anim_timer, anim_ori_timer_period); lv_disp_t * disp = lv_disp_get_default(); lv_timer_t * refr_timer = _lv_disp_get_refr_timer(disp); if(refr_timer) { lv_timer_set_period(refr_timer, disp_ori_timer_period); } } generate_report(); /* generate report */ } } demo函数无法运行,另一个函数可运行,为什么?
06-07
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值