zoukankan      html  css  js  c++  java
  • 【转】Win32 创建控件风格不是Win XP解决方案

    有时候我有在用Win32 API来向窗体上添加控件时,通过CreateWindow或CreateWindowEx创建出来的控件的风格不像XP风格,而是像Windows 2000的风格,界面很难看。注意,是动态调用CreateWindow来创建控件,不是从资源中加载。

        Win32 <wbr>创建控件风格不是Win <wbr>XP解决方案

        这种情况下,我们怎么办呢,通常说来,造成这种情况都是由于没有正确加载资源,我们知道,像button, combobox, listbox等这些控件都是放在comctl32这个DLL里面的,所以有时候在用这些系统自定义的控件时,需要我们首先调用InitCommonControlsEx函数。这个DLL的版本有很多,存放在Windowswinsxs目录下面,或者你直接检索:common control,结果如下:

        Win32 <wbr>创建控件风格不是Win <wbr>XP解决方案

        好了,下面说一下解决方案吧:

        这个根本办法就是加一个manifest文件,其内容如下:  

          <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
          <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
          <assemblyIdentity
            name="XP style manifest"
            processorArchitecture="x86"
            version="1.0.0.0"
            type="win32"/>
              <dependency>
                <dependentAssembly>
                  <assemblyIdentity
                    type="win32"
                    name="Microsoft.Windows.Common-Controls"
                    version="6.0.0.0"
                    processorArchitecture="x86"
                    publicKeyToken="6595b64144ccf1df"
                    language="*"
                 />
                </dependentAssembly>
              </dependency>
           </assembly>

        保存成一个后缀为:.manifest的文件,引入到.res文件中。

        这样就行了。 

        还需要在资源文件中加一行:

           //      
         1 RT_MANIFEST "TestCtrlStyle.manifest"

        注意,有时候,加这样一句代码在资源文件中,链接时会出错:

          1>Linking...
          1>CVTRES : fatal error CVT1100: duplicate resource.  type:MANIFEST, name:1, language:0x0409
          1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

        这个错误是说工程中已经包括了RT_MANIFEST文件。那么此时,就不要加这句话,直接把这个.manifest文件加入到工程中就行了。

        修改过后的界面如下:

        Win32 <wbr>创建控件风格不是Win <wbr>XP解决方案

    上面这种方法是用manifest来设定引入的comctl32的版本,但注意上面的manifest,里面指定了这个comctl32的平台:processorArchitecture="x86",如果这个应用程序想在x64下面运行,就会导致一个问题:这个64位的程序链接的是32位的comctl32库,创建控件就会失败。所以最好的解决方案就是下面这种做法:

    在头文件里面加入下面的语句:

    #ifdef _UNICODE
    #if defined _M_IX86
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"")
    #elif defined _M_IA64
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'"")
    #elif defined _M_X64
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'"")
    #else
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")
    #endif
    #endif

    这里就是根据编译选项来指定链接什么版本的库,这样编译出来的程序就不会有上述的问题。

    转自:http://blog.sina.com.cn/s/blog_5f8817250100u2yr.html

  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/xiaoyusmile/p/4290309.html
Copyright © 2011-2022 走看看