Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1812738
  • 博文数量: 290
  • 博客积分: 10653
  • 博客等级: 上将
  • 技术积分: 3178
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 23:08
文章存档

2013年(6)

2012年(15)

2011年(25)

2010年(86)

2009年(52)

2008年(66)

2007年(40)

分类: C/C++

2008-10-25 22:30:25

获取屏幕分辨率:
 

/*-----------------------------------------------------
   SCRNSIZE.C -- Displays screen size in a message box
                 (c) Charles Petzold, 1998
  -----------------------------------------------------*/


#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
     TCHAR szBuffer [1024] ;
     va_list pArgList ;

          // The va_start macro (defined in STDARG.H) is usually equivalent to:

          // pArgList = (char *) &szFormat + sizeof (szFormat) ;


     va_start (pArgList, szFormat) ;

          // The last argument to wvsprintf points to the arguments


     _vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
                  szFormat, pArgList) ;// 格式化输出

          // The va_end macro just zeroes out pArgList for no good reason


     va_end (pArgList) ;//返回当前参数

     return MessageBox (NULL, szBuffer, szCaption, 0) ;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     int cxScreen, cyScreen ;

     cxScreen = GetSystemMetrics (SM_CXSCREEN) ;//获取屏幕的宽
     cyScreen = GetSystemMetrics (SM_CYSCREEN) ;//获取屏幕的高

     MessageBoxPrintf (TEXT ("ScrnSize"), //消息框标题
                       TEXT ("The screen is %i pixels wide by %i pixels high."), //格式化文本字串
                       cxScreen, cyScreen) ;// 格式化参数变量
     return 0 ;
}

 

从程序的功能来看,上述API函数调用以及执行流程都不难,但和一般常用的消息框函数又不同,而分析MessageBoxPrintf的功能时,是否对va_list va_start这几个宏感到陌生;下面我们来看看在MSDN中的定义

The va_arg, va_end, and va_start macros provide a portable way to access the arguments to a function when the function takes a variable number of arguments. Two versions of the macros are available: The macros defined in STDARG.H conform to the ANSI C standard, and the macros defined in VARARGS.H are compatible with the UNIX System V definition. The macros are:

va_alist

Name of parameter to called function (UNIX version only)

va_arg

Macro to retrieve current argument

va_dcl

Declaration of va_alist (UNIX version only)

va_end

Macro to reset arg_ptr

va_list

typedef for pointer to list of arguments defined in STDIO.H

typedef struct {
        char *a0;       /* pointer to first homed integer argument */
        int offset;     /* byte offset of next parameter */
} va_list;

va_start

Macro to set arg_ptr to beginning of list of optional arguments (UNIX version only)

下面给出这几个宏的一个例子:

 

 

/* VA.C: The program below illustrates passing a variable
 * number of arguments using the following macros:
 * va_start va_arg va_end
 * va_list va_dcl (UNIX only)
 */


#include <stdio.h>
#define ANSI /* Comment out for UNIX version */
#ifdef ANSI /* ANSI compatible version */
#include <stdarg.h>
int average( int first, ... );
#else /* UNIX compatible version */
#include <varargs.h>
int average( va_list );
#endif

void main( void )
{
   /* Call with 3 integers (-1 is used as terminator). */
   printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

   /* Call with 4 integers. */
   printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

   /* Call with just -1 terminator. */
   printf( "Average is: %d\n", average( -1 ) );
}

/* Returns the average of a variable list of integers. */
#ifdef ANSI /* ANSI compatible version */
int average( int first, ... )
{
   int count = 0, sum = 0, i = first;
   va_list marker;

   va_start( marker, first ); /* Initialize variable arguments. */
   while( i != -1 )
   {
      sum += i;
      count++;
      i = va_arg( marker, int);
   }
   va_end( marker ); /* Reset variable arguments. */
   return( sum ? (sum / count) : 0 );
}
#else /* UNIX compatible version must use old-style definition. */
int average( va_alist )
va_dcl
{
   int i, count, sum;
   va_list marker;

   va_start( marker ); /* Initialize variable arguments. */
   for( sum = count = 0; (i = va_arg( marker, int)) != -1; count++ )
      sum += i;
   va_end( marker ); /* Reset variable arguments. */
   return( sum ? (sum / count) : 0 );
}
#endif

The va_arg, va_end, and va_start macros provide access to function arguments when the number of arguments is variable. These macros are defined in STDARG.H for ANSI C compatibility, and in VARARGS.H for compatibility with UNIX System V.

 

Argument-Access Macros

Macro Use
Retrieve argument from list  从参数表中检索参数
Reset pointer  重置指针
Set pointer to beginning of argument list 设置一指针指向参数的起始位置

当函数的参数数目为变量时,使用这三个宏(va_arg, va_end, 和 va_start )来获取函数的参数;

 

阅读(1912) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~