我想将对话框窗口按自定义的窗口类名来注册,以便在其他程序中用FindWindow来找到窗口,
但是不知道如何实现自定义窗口类的注册。对话框资源里Class Name是灰色的,不能修改;我重载PreCreateWindow(CREATESTRUCT& cs)函数,写
cs.lpszClass = "MyWindowClass";
return CDialog::PreCreateWindow(cs);
发现这两行代码根本不会运行;重载OnCreate(LPCREATESTRUCT lpCreateStruct)函数,写
lpCreateStruct->lpszClass = "MyWindowClass";
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
也不起作用;重载CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
 DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam)
也不对,窗口是调用Create函数创建的,以为这个函数可以指定资源ID,而CreateEx似乎不行。我该怎么做呢?求助。BTW:我的编程环境是Win2kPro + VS.NET2003

解决方案 »

  1.   

    http://www.ddvip.net/program/vc/index2/43.htm
      

  2.   

    Thanks!
    你指示的文章我看了,我照着做了,但是PreCreateWindow(CREATESTRUCT& cs)函数没有
    预期执行,这是为什么呀?
      

  3.   

    PreCreateWindow是一般窗口用来注册窗口类用的。对话框的窗口类是己经注册过的(类名为#32770),所有对话框类不再调用PreCreateWindow.
    see 
    MSDN:
    HOWTO: Provide Your Own Window Class Name for an MFC Dialog Box
      

  4.   

    10 members have rated this article. Result:      
     
    Popularity: 4.54. Rating: 4.54 out of 5. 
     
     
    Download demo project - 23.9 Kb 
    Download sample application - 14.3 Kb 
    Introduction
    You are here because you consider yourself a rookie and because you want to get a few pointers on how to create a custom control from a generic CWin. If you have already built a control from scratch, there will be little or nothing of interest here. The contents of this article are introductory and purely didactic. The only goal is to address the question "How on Earth could one begin doing something like that?"That said, now follows a warning. It is often difficult to justify replacing an existing control for one of your own making. CListBox, for instance, comes packed with functionality, and I recommend anyone interested in customization to subclass it, adding and/or removing functionality as needed. The Code Project has plenty of articles that will allow you to include icons, have multi-line text, provide checkboxes and tooltips, you name it. Pretty much anything one might dream of is already here. Perhaps missing are hacks to draw or place bitmaps in the background, and a way to retain the standard scrollbars while modifying their look. If you know of these, please post them.Direct acknowledgment must be given to Chris Maunder for his article "Creating Custom Controls" as one must go over the same first steps when creating a control from scratch, and I have followed his here because they are explained with pristine clarity. I have also borrowed and modified code for the gradient background from norm.net's article "Extended Use of CStatic Class - CLabel 1.6". Over time, I have read and learned from many of the contributions posted at The Code Project, so I now take the opportunity to acknowledge you all here, thank you people.What Is to Be Accomplished
    The target is a horizontal listbox with single selection functionality that allows both default and individual coloring for each item. The border color is to be user-defined and the background is to be drawn using a color gradient. The scrollbar is to have four button-like divisions and a center display: shift to the leftmost column, shift one column to the left, display the number of elements in the listbox, shift one column to the right, and shift to the rightmost column.Only those items that are visible will be drawn in the listbox, thus making it possible to add an unlimited number of items (memory permitting). This will reduce display time for large amounts of data (CListbox can appear to hang if there are many items), and will bypass, in the process, the - granted - huge limit that CListbox has regarding the maximum number of items that can be added to it.On with the coding. Step-by-Step Procedure
    Steps 1 through 8 carry out the basic steps to get a custom control up and running, nothing more. Once compiled, the application will run but nothing will be visible in the custom control area.Step #1. Create a new class derived from CWnd:Class Type: MFC Class. 
    Class Information/Name: CCWinListBox. 
    Class Information/Base class: generic CWnd. 
    (All else left to defaults.)Step #2. We will now register the new window class we intend to create. First, add the following line to "CWinListBox.h":#define C_CUSTOMLISTBOX_CLASSNAME _T("MFC_CCWinListBox")
    Step #3. Then, add the following protected method declaration to "CWinListBox.h":BOOL RegisterWindowClass();
    Step #4. Last, add the implementation of RegisterWindowClass (below) to "CWinListBox.cpp" and call it from the constructor. Note that the cursor IDC_CURSOR1 - the "hand" cursor in this example project - has to be added as a resource. You can substitute it for exactly whatever calls your fancy. Alternatively, you can set the field wndcls.hCursor to NULL and the standard cursor will be used instead.BOOL CCWinListBox::RegisterWindowClass()
    {
        WNDCLASS  wndcls;
        HINSTANCE hInst = AfxGetInstanceHandle();    if( !( ::GetClassInfo( hInst, C_CWINLISTBOX_CLASSNAME, 
                          &wndcls ) ) ) // Already registered?
        {
            wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
            wndcls.lpfnWndProc      = ::DefWindowProc;
            wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
            wndcls.hInstance        = hInst;
            wndcls.hIcon            = NULL;
            wndcls.hCursor          = AfxGetApp()->LoadCursor( IDC_CURSOR1 );
            wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
            wndcls.lpszMenuName     = NULL;
            wndcls.lpszClassName    = C_CWINLISTBOX_CLASSNAME;        if( !AfxRegisterClass( &wndcls ) )
            {
                AfxThrowResourceException();
                return FALSE;
            }
        }    return TRUE;
    }
      

  5.   

    MSDN中的那个例子基本上恰恰是我需要的,只有一个意外令我感到非常郁闷:在编辑资源时死活不让
    我修改Class Name那一项,而且工程属性里也找不到Enable MFC Features复选框,VC6里是有的,
    VC2003就是找不到,不知道是不是被微软的工程师遗漏掉了。
    最后是强制在Resource.h里添加一行
    #define _APS_NO_MFC 1
    就可以修改对话框资源的Class Name了。// thaks and bow