EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

We are discussing some of the Win32 API to retrive some system paramerts and settings. We get verious Windows settings using GetSystemMetrics() function. GetDeviceCaps, GetTextMetrics, GetTextFace are the functions to retrieve the settings from screen and printer devices. Lets discuss these functions and see the utilities of these functions with one practical example for each.

GetDeviceCaps function

The GetDeviceCaps function retrieves device-specific capabilities and information for the specified GDI device.

Syntax

int GetDeviceCaps(
  HDC hdc,
  int nIndex
);

Input & Output

Handle to the device context and the capability index number (see MSDN for list of capabilities). It returns the value of the corresponding capability.

GetDeviceCaps example

#include <windows.h>
#include <commdlg.h>
int main(int argc, char* argv[])
{
  int nWidth, nHeight;
  PRINTDLG pdlg;
  /* Initialize the PRINTDLG structure. */
  memset( &pdlg, 0, sizeof( PRINTDLG ) );
10    pdlg.lStructSize = sizeof( PRINTDLG );
11    /* Set the flag to return printer DC. */
12    pdlg.Flags =  PD_RETURNDC;
13 
14    /* Invoke the printer dialog box. */
15    PrintDlg( &pdlg );
16 
17    /* hDC member of the PRINTDLG structure contains the printer DC. */
18    /* Page width in px */
19    nWidth = GetDeviceCaps(pdlg.hDC, HORZRES);
20    
21    /* Page height in px */
22    nHeight = GetDeviceCaps(pdlg.hDC, VERTRES);
23 
24    printf("Printer page dimension is %dx%d px\n", nWidth, nHeight);
25    return 0;
26  }

GetDeviceCaps output

Printer page dimension is 5100x6600 px
Press any key to continue

GetSystemMetrics function

Retrieves the specified system metric or system configuration setting.

Syntax

int WINAPI GetSystemMetrics(
  int nIndex
);

Input & Output

The system metric index which is required (see MSDN for list of values). It returns the value of the corresponding system parameter.

GetSystemMetrics example

#include <windows.h>
int main(int argc, char* argv[])
{
  int nWidth, nHeight;
    nWidth = GetSystemMetrics(SM_CXSCREEN);
  nHeight = GetSystemMetrics(SM_CYSCREEN);
  printf("Primary desktop screen is %dx%d px\n", nWidth, nHeight);
  return 0;
10  }

GetSystemMetrics output

Primary desktop screen is 1366x768 px
Press any key to continue

GetTextMetrics function

The GetTextMetrics function fills the specified buffer with the metrics for the currently selected font.

Syntax

BOOL GetTextMetrics(
   HDC          hdc,
   LPTEXTMETRIC lptm
);

GetTextFace function

The GetTextFace function retrieves the typeface name of the font that is selected into the specified device context. Lenght of the string buffer and buffer pointer for retrieving the font name are given as the input.

Syntax

int GetTextFace(
    HDC    hdc,
    int    nCount,
    LPTSTR lpFaceName
);

GetTextMetrics GetTextFace example

#include <windows.h>
#include <commdlg.h>
int main(int argc, char* argv[])
{
  char szFontName[100] = { 0};
  TEXTMETRIC tm;
  HWND  hWnd;
  HDC hDc;
  
10    /* Get desktop handle */
11    hWnd = GetDesktopWindow();
12    
13    /* Get handle to DC */
14    hDc= GetDC(hWnd);
15    
16    /* Get Desktop font name */
17    GetTextFace(hDc, 100, szFontName);
18    
19    /* Get Desktop text attributes */
20    GetTextMetrics(hDc, &tm);
21 
22    printf("Desktop font name is \"%s\" font size = %d\n", szFontName,tm.tmHeight);
23    return 0;
24  }

Output

Desktop font name is "System" font size = 16
Press any key to continue

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 1

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 11:30:07(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.