Drawing in MFC

This is a discussion on Drawing in MFC within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I had a question as to how to have a drawing window and the other text windows and stuff ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Drawing in MFC

    Hello, I had a question as to how to have a drawing window and the other text windows and stuff in MFC. Basically I want to use the Resource editor to create my display. I want to have lists, text box's, etc, but I also want a box where I can display simple drawings. The drawing would be done using those DrawRectangle type functions, I don't want to load a picture of any kind. Which type of control should I use to draw? None of them seem like I can draw in them.

    I suppose I could create a list box or text box or something, get the client dc, and try to use the draw rectangle functions with that (assuming that works), but I dont want people typing text or selecting lists or anything. How do I get just a blank white background for a control?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    Use a "Picture" control in the resource editor. Once the control is sized and placed in your dialog, go the its properties and give the control its own ID. Set "Type" to "Rectangle" and "Color" to "White". On the "Extended Styles" tab, give it a "Client edge".

    Go to Class Wizard->Member Variables tab. Select the Control ID you just added and hit "Add Variable". Set Category to "Control" and the Variable type will be CStatic.

    You can then call GetDC() on that CStatic and draw away.

    gg

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Drawing

    Hmm, I did what you suggested and it creates the white box just like I want. Then I do something like this:

    Code:
    CDC *myDC = GetDlgItem(IDC_DRAWBOX)->GetDC();
    myDC->Rectangle(10, 10, 20, 20);
    And it doesn't seem to draw anything. If I overwrite the view's OnDraw function, I can do something like this:

    Code:
    void CDrawView::OnDraw(CDC* pDC) 
    {
    	pDC->Rectangle(10, 10, 20, 20);	
    }
    And it draws on the main window. But I want to draw in the picture box. But using the picture box's dc doesn't seem to draw anything. Is there some update function or something I'm supposed to use? Or some way to make the pDC draw in the correct spot?
    -Grunt (Malek)

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    hmmm, looks like I lied to you...my MFC is getting rusty.

    Here's what you can do:
    - don't use any control and in the CDialog::OnPaint() draw right in the dialog itself
    - or you can still use the previous approach I mentioned, except you'll have to create your own CStatic extension and do your drawing in it's OnPaint(). You can also use a static text box as the the control, it doesn't have to be a "picture" control.

    I actually tried my own suggestions this time - one of these will work for you.

    gg

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Another question

    I'm having trouble figuring out how to do your second suggestion. I'm trying to create my own custom class on the dialog, but the program keeps giving me the error "Failed to create empty document". Here's what I did, and if you can tell me what I did wrong and how to fix it, that'd be great.

    I created a new FormView mfc document, and in the resource editor put a "Custom Control" on there. I went to the Class Wizard and hit the "New Class" button, and created a new control of base type CView, called MyDrawControl (I tried one of base type CStatic, but it wouldn't let me overwrite the OnDraw function, apparently CStatic's don't have one). I then went back to the resource editor, selected my custom control, and under the "Class" option, typed in MyDrawControl. Then I went to run it, and it gave me that error before displaying anything.

    I tried a CEdit base type for my class too, incase it didn't like my using CView as my base type, but it gave me the same error. Is there another way I should be doing this, or do you see what I did wrong with this way? Thanks for the help.
    -Grunt (Malek)

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    I don't know about using a CView. All I did was add a new class, CPict, that extended CStatic, then mapped WM_PAINT in class wizard which will give you an OnPaint() method.
    Code:
    void CPict::OnPaint() 
    {
        CPaintDC dc(this);
    
        dc.SelectStockObject(BLACK_BRUSH);
        dc.Rectangle(0,0,10,10);
    }
    Then in class wizard for the dialog, I added a new member variable for my IDC_PICTURE control and made it type CPict. I tried both a picture control and a static text box - both had a 10x10 black box in it

    I've attached my project.

    gg
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Thanks :)

    Alright, got it working. Thanks for the help.
    -Grunt (Malek)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing, rubber banding in a dialog (MFC)?
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 02-04-2009, 07:00 AM
  2. Drawing in a dialog box with MFC
    By Gravedigga in forum Windows Programming
    Replies: 3
    Last Post: 01-03-2004, 10:01 PM
  3. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  4. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM
  5. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21