Re: Help Drawing into a Dialog
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sun, 22 Jun 2008 20:04:43 -0400
First thing: you should NEVER "draw into" a dialog. Right off, that's a design error.
If you want to draw something, you create a CStatic control that holds the drawing, and
you do the drawing in its OnPaint handler.
Therefore, there is no reason to "draw" on a dialog except in VERY rare and exotic
circumstances.
On Tue, 17 Jun 2008 19:32:26 -0700 (PDT), Peter <pvrequiz@xxxxxxxxx> wrote:
Hello,****
What I have done now is simple draw a BITMAP into a dialogue
windows(adding a variable ellipse), it works fine, it display and I
can change the position of the ellipse in the map but I can not erase
the previous drawing, I think it is something because the
CDialog::OnInitDialog() is is not a part of the Dlg class, isn't it? I
am not sure what is going on here. I think I should be able to reset
the map only calling the CDialog::OnInitDialog() routine. But it doest
not work.
Thank you for any help here,
Regards,
Here is the code on *Dlg.cpp
// BitMapInDIALOGDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BitMapInDIALOG.h"
#include "BitMapInDIALOGDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBitMapInDIALOGDlg dialog
CBitMapInDIALOGDlg::CBitMapInDIALOGDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBitMapInDIALOGDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBitMapInDIALOGDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in
Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBitMapInDIALOGDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBitMapInDIALOGDlg)
DDX_Control(pDX, IDC_PICTURE, m_Picture);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBitMapInDIALOGDlg, CDialog)
//{{AFX_MSG_MAP(CBitMapInDIALOGDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SubmitBtn, OnSubmitBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBitMapInDIALOGDlg message handlers
BOOL CBitMapInDIALOGDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this
automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
BITMAP BM;
BITMAP BMCLE; //for clear map no markrs
Bmp1.LoadBitmap (IDB_BITMAP1);
Bmp1CLE.LoadBitmap (IDB_BITMAP2);//for clear map no markrs
Bmp1.GetObject (sizeof (BM), &BM);
Bmp1CLE.GetObject (sizeof (BMCLE), &BMCLE);//for clear map no markrs
m_BitmapWidth = BM.bmWidth;
m_BitmapWidthCLE = BM.bmWidth;//for clear map no markrs
m_BitmapHeight = BM.bmHeight;
m_BitmapHeightCLE = BM.bmHeight;//for clear map no markrs
/* CDC MemDC;
CDC MemDCCLE; //for clear map no markrs
Immediately, it is obvious that this code is completely wrong.
You would NEVER draw anything in the OnInitDialog, for any reason, at any time, for any
purpose (OK, I'm sure there is some completely obscure reason this might make sense in
some bizzare application, but it is hard to imagine an application this weird).
So all of the code starting with the declaration BITMAP and ending with the Ellipse call
is completely wrong.
You MIGHT consider doing it in the OnPaint handler, but I think this whole design is
wrong. Create a CStatic and do the drawing in that.
****
//RECT ClientRect;****
//RECT ClientRectCLE;//for clear map no markrs
// create memory device context object and select bitmap
// object into it:
MemDC.CreateCompatibleDC (NULL);
MemDCCLE.CreateCompatibleDC (NULL);//for clear map no markrs
MemDC.SelectObject (&Bmp1);
MemDCCLE.SelectObject (&Bmp1CLE); //for clear map no markrs
int x11 = xposition - 6;
int y11 = yposition - 6;
int x22 = xposition + 6;
int y22 = yposition + 6;
MemDC.Ellipse (x11, y11, x22, y22);*/
m_Picture.SetBitmap(Bmp1);
SetDlgItemText(IDC_EDIT1x,"194");
SetDlgItemText(IDC_EDIT2y,"250");
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code
below
// to draw the icon. For MFC applications using the document/view
model,
// this is automatically done for you by the framework.
int xposition = 194;
int yposition = 199;
void CBitMapInDIALOGDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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 = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
// CPaintDC dc(this); // device context for painting
Commenting this out is definitely a mistake. A CPaintDC *must* be declared in the OnPaint
handler!
****
****
// Bmp1.LoadBitmap(IDB_BITMAP1);
CDC MemDC;
CDC MemDCCLE; //for clear map no markrs
// RECT ClientRect;
// RECT ClientRectCLE;//for clear map no markrs
// create memory device context object and select bitmap
// object into it:
MemDC.CreateCompatibleDC (NULL);
MemDCCLE.CreateCompatibleDC (NULL);//for clear map no markrs
MemDC.SelectObject (&Bmp1);
MemDCCLE.SelectObject (&Bmp1CLE); //for clear map no markrs
int x11 = xposition - 6;
int y11 = yposition - 6;
int x22 = xposition + 6;
int y22 = yposition + 6;
MemDC.Ellipse (x11, y11, x22, y22);
It is not clear what value there is in drawing an ellipse in a memory DC that is never
used
****
****
MemDC.DeleteDC ();
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the
user drags
// the minimized window.
HCURSOR CBitMapInDIALOGDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBitMapInDIALOGDlg::OnSubmitBtn()
{
// TODO: Add your control notification handler code here
// This works
/*
int xe = 3445;
char buffer[20];
First rule: if you use char, your code is Obsolete As Written. If you declare fixed-sized
buffers for purposes of doing conversions like this, your code is dangerous. Why are you
doing something so convoluted to accomplish a simple task?
****
****
_itoa(xe, buffer, 10 );
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_EDIT1,buffer);
If you use GetDlgItem or SetDlgItemText, you are using very, very poor code techniques. If
you leave the silly IDC_EDIT1 names created by the dialog editor, you are learning how to
make your life miserable. Presumably this edit control serves some useful purpose, that
ought to be encoded in its name, such as IDC_COUNT, or IDC_LIMIT, or IDC_XPOSITION, or
something else useful. THEN, you create a control variable, such as c_Count, or c_Limit,
or c_XPosition, or something else that makes sense. Then you would do
CString s;
s.Format(_T("%d"), xe);
c_XPosition.SetWindowText(s);
so much easier, Unicode-aware, and it looks like MFC and not badly-written 1975 C code for
the PDP-11.
****
////////////////////////////////////////*/****
//xposition = atoi(strXcoordi1);
That would be _ttoi. Consider 'char' as an obsolete data type used only in very rare and
exotic circumstances, of which this is clearly not an example.
****
***
//yposition = atoi(strYcoordi1);****
//char buffer[20];
CString buffercharx;
CString bufferchary;
GetDlgItemText(IDC_EDIT1x,buffercharx);
GetDlgItemText(IDC_EDIT2y,bufferchary);
Think of GetDlgItemText as a completely obsolete method that is not used in modern
programming.
****
xposition = atoi(buffercharx);****
yposition = atoi(bufferchary);
CDialog::OnInitDialog();
What does this line do? CDialog::OnInitDialog was called back at the beginning, and
calling it a second time is likely to cause damage.
****
****
CBitMapInDIALOGDlg::OnPaint();
What is this line supposed to do? Certainly it cannot possibly cause any painting,
because there is no painting context possible.
****
****
Invalidate();
What does this line do? You are in OnInitDialog. The dialog has not even been shown yet,
so there is no point in invalidating it.
****
}Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Help Drawing into a Dialog
- From: Peter
- Help Drawing into a Dialog
- Prev by Date: Re: CString and embedded 0s
- Next by Date: Re: Different Button Position When On Design Screen and Applicatio
- Previous by thread: Re: Help Drawing into a Dialog
- Next by thread: How to use C# DLL in MFC
- Index(es):
Relevant Pages
|