博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VS2008在工具栏Toolbar里添加XP风格spin box control
阅读量:3975 次
发布时间:2019-05-24

本文共 3820 字,大约阅读时间需要 12 分钟。

 

Spin box control本是用于对话框中的控件。但有时需要将它放到工具栏上。VC++ 2008里实现如下:

一、在工具栏上新增一个工具按钮项,资源命名为ID_TOOL_EDIT_TIME。再增加一个工具项,资源命名为ID_TOOL_INTEGRA_TIME。

二、在工程里增加一个新MFC Class,base class选CToolBar,新类命名为CMainToolBar。

三、在MainToolBar.h中声明两个对象:

        CSpinButtonCtrl m_wndIntegrationTime;

        CEdit m_wndEditTime;

四、在MainFrm.h中加上 #include "MainToolBar.h",然后找到CToolBar m_wndToolBar; 改为CMainToolBar m_wndToolBar;

五、在MainFrm.cpp中,CMainFrame::OnCreate函数里,后面(return TRUE 语句前)前添加:

int index = 0;

RECT rect;

//添加Edit控件

index = m_wndToolBar.CommandToIndex(ID_TOOL_EDIT_TIME);

m_wndToolBar.SetButtonInfo(index, ID_TOOL_EDIT_TIME, TBBS_SEPARATOR, 80);

m_wndToolBar.GetItemRect(index, &rect);

    //设置位置,几个数字是调出来的,可以根据自己的情况改变

rect.left+=6;

rect.right+=10;

rect.top+=2;

rect.bottom -= 3;

if(!m_wndToolBar.m_wndEditTime.Create(ES_CENTER | ES_NUMBER |ES_LOWERCASE, rect, &m_wndToolBar, ID_TOOL_EDIT_TIME))                 //创建Edit控件

{

        TRACE0("Failed to create spin-box/n");

         return FALSE;

}

m_wndToolBar.m_wndEditTime.ShowWindow(SW_SHOW);

//添加Spin box control控件

// 创建并显示控件

if(!m_wndToolBar.m_wndIntegrationTime.Create(UDS_ALIGNRIGHT| UDS_NOTHOUSANDS |UDS_ARROWKEYS |UDS_SETBUDDYINT   , rect, &m_wndToolBar, ID_TOOL_INTEGRA_TIME))

{

       TRACE0("Failed to create spin-box/n");

        return FALSE;

}

m_wndToolBar.m_wndIntegrationTime.SetBuddy(m_wndToolBar.GetDlgItem(ID_TOOL_EDIT_TIME)); //set the Edit box to be the buddy window

m_wndToolBar.m_wndIntegrationTime.SetRange32(1,100000); //set the Edit time limits

UDACCEL v_accel={0,100};        //set the increasing step

m_wndToolBar.m_wndIntegrationTime.SetAccel(1,&v_accel); //set spin control interval/step to be 100

m_wndToolBar.m_wndIntegrationTime.ShowWindow(SW_SHOW);

六、编译,运行,此时工具栏上出现如下所示的Spin box和Edit Box。

上面语句中的UDACCEL v_accel={0,100};是指定加速键,即按下向上或向下的按钮后,每次增量步长为100,按下键0秒后开始加速。

m_wndToolBar.m_wndIntegrationTime.SetAccel(1,&v_accel);这句使加速设置有效。

注意到,此时的spin box控件是如下所示的2000风格,比较难看。

 

可根据需要改为XP风格。VS 2005和2008中,如果Project->Property -- General -- Character Set 为Use Unicode Character Set,那么程序的界面自然就是XP风格,不需要手动添加任何东西。但是如果这里是Use Multi-Byte Character Set,那么就要自己动手加东西了。

1、建一个名为XPStyle.manifest的文件

2、把下面的内容拷到这个文件中

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

<assemblyIdentity

      version="1.0.0.0"

      processorArchitecture="X86"

     

      type="win32"

/>

<description>Your app description here</description>

<dependency>

    <dependentAssembly>

      <assemblyIdentity

          type="win32"

         

          version="6.0.0.0"

          processorArchitecture="X86"

          publicKeyToken="6595b64144ccf1df"

          language="*"

        />

    </dependentAssembly>

</dependency>

</assembly>

3、Project->Add Existing Item...将XPStyle.manifest添加到工程中。

4、Project->***properties-> configuration properties ->Manifest tool->Input and Output->Embed Manifest选no

5、Rebuild.....就可以了。

 

此时的spin box和edit box其实什么都做不了,因为没有添加消息相应函数。很不幸,ClassWizard上看不到这些消息。所以我们只能写代码添加。由于spin box此时已与edit box是关联控件(buddy window),已实现联动,即spin box值的改变将自动引起edit box相应变化。所以,只需要添加edit box变化时的消息相应函数即可。

首先,在MainFrm.h里声明消息相应函数

protected:

        afx_msg void OnEditIntegrationTimeChange();

再在MainFrm.cpp里的消息映射中添加语句

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

        ON_EN_CHANGE(ID_TOOL_EDIT_TIME, &CMainFrame::OnEditIntegrationTimeChange)

END_MESSAGE_MAP()

然后在MainFrm.cpp里加入消息函数的定义

void CMainFrame::OnEditIntegrationTimeChange()

{

//add your own code here

}

最后再次编译,运行。应该是正常工作了。

 

最后要说明的是,这里是利用了set buddy window功能,利用设置关联窗口实现编辑控件的自动更新变化。也可以稍作修改,不设置关联。添加spin box的消息处理函数,手动添加改变edit box的代码。最简单的spin box消息是UDN_DELTAPOS消息,对应函数映射方式为:

        ON_NOTIFY(UDN_DELTAPOS , ID_TOOL_INTEGRA_TIME, &CMainFrame::OnSpinBoxChange)

但是这个消息有个缺点,就是它是在点击spin box上下箭头后,更新spin box value前发出消息的。所以,消息处理函数中得到的值仍是变化前的spin box position值。这样就会导致每次都滞后一个变化。还有一个消息是WM_VSCROLL,但是Windows消息的处理过程就稍复杂了,需要switch(uMsg)判断。所以,推荐最好还是尽量使用上面提到的关联来实现edit box的变化。

Reference web pages:

如何在工具栏上添加平面下拉控件:

VC中Spin控件的使用:

使程序具有XP风格:

Manifest问题:

Spin Control用法:

Spin Control使用心得:

Customizing Toolbars:

转载地址:http://gneki.baihongyu.com/

你可能感兴趣的文章
s3c2440&nbsp;spi驱动DMA模式
查看>>
编写的dm9000ep驱动(2.6.13)
查看>>
编写的dm9000ep驱动(2.6.13)
查看>>
DM9000&nbsp;驱动移植及源码简析
查看>>
DM9000&nbsp;驱动移植及源码简析
查看>>
创建字符设备的三种方法&nbsp;(转载)
查看>>
Linux下ARM汇编教程
查看>>
Linux下ARM汇编教程
查看>>
linux面试题参考答案
查看>>
LINUX驱动之IIC子系统之三I2C的数…
查看>>
LINUX驱动之IIC子系统之三I2C的数…
查看>>
Linux设备驱动之I2C架构分析
查看>>
浅析linux内核中的idr机制
查看>>
i2c子系统之__i2c_first_dynamic_b…
查看>>
linux内核SPI总线驱动分析(一)
查看>>
浅析linux内核中的idr机制
查看>>
source&nbsp;insight使用
查看>>
linux内核SPI总线驱动分析(一)
查看>>
linux设备模型之uart驱动架构分析
查看>>
source&nbsp;insight使用
查看>>