我想写一个debug类,用来输出调试信息,因为这个类可能不止用在一个工程内,所以我希望debug类中创建窗口的时候不使用mfc的资源文件,而是完全独立的创建一个窗口用来显示信息,这样的话如果别的程序需要用到这个deubg类,我只要直接包含这个类就可以直接使用了,而不需要再去创建资源文件什么的,最好创建窗口的代码都在一个函数中完成才好,请各位帮忙,十分感谢!

解决方案 »

  1.   

    没有显示出来,一些参数我不知道应该怎么样填,还有我看的教程中说要使用回掉函数处理消息循环,
    但是我在vc中按教程中说的方法声明回掉函数出现问题,回掉函数是这样声明的,其它的代码我就不贴上来了,肯定是错了,哪位给个正确的代码吧,谢谢
    LRESULT CALLBACK msg (HWND, UINT, WPARAM, LPARAM)
      

  2.   

    /*------------------------------------------------------------
       HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                     (c) Charles Petzold, 1998
      ------------------------------------------------------------*/#include <windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         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) GetStockObject (WHITE_BRUSH) ;
         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,                  // window class name
                              TEXT ("The Hello Program"), // 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
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
         
         switch (message)
         {
         case WM_CREATE:
              PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
              return 0 ;
              
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
              
              GetClientRect (hwnd, &rect) ;
              
              DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
              
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
      

  3.   

    先注册好窗口类,然后编写好窗口回调函数,用CreateWindow()创建窗口
      

  4.   

    使用CreateWindow函数,使用VC创建Win32 Application中的HelloWorld程序,看看这个程序的源代码你就知道怎么使用这个函数了。
      

  5.   

    WNDCLASS     wc ;      wc.style         = CS_HREDRAW | CS_VREDRAW ;     wc.lpfnWndProc   = WndProc ;     wc.cbClsExtra    = 0 ;     wc.cbWndExtra    = 0 ;     wc.hInstance     = hInstance ;     wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;     wc.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wc.lpszMenuName  = NULL ;     wc.lpszClassName = szAppName ; 
         if (!RegisterClass (&wc))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                       szAppName, MB_ICONERROR) ;          return 0 ;     }     hwnd = CreateWindow (szAppName,                  // window class name                          TEXT ("创建!"), // 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                          NULL,                       // parent window handle                          NULL,                       // window menu handle                          hInstance,                  // program instance handle                          NULL) ;                     // creation parameters          ShowWindow (hwnd, iCmdShow) ;     UpdateWindow (hwnd) ;
      

  6.   

    我之前就是参考的象上面那样的代码,不过我声明回掉函数的时候出现下面的错误error C2535: 'long __stdcall debugclass::msg(struct HWND__ *,unsigned int,unsigned int,long)' : member function already defined or declared
      

  7.   

    搞的怎么复杂?!如果你只是做调试用的类,MessageBox(..)就搞定了。
    我相信:既然它都能满足微软的需要,也满足你的需要吧?!:)
      

  8.   

    另,从你的错误:
    error C2535: 'long __stdcall debugclass::msg(struct HWND__ *,unsigned int,unsigned int,long)' : member function already defined or declared好像是你没有注意一点:类中的回调函数前面要加static 或者你应该把回调函数声明为全局函数。
      

  9.   

    在vc里面是不是这样声明回调函数不行?LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
      

  10.   

    我知道的楼主的意思,我给楼主提供一个函数,
     
    void Printf(const char *format, ...)
    {
    #ifdef _Console_
        static char
        trace_text [500];
    DWORD consolelen;
        va_list argptr;                     /*  Argument list pointer            */
        va_start (argptr, format);      /*  Start variable args processing   */
        vsprintf  (trace_text, format, argptr);
        va_end (argptr);               /*  End variable args processing     */ if(trace_text && *trace_text)
    {
    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
       trace_text,
      strlen(trace_text),
      &consolelen,
      NULL);
    }
    #endif
    }///// WriteConsole函数可以生成一个控制台的窗口,伴随着你的应用程序窗口,你的调试信息 可以在这个窗口打印出来,
    注意#ifdef _Console_ 预编译,不用这个函数的时候,将_Console_ 取消记得
      

  11.   

    谢谢各位,LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;是不是只能作为整个程序的一个全局函数?不是能放在我的debug类里面?难道没有办法把创建这个窗口用到的所有信息和函数都放到我定义的debug类中?谢谢!
      

  12.   

    很复杂啊,MFC可是用尽了方法来实现Debug的
      

  13.   

    主要我是希望这个debug数据不只在vc下可以输出,最好在程序编译好后仍然能够输出,这样当我的程序运行出现问题的时候,我就可以知道问题出在哪里
      

  14.   

    "谢谢各位,LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;是不是只能作为整个程序的一个全局函数?不是能放在我的debug类里面?难道没有办法把创建这个窗口用到的所有信息和函数都放到我定义的debug类中?谢谢!"不是告诉你了吗?
    1、类中的回调函数前面要加static (<----这儿)
    2、或者你应该把回调函数声明为全局函数。
      

  15.   

    而且,你在文件中定义了LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;只要把文件包含过去,就是整个工程的全局函数了啊。