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

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

Related

Can't get text info to member variable in form CDialog MFC dialog form

I have small dialog form with Edit Text control:
#include "stdafx.h"
#include "MyDlg3.h"
#include "afxdialogex.h"
// MyDlg3 dialog
IMPLEMENT_DYNAMIC(MyDlg3, CDialog)
MyDlg3::MyDlg3(CWnd* pParent /*=NULL*/)
: CDialog(MyDlg3::IDD, pParent)
, m_edit(_T(""))
{
}
MyDlg3::~MyDlg3()
{
}
void MyDlg3::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_edit);
}
BEGIN_MESSAGE_MAP(MyDlg3, CDialog)
ON_BN_CLICKED(IDOK, &MyDlg3::OnBnClickedOk)
END_MESSAGE_MAP()
// MyDlg3 message handlers
void MyDlg3::OnBnClickedOk()
{
txt=m_edit;
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
And I have simple application that calls this dialog:
BOOL CPreparationApp::InitInstance()
{
MyDlg3 Dlg3;
Dlg3.DoModal();
CString strLine0=Dlg3.txt;
return true;
}
I cant't find why I can't get text that was entered in dialogs Text Control to txt and strLine0 variables.
I found that if I make MyDlg3 from CDialogEx (not from CDialog like it is now) - everything goes fine. Where is problem?
The text is transferred between the control and the member variable in DoDataExchange. DoDataExchange is called when you do CDialog::OnOK as you can see in the documentation for UpdateData. By copying the value before then you're not getting any results. You could fix this by moving the copy after the call to OnOK, or you could skip the copy altogether as suggested by another answer and use the member variable directly.
It's the call to CDialog::OnOK() in MyDlg3::OnBnClickedOk() that sets up the member variable m_edit, so you can write logic like
MyDlg3 Dlg3;
Dlg3.DoModal();
CString strLine0 = Dlg3.m_edit;
while making sure that m_edit is declared as public.
Try this:
BOOL CPreparationApp::InitInstance()
{
MyDlg3 Dlg3;
if(Dlg3.DoModal()==IDOK);
{
CString strLine0=Dlg3.txt;
return true;
}
return false;
}

Switching to different UITableViewControllers with UISegementedControl

I've an UINavigationViewController with an UISegmentedControl in the navigation bar. I want to achieve a simple switching to different ViewControllers when users push on the segmented control.
I've tried a lot and nothing works... Considered sources:
MonoTouch Instantiating a ViewController programmatically for ContainerView
https://stackoverflow.com/search?q=viewcontroller+intptr+handle
And a lot of google research...
The whole project is storyboard based! Any solutions which targets NIB's aren't useful.
Adding a ContainerControl to my UINavigationViewController. But in this case I can only embed one controller. Creating a Embedded-Segue programmatically was not possible. Even more instantiating a UITableViewController in code which is designed in IB results in an empty view. Because I've to change the c'tor from MyTableViewController(IntPtr handle) : base(handle) to an empty constructor.
Can someone publish a working example how to use a UISegmentedControl to switch between different ViewControllers? I appreciate all your help very much.
Working solution:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
CreateAndEmbed (TrDetailNavType.Info);
}
partial void segmentNavigationValueChanged (MonoTouch.UIKit.UISegmentedControl sender, MonoTouch.UIKit.UIEvent e)
{
CreateAndEmbed((TrDetailNavType)sender.SelectedSegment);
}
private void CreateAndEmbed(TrDetailNavType tab)
{
if (_currentViewController != null)
{
_currentViewController.View.RemoveFromSuperview ();
_currentViewController.RemoveFromParentViewController();
}
string id;
switch (tab)
{
case TrDetailNavType.Info:
id = "TagesRapportDetailInfoTableViewController";
break;
case TrDetailNavType.Lohn:
case TrDetailNavType.Material:
case TrDetailNavType.Inventar:
case TrDetailNavType.Fremdleistung:
case TrDetailNavType.Regie:
id = "TagesRapportDetailDummyViewController";
break;
}
_currentViewController = (UIViewController)Storyboard.InstantiateViewController (id);
_currentViewController.View.Frame = containerDetail.Bounds;
AddChildViewController (_currentViewController);
_currentViewController.DidMoveToParentViewController (this);
containerDetail.AddSubview (_currentViewController.View);
}

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

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.

How to access the Keyboard in an Eclipse RCP / LWJGL app?

I am working my way through the NeHe OpenGL examples, using the LWJGL for the OpenGL binding inside an Eclipse RCP application.
My OpenGL graphics are displayed inside the RCP canvas, not in a separate window.
Lesson 07 shows how to use the keyboard. If I try to do a:
Keyboard.create();
I get an error that the (OpenGL) "Display" has not been created.
If I create an OpenGL "Display" with org.lwjgl.opengl.Display.create(), then I get a new Window.
So how do I access the Keyboard without creating a new Window?
You cannot use the Keyboard without a Display, because of how LWJGL works behind the scenes. The best way is to just use AWT events. You can write your own input class, that could go something like this.
public class Input implements KeyListener {
private boolean aDown; //is the A key down?
//Ect, for all needed keys
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = true; break;
//and so on for all other needed keys.
}
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = false; break;
//and so on for all other needed keys.
}
}
public void keyTyped(KeyEvent ke) {} //Do nothing
public void isADown() {return aDown;}
}

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