程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c-win32編程中窗口不顯示是什麼問題

c-win32編程中窗口不顯示是什麼問題

編輯:編程綜合問答
win32編程中窗口不顯示是什麼問題

《windows程序設計》第9章ENVIRON程序
把光盤中的源代碼粘貼出來發現還是沒有窗口在運行,實在想不明白
(運行環境:VS2013,WINDOWS7)
以下源代碼:

#include

#define ID_LIST 1
#define ID_TEXT 2

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Environ") ;
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         = LoadIcon (NULL, IDI_APPLICATION) ;
 wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
 wndclass.lpszMenuName  = NULL ;
 wndclass.lpszClassName = szAppName ;

 if (!RegisterClass (&wndclass))
 {
      MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                  szAppName, MB_ICONERROR) ;
      return 0 ;
 }

 hwnd = CreateWindow (szAppName, TEXT ("Environment List Box"),
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      NULL, NULL, hInstance, NULL) ;

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

 while (GetMessage (&msg, NULL, 0, 0))
 {
      TranslateMessage (&msg) ;
      DispatchMessage (&msg) ;
 }
 return msg.wParam ;

}

void FillListBox (HWND hwndList)
{
int iLength ;
TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;

 pVarBlock = GetEnvironmentStrings () ;  // Get pointer to environment block

 while (*pVarBlock)
 {
      if (*pVarBlock != '=')   // Skip variable names beginning with '='
      {
           pVarBeg = pVarBlock ;              // Beginning of variable name
           while (*pVarBlock++ != '=') ;      // Scan until '='
           pVarEnd = pVarBlock - 1 ;          // Points to '=' sign
           iLength = pVarEnd - pVarBeg ;      // Length of variable name

                // Allocate memory for the variable name and terminating
                // zero. Copy the variable name and append a zero.

           pVarName = calloc (iLength + 1, sizeof (TCHAR)) ;
           CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
           pVarName[iLength] = '\0' ;

                // Put the variable name in the list box and free memory.

           SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
           free (pVarName) ;
      }
      while (*pVarBlock++ != '\0') ;     // Scan until terminating zero
 }
 FreeEnvironmentStrings (pVarBlock) ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList, hwndText ;
int iIndex, iLength, cxChar, cyChar ;
TCHAR * pVarName, * pVarValue ;

 switch (message)
 {
 case WM_CREATE :
      cxChar = LOWORD (GetDialogBaseUnits ()) ;
      cyChar = HIWORD (GetDialogBaseUnits ()) ;

           // Create listbox and static text windows.

      hwndList = CreateWindow (TEXT ("listbox"), NULL,
                          WS_CHILD | WS_VISIBLE | LBS_STANDARD,
                          cxChar, cyChar * 3,
                          cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL),
                          cyChar * 5,
                          hwnd, (HMENU) ID_LIST,
                          (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
                          NULL) ;

      hwndText = CreateWindow (TEXT ("static"), NULL,
                          WS_CHILD | WS_VISIBLE | SS_LEFT,
                          cxChar, cyChar, 
                          GetSystemMetrics (SM_CXSCREEN), cyChar,
                          hwnd, (HMENU) ID_TEXT,
                          (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
                          NULL) ;

      FillListBox (hwndList) ;
      return 0 ;

 case WM_SETFOCUS :
      SetFocus (hwndList) ;
      return 0 ;

 case WM_COMMAND :
      if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_SELCHANGE)
      {
                // Get current selection.

           iIndex  = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
           iLength = SendMessage (hwndList, LB_GETTEXTLEN, iIndex, 0) + 1 ;
           pVarName = calloc (iLength, sizeof (TCHAR)) ;
           SendMessage (hwndList, LB_GETTEXT, iIndex, (LPARAM) pVarName) ;

                // Get environment string.

           iLength = GetEnvironmentVariable (pVarName, NULL, 0) ;
           pVarValue = calloc (iLength, sizeof (TCHAR)) ;
           GetEnvironmentVariable (pVarName, pVarValue, iLength) ;

                // Show it in window.

           SetWindowText (hwndText, pVarValue) ;
           free (pVarName) ;
           free (pVarValue) ;
      }
      return 0 ;

 case WM_DESTROY :
      PostQuitMessage (0) ;
      return 0 ;
 }
 return DefWindowProc (hwnd, message, wParam, lParam) ;

}
運行時沒有顯示錯誤
圖片說明

最佳回答:


估計窗口沒有創建成功等,失敗了,你可以設置斷點跟蹤一下,看你的代碼在哪裡出錯了

s594102234
s594102234
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved