I am developing a small application in MFC... there is a little problem..hope you guys would help me regarding this...Here we go..the problem is...I have 6 little edit control(Text box) in which I will allow the user to enter some numbers..I have limited the number of chars/textbox as 4 but its allowing the user to copy and paste n numbers....How do I restrict the copy paste option in an Edit control....Please help me...
I found 2 ways of solving the problem....please check the below...
1st method:
class CNoPasteEdit: public CEdit
{
public:
CNoPasteEdit();
~CNoPasteEdit();
protected:
// This line will need to be added by hand because WM_PASTE is not available in
// class wizard
afx_msg void OnPaste(WPARAM wParam, LPARAM lParam);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
};
Then you will need to edit the .cpp file for this class like so
CNoPasteEdit::CNoPasteEdit(){
// Put any construction code here
}
CNoPasteEdit:~:CNoPasteEdit(){
// Put any destruction code here
}
BEGIN_MESSAGE_MAP(CNoPasteEdit, CEdit)
// This line is needed because there is no default macro for WM_PASTE messages
// This line will also need to be added by hand
ON_MESSAGE(WM_PASTE, OnPaste)
ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
void CNoPasteEdit::OnPaste(WPARAM wParam, LPARAM lParam){
// Put any code here you want to execute when the user right clicks on the edit
// control. Just leave it blank to disable the menu
}
void CNoPasteEdit::OnContextMenu(CWnd* pWnd, CPoint point){
// Put any code here you want to execute when the user tries to paste into the edit
// conrtol. Just leave it blank to prevent pasting.
}
2nd method:
Handle the ON_EN_CHANGE event and capture the text in the CString and check if its more than the limited character..if its..you can clear the text box with a warning message...
Related
I have a CStatic picture control on a CDialog that I use to draw content:
CMyDrawingControl.h
CMyDrawingControl : CStatic
{
//Constructor, Destructor, other items
//End Constructor, Destructor, other items
public:
void DrawStuff(CDC *dc);
protected:
afx_msg void OnPaint();
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
}
CMyDrawingControl.cpp
CMyDrawingControl::CMyDrawingControl
{
}
BEGIN_MESSAGE_MAP(CMyDrawingControl, CStatic)
//{{AFX_MSG_MAP(CMyDrawingControl)
ON_WM_VSCROLL()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMyDrawingControl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
//determine delta
ScrollWindow(0, -delta);
Invalidate();
UpdateWindow();
}
void CMyDrawingControl::OnPaint()
{
CPaint dc(this);
DrawStuff(&dc);
}
void CMyDrawingControl::DrawStuff(CDC *dc)
{
dc->SetMapMode(MM_LOMETRIC);
//draw on dc
//text, lines, shapes, etc
}
However the content is usually bigger than the control so I need to be able to scroll the content. CScrollView automatically handles by drawing to the view in OnDraw, but I can't seem to get it to work in OnPaint(). The control either will draw blank or has a lot of repeated content when scrolling.
I'm basically trying to duplicate the exact behavior of CScrollView on a CDialog; I've seen some posts that come close to this, but I don't want to implement a CDocument and a CView.
I think it is much simpler to use a read only edit control.
Beside scrolling this also makes it possible that the user can select and copy parts of the text.
I'm writing a simple MFC application with a Dialog window and some buttons.
I added also a edit control in order to let user insert a text string.
I'd like to read the value which is present in the edit control and to store it in a string but i do not know how to do this.
I have no compilation error, but I always read only a "." mark.
I added a variable name to the text edit control which is filepath1 and this is the code:
// CMFC_1Dlg dialog
class CMFC_1Dlg : public CDialogEx
{
// Construction
public:
CMFC_1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFC_1_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnEnChangeEdit1();
CString filePath1;
}
//...
void CMFC_1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
CMFC_1Dlg::CMFC_1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFC_1Dlg::IDD, pParent)
,filePath1(("..\\Experiments\\Dirs\\"))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFC_1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, filePath1);
}
// then i try to get the string value with
CString txtname=filePath1;
_cprintf("Value %s\n", txtname); // but i always read just a "."
_cprintf("Value %S\n", txtname.GetString());
Note the capital 'S'
or you can cast:
_cprintf("Value %S\n", (LPCTSTR)txtname);
You would be better off using an edit control. To create a CEdit variable, right click on the edit box in VS and select "Add Member Variable", give the variable a name and click OK.
You can then retrieve the text in the edit box like this:
CEdit m_EditCtrl;
// ....
CString filePath1;
m_EditCtrl.GetWindowText(filePath1);
I think your original code was OK for DDX use and CString. The advice to use a control variable and avoid the DDX/DDV functions is really one of preference and not the issue.
I suspect you are compiling with the UNICODE libraries but explicitly calling an ASCII function _cprintf. UNICODE is held as two bytes, for ASCII characters one of these will be 0. If you pass this to an ASCII string function it will stop after the first character.
If you are using UNICODE then call _cwprintf or use the tchar.h macro _tcprintf which will call the correct version for the compiler switch.
Tip: If you are targeting UNICODE only and will never require MBCS support then avoid using the tchar.h macros as they will obscure any issues with char and TCHAR data type mixing.
Step 1: Create a CEdit control variable using "Add Variable List".
Step 2: Use GetDlgItemText() to hold the text of that edit control.
Example: such as CEdit control list variable is mc_strChatPane, then GetDlgItemText(mc_strChatPane, message) where message is a user defined CString variable.
I have asked two questions earlier about this and for each post there was some solutions i tried them, but the problem still exist.
My first question was : why a windowless Activex does not return the Handle. the suggestion was "change the creation setting an make windowless activate off, i have tried it but still m_hWnd property has returned zero as GetSafeHwnd() method has did.
the second one was the same question this one focused on COleControl class and it's ancestor CWnd. the solution was as this "Create invisible window somewhere in your control initialization code. Handle the messages sent to this window, and call controls methods directly". so i did that but the created class still returns zero handle.
here is my new invisible class source:
// moWind.cpp : implementation file
//
#include "stdafx.h"
#include "PINActive.h"
#include "moWind.h"
#include "include\xfspin.h"
#include <math.h>
// moWind
IMPLEMENT_DYNAMIC(moWind, CWnd)
moWind::moWind(){}
moWind::~moWind(){}
//=============================================================
LRESULT moWind::OnExecuteEvent (WPARAM wParam, LPARAM lParam)
{
WFSRESULT *pResult = (WFSRESULT *)lParam;
CString EK=_T("");
CString str;
int reskey=0;
if (pResult->u.dwEventID=WFS_EXEE_PIN_KEY)
{
LPWFSPINKEY pressedkey;
pressedkey=(LPWFSPINKEY)pResult->lpBuffer;
reskey = log10((double)pressedkey->ulDigit) / log10((double)2);
EK.Format("%d",reskey);
xfsOnKeyEvent->OnKeyRecieved(reskey);
}
else
{
str.Format("ExecuteEvent: ID = %d\r\n", pResult->u.dwEventID);
}
MessageBox("a Execute message Recieved");
return 0;
}
BEGIN_MESSAGE_MAP(moWind, CWnd)
ON_MESSAGE(WFS_EXECUTE_EVENT,OnExecuteEvent)
END_MESSAGE_MAP()
and this is .h file of the class:
// moWind.h
class IXFSEvents
{
protected:
IXFSEvents(){};
virtual ~IXFSEvents(){};
public:
virtual void OnKeyRecieved(int key)=0;
};
class moWind : public CWnd
{
DECLARE_DYNAMIC(moWind)
public:
moWind();
virtual ~moWind();
void Register(IXFSEvents* obj)
{
xfsOnKeyEvent= obj;
}
protected:
IXFSEvents* xfsOnKeyEvent;
LRESULT OnExecuteEvent (WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
};
and at the end here this the way I've used this class in my Activex:
in the myActivex.h file:
include "moWind.h"
class CmyActivexCtrl : public COleControl, public IXFSEvents
{
...
Class definition
...
protected:
moWind tmpWind;
.
.
};
finally in the creation method of myActivex i have initialized the component callback method an wanted to get it's Handle as this:
CmyActivexCtrl::CmyActivexCtrl()
{
InitializeIIDs(&IID_DmyActivex, &IID_DmyActivexEvents);
tmpWind.Register(this);
myOtherComponent.WindowsHandle=tmpWind.GetSafeHwnd(); //here my Cwnd derived class returns zero
//my other component gets the handle and call an API with it to register
//the given handle and force the API to send the messages to that handle.
}
As you mentioned you need a window handle to be able to receive user messages through it, you always have an option of creating a helper window, such as message only window, see Using CreateWindowEx to Make a Message-Only Window.
For your windowless control it is okay to not have any window handle at all, so you cannot really rely on handle availability unless you own a window yourself.
I have a CListCtrl class and at the moment when a user selects one of the sub items I am displaying a CComboBox over the subitem which the user can then make a selection from.
However I have a problem. When the user has made a selection i need the combo box to disappear (ie intercept CBN_SELCHANGE). The problem is that I need to make the CComboBox a child of the CListCtrl (Otherwise I get weird problems with the list drawing over the combo box even if i set the combo box to be topmost). So the CBN_SELCHANGE message gets sent to the list view which, understandably, ignores it. How can I get the list view to pass that message up to the parent window.
Do I really need to derive my own CListCtrl class that simply intercepts the CBN_SELCHANGE message and passes it up to the parent window? Is there a better way to do this than creating an OnWndMsg handler?
Thanks for any help!
Edit: This code works
class CPassThroughListCtrl : public CListCtrl
{
protected:
virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
if ( message == WM_COMMAND )
{
GetParent()->SendMessage( message, wParam, lParam );
}
return CListCtrl::OnWndMsg( message, wParam, lParam, pResult );
}
public:
CPassThroughListCtrl()
{
};
};
But i'd really like to know if there is a nicer way to do this.
You can subclass CComboBox such that it will handle CBN_CLOSEUP message.
Your custom Combo will know about the manager i.e. the object that created it in the first place and will have to destroy it upon close up (top level window or whatever, should be provided as an argument to your custom combobox constructor)...
So when you create combobox on a top of the list item you will create instance of this customized combobox instead of the MFC default one.
Combobox event handler could look like that:
BEGIN_MESSAGE_MAP(CNotifyingComboBox, CComboBox)
ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseUp)
END_MESSAGE_MAP()
void CNotifyingComboBox::OnCloseUp()
{
// _manager is pointer to the object that created this combobox,
// and is responsible for its destruction,
// should be passed into CNotifyingComboBox cosntructor
if( NULL != _manager )
{
_manager->OnCloseUpComboBox(this);
}
}
I'm building a project with MFC Feature Pack. Is this project I have a window which includes a CView, which includes a CListCtrl-derived object. The object includes the LVS_EDITLABELS flag.
Somehow I cannot edit the CListCtrl icon labels by two-time clicking (not double-clicking) on the icon label. After I select the item with a single click, a second click just flashes the item (button down turns text background to white, button up turns it back to blue) and the edit control never appears.
I reduced this problem to the simplest form, and even with a plain CListCtrl object I cannot edit the labels.
I also found that:
This problem occurs in VS2008. It doesn't occur in a similar project built in VS2003.
I am able to edit the labels if I build a CListView instead of a CView+CListCtrl.
I am also able to edit the labels if I build a CFormView and put the CListCtrl inside the resource dialog.
Here's some code in the simplest form: the .h file:
// vwTerminaisTeste.h
//
#pragma once
// vwTerminaisTeste view
class vwTerminaisTeste : public CView
{
DECLARE_DYNCREATE(vwTerminaisTeste)
protected:
vwTerminaisTeste(); // protected constructor used by dynamic creation
virtual ~vwTerminaisTeste();
CListCtrl m_lstTerminais;
protected:
DECLARE_MESSAGE_MAP()
virtual void OnDraw(CDC* /*pDC*/);
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
and the .cpp file:
// vwTerminaisTeste.cpp : implementation file
//
#include "stdafx.h"
#include "vwTerminaisTeste.h"
// vwTerminaisTeste
IMPLEMENT_DYNCREATE(vwTerminaisTeste, CView)
vwTerminaisTeste::vwTerminaisTeste()
{
}
vwTerminaisTeste::~vwTerminaisTeste()
{
}
BEGIN_MESSAGE_MAP(vwTerminaisTeste, CView)
ON_WM_CREATE()
ON_WM_SIZE()
END_MESSAGE_MAP()
// vwTerminaisTeste message handlers
void vwTerminaisTeste::OnDraw(CDC* /*pDC*/)
{
CDocument* pDoc = GetDocument();
}
int vwTerminaisTeste::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_lstTerminais.Create(WS_CHILD | WS_VISIBLE | LVS_EDITLABELS, CRect(0,0,1,1), this, 0);
m_lstTerminais.InsertItem(0, "Teste", 0);
return 0;
}
void vwTerminaisTeste::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (IsWindow(m_lstTerminais.GetSafeHwnd()))
m_lstTerminais.MoveWindow(0, 0, cx, cy);
}
This way I cannot edit labels.
To change it to a CListView I simply replaced CView by CListView and m_lstTerminais by GetListCtrl(), and removed the OnCreate and OnSize implementations. That way it worked.
Note: the vwTerminaisTeste is created from a CSplitterWndEx within a CMDIChildWndEx-derived class.
Well nobody solved this problem but I managed to go around it by changing the CView to a CFormView and building a resource dialog with a ListView control, attaching it to the CListCtrl-derived class.
If anyone still has any suggestions on how could I solve this problem entirely, I'd appreciate them.
This sounds like it may be a focus or command routing issue, although that doesn't explain why it works OK in VS2003. You might try routing the command and/or focus messages from the splitter ctrl to vwTerminaisTeste, and/or from the MDIChild to the splitter. If you haven't already, you may need to derive your own splitter window. The command/focus forwarding would be something like...
BEGIN_MESSAGE_MAP(MySplitter, CSplitterWnd)
ON_WM_SETFOCUS()
END_MESSAGE_MAP(...)
void MySplitter::OnSetFocus(CWnd* pOldWnd)
{
// forward focus to the view window
m_vwTerminaisTeste.SetFocus();
}
BOOL MySplitter::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// let the view have first crack at the command
if (m_vwTerminaisTeste.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
// otherwise, do default handling
return MySplitter::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}