dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [MFC] SDI based on ViewForm - How to create different forms according to the user log

  1. #1
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    [MFC] SDI based on ViewForm - How to create different forms according to the user log

    Dear All,

    I am trying to understand how to create SDI based on ViewForm class that has a different dialog in the mainframe according to the type of user that has logged.

    I have been playing our this afternoon and noticed that when you click on the ".rc" file in the resource folder you can create add a new dialog. Then I went to the view class and adding to his enum the token identifying the dialog ID.

    (Something like "enum{IDD = IDD_DIALOGTEST_FORM, IDD1 = IDD_FORMVIEW1, IDD1 = IDD_FORMVIEW2}")


    After this I have modified the constructor method of the myview class changing the paramenter that gives to the CFormView upper class.

    (Something like "CDialogTestView::CDialogTestView()::CFormView(IDD1){}")

    The goal I wanted to achieve was to change the token basing on the login and for this I tried to call the super constructor from the constructor method body:

    (Something like "CDialogTestView::CDialogTestView(){
    if(login==1){
    CFormView(IDD1);}
    else{CFormView(IDD);}
    }")



    Unfortunately it gives me the following error at compile time:
    error C2512: 'CFormView' : no appropriate default constructor available

    I was wondering if anyone know already a better way of doing this and if MICROSOFT has already implemented a class or method to do so.

    Thanks for ur attention and Best Regards!

    PS: If interested please find the source file and the project for VS 2008 at
    http://www.2shared.com/file/12618099...ialogTest.html

    Size approx 6.6 MB

  2. #2
    Join Date
    Nov 2007
    Posts
    613

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    You did it almost right. Try this:

    CDialogTestView::CDialogTestView()
    : CFormView((login == 1) ? IDD_DLG1 : IDD_DLG2)
    {
    }

    Note that you cannot use just any kind of dialog, there are some restrictions. To have a second dialog compliant with the requirements, make a copy of the dialog created by the wizard.

  3. #3
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    Thank you very much.. so basically I can use a tree of conditions in this way and choose between 2 or more users. That's good. I am kinda of surprised that there isn't an MFC standard class or method to do so.. but happy to have fixed that.

    Quote Originally Posted by srelu View Post
    Note that you cannot use just any kind of dialog, there are some restrictions. To have a second dialog compliant with the requirements, make a copy of the dialog created by the wizard.
    Regarding the quotation above I have not clear what you mean exactly. Do you refer to the dialog class created by the App Wizard at the very beginning when creating the application?

    Isn't there a risk by just copying the dialog? I mean.. when using VS the dialogs are created but often there are macros and other pieces of code added in other classes, if we copy only we loose the dependencies..

    To create a new dialog I went to the ".rc" file and added the dialog as new resource.. is this sound to u guys?

    thanks again!

  4. #4
    Join Date
    Nov 2007
    Posts
    613

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    I simply answered your question. They way you're doing the job is not the standard one. Normally you should create multiple views for the same document and activate the one you need.

    Not going on the standard way means that nobody can tell you for sure that everything will be ok.

    But, it would be highly unusual for a parent class to access objects from the derived class. Anyway the parent class receives the dialog identifier trough the constructor.

    I even checked the mfc sources, there was nowhere any IDD (except for some classes derived from CDialog - like your form view class).

    Yes indeed I was talking about the dialog created by the app wizard.
    When I told you that I made a copy I meant that the dialog requires some features to work properly (the WS_CHILD style). Instead of verifying all the styles, I preferred the easier way, a copy.

    If you don't have a resource editor, modifying manually the rc file is the right way to add resources (dalogs).

  5. #5
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    thank you..

    I am sorry to bother you but I am totally new to MFC programming.. I am trying to get my head around it but is a bit confusing sometimes.

    Ok then, I will do as according to the standard way.. I do not want to re-invent the wheel.

    Basically you are telling me to create different views. I am not sure how to do it but I will list the steps I would do, please correct me if I am wrong:

    1 - create an SDI project using VS App wizard
    2 - create a new view: ? what does this mean -
    - is it correct to add a new resource choosing VIEW and then adding a class to the view?

    Once createad the class I do not know how to link it with the mainframe.. how would you do that?

    I am sorry for asking that, but I realize that I probably need a step by step tutorial to do that.. I would be grateful for any help.. thanks!

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,602

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    Normally you should create multiple views for the same document and activate the one you need.
    I would say, it depends on the nature of the issue that we're not aware of. Why another user has to have another kind of dialog? How different the dialogs are? If the difference is sufficient enough, it might be better to create different document as well to correspond their views more accurate.
    Best regards,
    Igor

  7. #7
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    Ok well..
    I am working on SDI documents since the views are supposed to be similar...

    could anyone contribute with a step by step explaination of my question above? If not nevermind and thanks anyway!

  8. #8
    Join Date
    Nov 2007
    Posts
    613

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    Quote Originally Posted by pengCC View Post
    2 - create a new view: ? what does this mean -
    .........................

    Once createad the class I do not know how to link it with the mainframe.. how would you do that?
    http://msdn.microsoft.com/en-US/libr...(v=VS.80).aspx

    Quote Originally Posted by pengCC View Post
    - is it correct to add a new resource choosing VIEW and then adding a class to the view?
    Yes. You must add first the dialog resource, then add the class.

    I don't know what Visual Studio do you use.
    In the case you use an Express Edition, well I never used one, but as I heard it doesn't have a resource editor. If that's your case, your only option is to modify manually the *.rc file to add the dialog reesource.

    If you have a better VS, go to the menu click on View then click the menu intem named "Resource View". A window will open allowing you to browse resources, add, remove and edit them.

    After you added the dialog resource, you can add a class for it.

    1. How to add the class if you have a resource editor:

    1.1 Open the dialog box in the resource editor.
    1.2 Double click it's surface. The MFC Class Wizzard will pop up inviting you to add a class for that dialog box.
    1.3 Enter the name of the class and click OK.
    1.4 Done.

    2 How to add the class if you don't hve a resource editor:
    2.1 Clik the menu "Projects" item and then click "Add Class".
    2.2 From the dialog box that will pop up chose MFC class then click Add.
    2.3 In the next dialog:
    - add a name for your class in the "Class Name" box
    - In the "Base Class" combo pick "CFormView"
    - In the "Dialog ID" combo pick the identifier of your dialog box. (if you don't see it there make sure the base class is CFormView and check if the identifier is added to the resource.h file.)
    - Click "Finish"
    2.4 Done.

    For the rest, see the link I provided to you. As a beginer, you'll need to read and work a lot to understand the document-view architecture. Not to mention that the task you picked is not among the simplest.

  9. #9
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    Thank you for that, looks very helpful. I have started following your hints + the msdn tutorial but I got stuck..

    I followed step by step the MSDN tutorial but it does tell me only that I need to create a new class. Once I run the program it gives me the following errors:

    Compiling...
    1>MyWinApp.cpp
    1>c:\documents and settings\pengCC\desktop\c# trials\mywinapp\mywinapp\mywinapp.cpp(121) : error C2259: 'CNewView' : cannot instantiate abstract class
    1> due to following members:
    1> 'void CView::OnDraw(CDC *)' : is abstract
    1> c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h(4242) : see declaration of 'CView::OnDraw'
    etc..


    I believe that is cause the class is abstract and I need to declare all the methods.. the thing is that I do not know how to declare manually the massage map etc..

    That is what the AppWizard generates:

    #include "StdAfx.h"
    #include "NewView.h"

    CNewView::CNewView(void)
    {
    }

    CNewView::~CNewView(void)
    {
    }


    I have visual studio professional 2008.

    I will now try doing the same with a CFormView based project (instead of the CView based one) and see if I can get it working..

    Will keep u posted..

  10. #10
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    ...its still gives me a bunch of errors at compilation time.. following step by step the microsoft tutorial there are errors, trying to do what you suggested step by step as well.. something is definetely missing and I have been trying to figure it out..

    At the moment:

    1>Compiling...
    1>MyFormApp.cpp
    1>c:\documents and settings\pengCC\desktop\c# trials\myformapp\myformapp\myformapp.cpp(142) : error C2664: 'CFormView::Create' : cannot convert parameter 2 from 'const char [14]' to 'LPCTSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    etc..

    And something else that looks kinda of odd to me is that when I create a newContext the m_pLastView function returns only a view but there is not way to get a similar function to return a CFormView..

    CCreateContext newContext;
    newContext.m_pNewViewClass = NULL;
    newContext.m_pNewDocTemplate = NULL;
    newContext.m_pLastView = NULL;
    newContext.m_pCurrentFrame = NULL;
    newContext.m_pCurrentDoc = pCurrentDoc;

    I think that there is probably some problem with that..

    Anyone managed to implement the MSDN tutorial successfully?

    Thanks!

  11. #11
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] SDI based on ViewForm - How to create different forms according to the user

    ok I solved that..

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)