0

I wrote this code to draw a simple rectangle in a dialog , I also added ON_WM_PAINT() to my message map. but it didnt show anything on dialog to me ! I really appreciate it if anyone could tell my mistakes in code:

void Ctest4Dlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting
        // TODO: Add your message handler code here

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = 2;
        int y = 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);

        //I want to draw a rectangle 
        dc.Rectangle(10,10,50,50);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
Saeed Taheri
  • 41
  • 1
  • 4
  • 8

2 Answers2

5

Looks like your paint code only runs when the window is iconic? Why are you doing that?

Put it in the else block, after the call to CDialogEx::OnPaint().

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Your first and biggest mistake is trying to draw directly in a dialog. While it is possible to do so, it's almost always a bad idea. A dialog should usually be treated as a container for controls.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • A dialog's usefulness is based primarily on it's ability to contain controls. However, there's no reason why it's a bad idea to draw directly on it. If that's what is needed, what problem does it cause? – Jonathan Wood Jul 09 '11 at 05:41
  • @Jonathon: Drawing directly to a dialog seems to cause problems with doing the drawing dependably. I'm not the only one to see it either. For example, see a few [posts](http://groups.google.com/group/microsoft.public.vc.mfc/browse_frm/thread/88e61b2d5ae82e0a/ed8fe4997e4878e3?hl=en#ed8fe4997e4878e3) by Joseph Newcomer on the same subject. – Jerry Coffin Jul 09 '11 at 05:53
  • Hmm... I've done this from time to time and just haven't had any problems. Doesn't look like Joseph had any specific examples either. At any rate, for more drawing-intensive situations, I would put it to it's own child window. – Jonathan Wood Jul 09 '11 at 06:04
  • Dear Jerry, thanks for your comment. I am very very new to MFC in visual c++ , actually this is my first experience in Windows Programming.I dont know how to insert a custom control and how to define an event handler or how to add class to a control to draw some shapes, I am googling too much but the more I searched the more confuse I am. I really appreciate it if you explain a little to me. – Saeed Taheri Jul 09 '11 at 10:19
  • 1
    @Saeed: You need a book or an online tutorial to learn MFC, not Stack Overflow. It's far too complicated to pick up by asking a few questions here and there. And it's very likely that if you *do* manage to cobble together a program from copying and pasting some code snippets found around the Internet, you won't really know how you did it and why it works. Do yourself a favor and learn Win32 programming first. Charles Petzold's [Programming Windows](http://www.amazon.com/Programming-Windows%C2%AE-Fifth-Microsoft/dp/157231995X) is the canonical resource for this. – Cody Gray - on strike Jul 09 '11 at 10:47
  • 1
    And while I agree that drawing in a dialog is probably not the best approach, and that a subclassed `CStatic` control is a much better option, I don't really think that this is an "answer" to the question. It doesn't really explain why the code that he has is failing. I'd have probably posted it as a comment. @Jonathan's answer is correct, although it might be better if it went a little deeper in explaining the *right* way to do this. – Cody Gray - on strike Jul 09 '11 at 10:49
  • @Saeed: I have posted a [free example of a custom hex editor control](http://www.blackbeltcoder.com/Articles/controls/mfc-hex-editor-control). Unfortunately, it's a complex example and I don't have a simple one. Note that I consider this an advanced topic and this really is not a good task for someone new to MFC. – Jonathan Wood Jul 09 '11 at 14:20