Results 1 to 7 of 7

Thread: Creating Custom Controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    26

    Creating Custom Controls

    I would like like to create my own control class like an EDIT, STATIC, etc. I don't want it to be derived from an MFC. In short, I want to do something to this effect:

    MYCLASS i;
    RegisterClassEx(&i);
    CreateWindow(...);

    As you can do with other things. Is this possible?


    VC++ 6.0 SP5
    Cartethus Artraze
    Visual Studio 6 (SP3)
    http://www.angelfire.com/pa3/cartethus
    "The ideal answer comes only from the ideal question."

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep. You can - however it will only work as long as whatever registered it is running, which means you need to put it into a DLL (similar to the MS common controls).

    What happens is, you have a function in your DLL to actually register the class(es), and then you can use them subsequently...Windows will automatically reclaim the classes when the last user of the DLL terminates (anyone, correct me if I'm wrong because I think 9x and NT differ here).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    26
    How do you create the class and display it?

    Also, when you say "whatever registered it is running", does that mean my program, or only a part of it? I imagine it would mean the former, but I'm just making sure.
    Cartethus Artraze
    Visual Studio 6 (SP3)
    http://www.angelfire.com/pa3/cartethus
    "The ideal answer comes only from the ideal question."

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Okay. As far as it goes...

    1. Your program loads, implicitly loading the DLL
    2. You call a function in that DLL to register the window classes
    3. Your program creates windows using those classes
    4. Your program closes
    5. The DLL is implicitly unloaded, and it then unregisters the classes

    If you had more than one program running using the same classes, then step 5 would be postponed until ALL its users had terminated.

    Just for maximum confusion, this is from MSDN:
    Windows 95: All window classes registered by a .dll are unregistered when the .dll is unloaded.

    Windows NT/2000 or later: No window classes registered by a .dll registers are unregistered when the .dll is unloaded.
    So, it's safer to unregister them afterwards since the 9x series is close to death.

    The function in the DLL would call RegisterClassEx, and then your program would use CreateWindow.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    26
    Ok, I understand. Thanks.

    Now, how would I create the control?

    Right now I do:
    (This is a normal C++ project, so I can test)
    Code:
    WNDCLASS myw;
    
    //here I set up the main window
    
    myw.style= CS_HREDRAW | CS_VREDRAW;
    myw.lpfnWndProc=(WNDPROC)MYW;
    myw.cbClsExtra=0;
    myw.cbWndExtra=0;
    myw.hInstance=hInstance;
    myw.hIcon=NULL;
    myw.hCursor=LoadCursor(NULL, IDC_ARROW);
    myw.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    myw.lpszMenuName=NULL;
    myw.lpszClassName=(LPCTSTR)"MYW";
    
    if (!RegisterClass(&myw)) return FALSE;
    
    //in main windows's WinProc for WM_CREATE:
    CreateWindow("MYW", "App", WS_CHILD | WS_VISIBLE, 100, 100, 100, 100, hWnd, (HMENU)IDC_MYW, hInst, NULL);
    
    //in myv's WinProc: (for WM_CREATE)
    hDC=GetDC(hWnd);
    hBrush=CreateSolidBrush(RGB(0,255,0));
    hRgn=CreateRoundRectRgn(0,0,100,100,5,5);
    FillRgn(hDC,hRgn,hBrush);
    DeleteObject(hRgn);
    DeleteObject(hBrush);
    ReleaseDC(hWnd,hDC);
    However, the green rectange doesn't show up. Am I doing this right?
    Cartethus Artraze
    Visual Studio 6 (SP3)
    http://www.angelfire.com/pa3/cartethus
    "The ideal answer comes only from the ideal question."

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You need to create the control on the window after the CreateWindow for the window has returned, not in WM_CREATE since you can't guarantee whether it will manage to create the window or not.

    And the display should be in WM_PAINT, not WM_CREATE:
    Code:
    PAINTSTRUCT ps;
    HBRUSH hBrush, hOldBrush;
    HREGION hRegion;
    
    BeginPaint(hDC, &ps);
    
    hBrush = CreateSolidBrush(RGB(0,255,0));
    hRgn = CreateRoundRectRgn(0,0,100,100,5,5);
    
    hOldBrush = SelectObject(hDC, hBrush);
    FillRgn(hDC,hRgn,hBrush);
    SelectObject(hDC, hOldBrush);
    
    DeleteObject(hRgn);
    DeleteObject(hBrush);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    26
    Thanks, that worked.

    I'm just curious, what are the SelectObject calls doing in this part of the code:

    hOldBrush = SelectObject(hDC, hBrush);
    FillRgn(hDC,hRgn,hBrush);
    SelectObject(hDC, hOldBrush);
    Cartethus Artraze
    Visual Studio 6 (SP3)
    http://www.angelfire.com/pa3/cartethus
    "The ideal answer comes only from the ideal question."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width