Drawing lines in MFC VS2010 and VC++6.0 doesn't get the same result - visual-c++

I have been learning MFC these days.I want to draw lines with MoveTo() and LineTo() functions both in VC++6.0 and VS2010,but it seems that it does not work in vs2010.I only add two windows message handler,WM_LBUTTONDOWN and WM_LBUTTONUP,in the single document project.
Here is the code in VC++6.0:
CPoint m_ptOrign;
void CStyleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrign=point;
CView::OnLButtonDown(nFlags, point);
}
void CStyleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.MoveTo(m_ptOrign);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
Here is the code in vs2010:
CPoint m_ptOrign;
void CStyleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrign=point;
CView::OnLButtonDown(nFlags, point);
}
void CStyleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.MoveTo(m_ptOrign);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
The codes I add to the two projects are the same.When I release the left button ,the line appears immediately in the vc++6.0 project,but it doesn't appear in the vs 2010 mfc project.
If the size or location of the window of the vs 2010 project changes,the line apprears.
But when I use dc.Rectangle(CRect(m_ptOrign,point)) in the vs 2010 project ,it works well.
I do not know why.....
What's more,if I use
CBrush *pBbrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBbrush);
dc.Rectangle(CRect(m_ptOrign,point))
in vs2010,it does not work again,like the drawing line case

LineTo is going to use the pen that is currently selected into the DC. Since you haven't selected a pen, it will use whatever is the default. I don't know why that would be different between VC6 and VC2010, perhaps it has something to do with the differences in MFC between the two versions.
In general it's a bad idea to grab a DC and start drawing on it. Better is to do all your drawing in the OnPaint or OnDraw methods. You can call InvalidateRect to cause a paint message to be sent to the window.

Related

Something for a child window analogous to `this`

I am a newcomer to programming. I am writing a dialog based application which has a static control on it. Using
Using
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (this == GetCapture())
{
CClientDC aDC(this);
aDC.SetPixel(point, RGB(255,0,0));
}
}
I can create results like
However what I want is that the locus of mouse is only drawn within the static window. I can't find the reference to this in MSDN and I don't know why the following method fails.
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pMYSTATIC = GetDlgItem (IDC_MYSTATIC); //IDC_MYSTATIC is the ID of the static control
if (pMYSTATIC == GetCapture())
{
CClientDC aDC(pMYSTATIC);
aDC.SetPixel(point, RGB(255,0,0));
}
}
How can I get what I want? Are there any methods to get something for the static window analogous to this? I will appreciate any help with these.
OK, try this:
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rect;
// Get static control's rectangle
GetDlgItem(IDC_MYSTATIC)->GetWindowRect(&rect);
// Convert it to client coordinates
ScreenToClient(&rect);
// Check if mouse pointer is inside the static window, if so draw the pixel
if (rect.PtInRect(point))
{
CClientDC dc(this);
dc.SetPixel(point.x, point.y, RGB(255,0,0));
}
}
This code may need some fixes too, eg shrink the rectangle (to its client-only area), before checking whether to draw the pixel.
Please note that you don't need to check GetCapture(); if your dialog hasn't captured the mouse it won't be receiving this message anyway.
Also, all these functions are wrappers of Windows SDK ones, eg the ClientDC() class, basically wraps GetDC()/ReleaseDC().

Selecting content of CEdit control in MFC when clicking the control

How could I select the content of CEdit control just when I click the text of the CEdit.
I could select the content with this code:
m_ctrlEdit.SetFocus();
m_ctrlEdit.SetSel(0, -1, FALSE);
and i put the code in ON_EN_SETFOCUS message handler, but the code doesn't work there.
Create a custom CEdit control and in the custom class add the handler OnLButtonDown in that put the following code
void CMyEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnLButtonDown(nFlags, point);
SetSel(0, -1, FALSE);
}

onTouchListener doesn't get called when dragging

