none
关于列表框和SendMessage的问题 RRS feed

  • 问题

  • 原想做个列表框简单的程序,思路是当您点击一个盘符的按键时,列表框显示出该盘中文件

    编译器:VC2010,WND 32程序

    我的代码:

    // 坚持不懈.cpp : 定义应用程序的入口点。

    #include "stdafx.h"
    #include "坚持不懈.h"
    #define 文件属性 (DDL_READWRITE|DDL_READONLY|DDL_HIDDEN|DDL_SYSTEM|DDL_DIRECTORY|DDL_ARCHIVE|DDL_DRIVES)
    #define MAX_LOADSTRING 100
    HINSTANCE hInst;        // 当前实例
    TCHAR szTitle[MAX_LOADSTRING];     // 标题栏文本
    TCHAR szWindowClass[MAX_LOADSTRING];   // 主窗口类名
    ATOM    MyRegisterClass(HINSTANCE hInstance);
    BOOL    InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
     UNREFERENCED_PARAMETER(hPrevInstance);
     UNREFERENCED_PARAMETER(lpCmdLine);
     MSG msg;
     HACCEL hAccelTable;
     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
     LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
     MyRegisterClass(hInstance);
     if (!InitInstance (hInstance, nCmdShow))
     {
      return FALSE;
     }
     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
     while (GetMessage(&msg, NULL, 0, 0))
     {
      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      {
       TranslateMessage(&msg);
       DispatchMessage(&msg);
      }
     }
     return (int) msg.wParam;
    }
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
     WNDCLASSEX wcex;
     wcex.cbSize = sizeof(WNDCLASSEX);
     wcex.style   = CS_HREDRAW | CS_VREDRAW;
     wcex.lpfnWndProc = WndProc;
     wcex.cbClsExtra  = 0;
     wcex.cbWndExtra  = 0;
     wcex.hInstance  = hInstance;
     wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
     wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
     wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY);
     wcex.lpszClassName = szWindowClass;
     wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
     return RegisterClassEx(&wcex);
    }
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
       hInst = hInstance; // 将实例句柄存储在全局变量中
       hWnd = CreateWindow(szWindowClass,TEXT("我的程序"), WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
       if (!hWnd)
       {
          return FALSE;
       }
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
       return TRUE;
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
     int wmId, wmEvent,i;
     PAINTSTRUCT ps;
     HDC hdc;
     static HWND 列表框,索引框[4];
     static int cxClient,cyClient,cxChar,cyChar;
     static TCHAR *缓存,v[100]=TEXT("文件信息"),l[50],*button[4]={TEXT("C盘"),TEXT("D盘"),TEXT("E盘"),TEXT("F盘")};
     switch (message)
     {
     case WM_CREATE:
      列表框=CreateWindow(TEXT("listbox"),TEXT("我的程序"),WS_CHILDWINDOW|WS_VISIBLE|LBS_STANDARD,
       0,0,0,0,hWnd,(HMENU)10,(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),NULL);
      
      for(i=0;i<4;i++)
       索引框[i]=CreateWindow(TEXT("button"),button[i],WS_CHILD|WS_VISIBLE,0,0,0,0,
       hWnd,(HMENU)i,(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),NULL);
       
      SetFocus(索引框[0]);
      return 0;
     case WM_SIZE:
      cxClient=LOWORD(lParam);
      cyClient=HIWORD(lParam);
      cxChar=LOWORD(GetDialogBaseUnits());
      cyChar=HIWORD(GetDialogBaseUnits());
      MoveWindow(列表框,cxClient*3/8,cyClient/4,cxClient/4+15*cxChar,cyClient/2,TRUE);
      for(i=0;i<4;i++)
       MoveWindow(索引框[i],4*cxChar,cyClient*(i+4)/12,cxChar*20,cyClient/12,TRUE);
      return 0;
     case WM_COMMAND:
      wmId    = LOWORD(wParam);
      wmEvent = HIWORD(wParam);
      if(LOWORD(wParam)==0&&WM_LBUTTONDOWN)
       SendMessage(列表框,LB_DIR,文件属性,(LPARAM)TEXT("C:\*.*"));
      if(LOWORD(wParam)==1&&WM_LBUTTONDOWN)
       SendMessage(列表框,LB_DIR,文件属性,(LPARAM)TEXT("D:\*.*"));
      if(LOWORD(wParam)==2&&WM_LBUTTONDOWN)
       SendMessage(列表框,LB_DIR,文件属性,(LPARAM)TEXT("E:\*.*"));
      if(LOWORD(wParam)==3&&WM_LBUTTONDOWN)
       SendMessage(列表框,LB_DIR,文件属性,(LPARAM)TEXT("F:\*.*"));
      switch (wmId)
      {
      case IDM_ABOUT:
       DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
       break;
      case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
      default:
       return DefWindowProc(hWnd, message, wParam, lParam);
      
      break;
      case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      TextOut(hdc,2,2*cyChar,l,wsprintf(l,TEXT("文件扫描, Writted  By  CXW_ALEX (学武)@All  Right, 2011-6-3")));
      SetTextColor(hdc,RGB(255,0,0));
      TextOut(hdc,cxClient*3/8+cyChar,cyClient/4+5*cyChar,v,wsprintf(v,TEXT("您的文件列表如下:")));
      EndPaint(hWnd, &ps);
      break;
     case WM_DESTROY:
      PostQuitMessage(0);
      break;
      }
     default:
      
      return DefWindowProc(hWnd, message, wParam, lParam);
     }
     return 0;
    }

    // “关于”框的消息处理程序。
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
     UNREFERENCED_PARAMETER(lParam);
     switch (message)
     {
     case WM_INITDIALOG:
      return (INT_PTR)TRUE;

     case WM_COMMAND:
      if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
      {
       EndDialog(hDlg, LOWORD(wParam));
       return (INT_PTR)TRUE;
      }
      break;
     }
     return (INT_PTR)FALSE;
    }

    最终运行的结果是:点击第一第二个键列表还能显示,再点就退出程序,且主窗口上也没有TextOut所要显示的内容,望各位高手指点

    • 已移动 doukooo 2011年6月6日 4:49 VC++ (发件人:微软学生讨论专区)
    2011年6月4日 4:25

答案

全部回复