home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 07 / win_book.asc < prev   
Text File  |  1991-06-11  |  4KB  |  129 lines

  1. _PROGRAMMER'S BOOKSHELF COLUMN_
  2. by Andrew Schulman
  3.  
  4.  
  5. Example 1: A simple Windows program, SYSTRES.C, consists of 
  6. little more than the OkMsgBox() function from Petzold's Programming
  7. Windows.
  8.  
  9. /*  SYSTRES.C -- System Resources
  10. Borland C++:    
  11. bcc -W systres.c
  12. rc systres.exe */
  13.  
  14. #include <windows.h>
  15.  
  16. /* undocumented Windows call: KERNEL.138 */
  17. extern DWORD FAR PASCAL GetHeapSpaces(WORD hModule);
  18. void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
  19. {
  20.     DWORD info = GetHeapSpaces(GetModuleHandle(module));
  21.     *pfree = LOWORD(info);
  22.     *ptotal = HIWORD(info);
  23.     *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
  24. }
  25.  
  26. #define PROGMAN_EXE     "progman.exe"
  27. #define PROGMAN_CLASS   "Progman"
  28. #define HELP_ABOUT      0x387       // subject to change!!!
  29.  
  30. // run the Program Manager Help menu About... box
  31. void progmgr_aboutbox(void)
  32. {
  33.     WORD progmgr;
  34.     // use class name ("Progman"), not window title ("Program Manager"):
  35.     // see Richter, Windows 3: A Developer's Guide, p.80
  36.     if (! (progmgr = FindWindow(PROGMAN_CLASS, 0)))
  37.     {
  38.         // WinExec() is async: equivalent of spawn() P_NOWAIT
  39.         WinExec(PROGMAN_EXE, SW_SHOWMINIMIZED);
  40.         if (! (progmgr = FindWindow(PROGMAN_CLASS, 0)))
  41.             return; // couldn't find or load Program Manager
  42.     }
  43.     // use PostMessage instead of SendMessage so we run async
  44.     PostMessage(progmgr, WM_COMMAND, HELP_ABOUT, 0L);
  45. }
  46. // from Petzold, Programming Windows, p. 441
  47. void OkMsgBox(char *szCaption, char *szFormat, ...)
  48. {
  49.     char szBuffer[256] ;
  50.     char *pArguments ;
  51.     pArguments = (char *) &szFormat + sizeof szFormat ;
  52.     wvsprintf(szBuffer, szFormat, pArguments) ; // changed from vsprintf
  53.     MessageBox(NULL, szBuffer, szCaption, MB_OK) ;
  54. }
  55. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, 
  56.                                                                  int nCmdShow)
  57. {
  58.     WORD user_free, user_total, user_percent;
  59.     WORD gdi_free, gdi_total, gdi_percent;
  60.     heap_info("USER", &user_free, &user_total, &user_percent);
  61.     heap_info("GDI",  &gdi_free,  &gdi_total, &gdi_percent);
  62.     progmgr_aboutbox();
  63.     OkMsgBox("System Resources",
  64.         "USER heap: %u bytes free out of %u (%u%% free)\n"
  65.         "GDI heap: %u bytes free out of %u (%u%% free)\n"
  66.         "Free system resources: %u%%\n",
  67.             user_free, user_total, user_percent,
  68.             gdi_free, gdi_total, gdi_percent,
  69.             min(user_percent, gdi_percent));
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. Example 2: The same utility as in Example 1, but this time written with
  77. the Realizer BASIC development environment for Windows.
  78.  
  79. ' SYSTRES.RLZ -- System Resources
  80. ' run-time dynamic linking to Windows API
  81. external "kernel" func GetHeapSpaces (word) as dword
  82. external "kernel" func GetModuleHandle(pointer) as word
  83. external "user" func FindWindow(pointer, pointer) as word
  84. external "user" proc PostMessage(word, word, word, dword)
  85.  
  86. func get_free(modname)
  87.     heap_space = GetHeapSpaces(GetModuleHandle(modname))
  88.     free = heap_space mod 65536         ' LOWORD
  89.     total = Floor(heap_space / 65536)   ' HIWORD
  90.     percent = 100 * free / total
  91.     Print #1; modname, "heap: ", free, " bytes free out of ", total;
  92.     Print #1; " (", percent, "%)"
  93.     return percent
  94. end func
  95.  
  96. WM_COMMAND = 273     ' 111h (yuk! no hex!)
  97. HELP_ABOUT = 903     ' 387h
  98.  
  99. ' pull down Program Manager Help About... box
  100. progmgr = FindWindow("Progman", Pointer(0))
  101. if progmgr = 0 then 
  102.     Shell "progman", _Minimize
  103.     progmgr = FindWindow("Progman", Pointer(0))
  104. end if
  105. PostMessage(progmgr, WM_COMMAND, HELP_ABOUT, 0)
  106.  
  107. LogNew(1; "System Resources")
  108. user_percent = get_free("USER")
  109. gdi_percent = get_free("GDI")
  110. Print #1; "System Resources: ", min(user_percent, gdi_percent), "% free"
  111. LogControl(_Show)
  112.  
  113.  
  114.  
  115.  
  116. Example 3: "hello world!" using the Oriel graphical batch language
  117.  
  118. { HELLO.ORL -- "hello world" for Oriel }
  119.  
  120. UseCaption("Hello")
  121. SetMenu("&File", IGNORE, "E&xit", Do_Exit, ENDPOPUP)
  122. UseFont("Roman", 0, 24, BOLD, NOITALIC, NOUNDERLINE, 64, 0, 0)
  123. DrawText(10, 10, "Hello world!")
  124. WaitInput()
  125. Do_Exit:
  126.     End
  127.  
  128.  
  129.