I have red and green LinearLayouts, and I set ontouch listeners to them.
When I touch the green one its turns to white (no problem so far)
When I touch the red one it is also turning to white (no problem so far)
The problem is when I touch the green one and I drag my finger over the red one (meaning I do not release the finger I just move it over the red one) then in this kind of scenarion only the green one go to white but not the red one.
I hope I describe good, if you do not get my problem please ask me.
Thanks for the help
green.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.WHITE);
return true;
}
});
red.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.WHITE);
return true;
}
});
This is because the touch event started on the Green square and so moving your finger will only call touch events on the Green square. You could try to send a touch event to the red square within the code. But there is no standard way of doing it i think.

how to create a line between two clicks using menu in vc++

I am trying to create a paint application where in i am using a menu to show the shapes i can draw and i have added some shapes such as line,rectangle,circle but i am unable to use the OnLButtonDown function within the line function so i would like to know how can i create a line between two clicks by not using LButtonDown function and any other way by which i can create a line when user clicks on the menu my sample code is pasted below:
#include<afxwin.h>
#include"resource.h"
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"simple",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}
void shape(int id)
{
CClientDC d(this);
CPen p;
p.CreatePen(PS_SOLID,1,RGB(255,0,0));
d.SelectObject(&p);
switch(id)
{
case 101:
d.MoveTo(100,100);
d.LineTo(200,200);
break;
case 102:
d.Rectangle(10,10,100,200);
break;
case 103:
d.Ellipse(20,20,100,100);
break;
}
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND_RANGE(101,103,shape)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *f;
f=new myframe();
f->ShowWindow(3);
m_pMainWnd=f;
return 1;
}
};
myapp a;
Several years ago, Microsoft published a sample application written in MFC that does exactly this.
It's called Scribble, and you can download it and following along with the written tutorial here.
Once you've done that, you can learn how to upgrade an existing MFC application to use the new Ribbon user interface. That tutorial is available here: Updating the MFC Scribble Application

MFC menu item checkbox behavior

I'm trying to add a menu item such that it acts like a check mark where the user can check/uncheck, and the other classes can see that menu item's check mark status. I received a suggestion of creating a class for the menu option (with a popup option), however, I can't create a class for the menu option when I'm in the resource layout editor in Visual Studio 2005. It would be great to hear suggestions on the easiest way to create menu items that can do what I have described.
You should use the CCmdUI::SetCheck function to add a checkbox to a menu item, via an ON_UPDATE_COMMAND_UI handler function, and the ON_COMMAND handler to change the state of the checkbox. This method works for both for your application's main menu and for any popup menus you might create.
Assuming you have an MDI or SDI MFC application, you should first decide where you want to add the handler functions, for example in the application, main frame, document, or view class. This depends on what the flag will be used for: if it controls application-wide behaviour, put it in the application class; if it controls view-specific behaviour, put it in your view class, etc.
(Also, I'd recommend leaving the menu item's Checked property in the resource editor set to False.)
Here's an example using a view class to control the checkbox state of the ID_MY_COMMAND menu item:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
afx_msg void OnMyCommand();
afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
ON_UPDATE_COMMAND_UI(ID_MY_COMMAND, OnUpdateMyCommand)
END_MESSAGE_MAP()
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
// Use the new flag value.
}
void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_Flag);
}
You should ensure the m_Flag member variable is initialised, for example, in the CMyView constructor or OnInitialUpdate function.
I hope this helps!
#ChrisN's approach doesn't quite work for MFC Dialog applications (the pCmdUI->SetCheck(m_Flag); has no effect). Here is a solution for Dialog apps:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
CMenu * m_menu;
virtual BOOL OnInitDialog();
afx_msg void OnMyCommand();
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
END_MESSAGE_MAP()
BOOL CMyView::OnInitDialog()
{
m_menu = GetMenu();
}
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
if (m_flag) {
m_menu->CheckMenuItem(ID_MENUITEM, MF_CHECKED | MF_BYCOMMAND);
} else {
m_menu->CheckMenuItem(ID_MENUITEM, MF_UNCHECKED | MF_BYCOMMAND);
}
}
References:
http://www.codeguru.com/forum/showthread.php?t=322261
I ended up retrieving the menu from the mainframe using GetMenu() method, and then used that menu object and ID numbers to call CheckMenuItem() with the right flags, as well as GetMenuState() function.

Resources