www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Win32 API: GetStockObject(), GetTextMetricsA(), TextOutA(), SetTextAlign()

reply Kevin M <Kevin_member pathlink.com> writes:
Hello, can someone help me fix the following errors? Thanks.

Kevin M


D:\SYSMETS1>dmd sysmets1.d
/C:\dmd\bin\..\..\dm\bin\link.exe sysmets2,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

sysmets1.obj(sysmets1)
Error 42: Symbol Undefined _GetTextMetricsA 8
sysmets1.obj(sysmets1)
Error 42: Symbol Undefined _TextOutA 20
sysmets1.obj(sysmets1)
Error 42: Symbol Undefined _SetTextAlign 8
sysmets1.obj(sysmets1)
Error 42: Symbol Undefined _GetStockObject 4
--- errorlevel 4
*/

/*----------------------------------------------------
SYSMETS1.D -- System Metrics Display Program No. 1
(c) Charles Petzold, 1998
----------------------------------------------------*/

import std.c.windows.windows;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int result;

gc_init();                  // initialize garbage collector
_minit();                   // initialize module constructor table

try
{
_moduleCtor();          // call module constructors
_moduleUnitTests();     // run unit tests (optional)

result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

catch (Object o)            // catch any uncaught exceptions
{
MessageBoxA(null, cast(char *)o.toString(), "Error",
MB_OK | MB_ICONEXCLAMATION);
result = 0;             // failed
}

gc_term();                  // run finalizers; terminate garbage collector
return result;
}

// ************************************************************

int myWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR[] szAppName = "SysMets1" ;
HWND         hwnd ;
MSG          msg ;
WNDCLASS     wndclass ;

wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc   = &WndProc ;
wndclass.cbClsExtra    = 0 ;
wndclass.cbWndExtra    = 0 ;
wndclass.hInstance     = hInstance ;
wndclass.hIcon         = LoadIconA (cast(HINSTANCE) null, IDI_APPLICATION) ;
wndclass.hCursor       = LoadCursorA (cast(HINSTANCE) null, IDC_ARROW) ;
wndclass.hbrBackground = cast (HBRUSH) (GetStockObject (WHITE_BRUSH)) ;
//wndclass.hbrBackground = cast (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName  = null ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClassA (&wndclass))
{
MessageBoxA (cast (HWND) null, "This program requires Windows NT!",
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindowA (szAppName,                     // window class name
"Get System Metrics No. 1",    // window caption
WS_OVERLAPPEDWINDOW,           // window style
CW_USEDEFAULT,                 // initial x position
CW_USEDEFAULT,                 // initial y position
CW_USEDEFAULT,                 // initial x size
CW_USEDEFAULT,                 // initial y size
cast(HWND) null,               // parent window handle
cast(HMENU) null,              // window menu handle
hInstance,                     // program instance handle
null) ;                        // creation parameters

assert(hwnd);

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessageA (&msg, cast (HWND) null, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessageA (&msg) ;
}
return msg.wParam ;
}

struct SYSMETRIC
{
int     iIndex ;
TCHAR[] szLabel ;
TCHAR[] szDesc  ;
}

SYSMETRIC[] sysmetrics =
[
{SM_CXSCREEN,            "SM_CXSCREEN",
"Screen width in pixels"},
{SM_CYSCREEN,            "SM_CYSCREEN",
"Screen height in pixels"}
] ;

extern (Windows)
int WndProc (HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
{
static int  cxChar, cxCaps, cyChar ;
HDC         hdc ;
int         i ;
PAINTSTRUCT ps ;
TCHAR       szBuffer [10] ;
TEXTMETRICA tm ;

switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;

GetTextMetricsA (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;

ReleaseDC (hwnd, hdc) ;
break ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

for (i = 0 ; i < sysmetrics.length ; i++)
{
TextOutA (hdc, 0, cyChar * i,
sysmetrics[i].szLabel,
lstrlenA (sysmetrics[i].szLabel)) ;

TextOutA (hdc, 22 * cxCaps, cyChar * i,
sysmetrics[i].szDesc,
lstrlenA (sysmetrics[i].szDesc)) ;

SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

TextOutA (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,
wsprintfA (szBuffer, "%5d",
GetSystemMetrics (sysmetrics[i].iIndex))) ;

SetTextAlign (hdc, TA_LEFT | TA_TOP) ;

}
EndPaint (hwnd, &ps) ;
break ;

case WM_DESTROY :
PostQuitMessage (0) ;
break ;

default:
break ;
}
return DefWindowProcA (hwnd, message, wParam, lParam) ;
}


//Win32 API header entries missing in C:\dmd\src\phobos\std\c\windows\windows.d
extern(Windows) int lstrlenA(LPCSTR);                       // Required, works
extern(Windows) int GetSystemMetrics(int);                  // Required, works

alias char  TCHAR;                                          // Required, works

enum                                                        // Required, works
{
SM_CXSCREEN =           0,    // Screen width in pixels
SM_CYSCREEN =           1,    // Screen height in pixels
}

//Win32 API header entries exist in C:\dmd\src\phobos\std\c\windows\windows.d
extern(Windows) int wsprintfA(LPSTR, LPCSTR, ...);          // Required, works
extern(Windows) HGDIOBJ GetStockObject(int);                // Doesn't work!
extern(Windows) BOOL GetTextMetricsA(HDC, TEXTMETRICA*);    // Doesn't work!
extern(Windows) BOOL TextOutA(HDC, int, int, LPCSTR, int);  // Doesn't work!
extern(Windows) uint SetTextAlign(HDC, UINT);               // Doesn't work!
Jan 25 2005
parent Vathix <vathix dprogramming.com> writes:
On Wed, 26 Jan 2005 04:40:07 +0000 (UTC), Kevin M  
<Kevin_member pathlink.com> wrote:

 Hello, can someone help me fix the following errors? Thanks.

 Kevin M


 D:\SYSMETS1>dmd sysmets1.d
 /C:\dmd\bin\..\..\dm\bin\link.exe sysmets2,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 7.50B1
 Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

 sysmets1.obj(sysmets1)
 Error 42: Symbol Undefined _GetTextMetricsA 8

Use "dmd sysmets1.d gdi32.lib" to link GDI in.
Jan 25 2005