| Datei CMain.h:
#include <afxwin.h> class CMain : public CFrameWnd { public: CMain(); }; |
Datei CAnwendung.h:
#include <afxwin.h> #include "CMain.h" class CAnwendung : public CWinApp { public: virtual BOOL InitInstance(); }; CAnwendung anwendung; |
| Datei CMain.cpp:
#include "CMain.h" CMain::CMain() { RECT fenster ={550,350,700,500}; Create(NULL,"Hi", WS_OVERLAPPEDWINDOW,fenster); } |
Datei CAnwendung.cpp:
#include "CAnwendung.h" BOOL CAnwendung::InitInstance() { m_pMainWnd = new CMain(); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } |
| Datei CMain.h:
#include <afxwin.h> #include "resource.h" class CMain : public CDialog { public: CMain(UINT nIDTemplate, CWnd* pParentWnd=NULL): CDialog(nIDTemplate,pParentWnd){}; protected: BOOL OnInitDialog(); }; |
Datei CAnwendung.h:
#include <afxwin.h> #include "CMain.h" class CAnwendung : public CWinApp { public: virtual BOOL InitInstance(); }; CAnwendung anwendung; |
| Datei CMain.cpp:
#include "CMain.h" BOOL CMotor::OnInitDialog() { CDialog::OnInitDialog(); return TRUE; } |
Datei CAnwendung.cpp:
#include "CAnwendung.h" BOOL CAnwendung::InitInstance() { CMain Main(IDD_MAIN); m_pMainWnd = &Main; Main.DoModal(); return FALSE; } |
| class CX : public
CDialog
{ ... protected: afx_msg void OnSenden(); afx_msg int OnCreate(LPCREATESTRUCT lpCS); afx_msg void OnDestroy( ); DECLARE_MESSAGE_MAP()
|
#include "CX.h"
BEGIN_MESSAGE_MAP(CX,CDialog) ON_WM_CREATE() ON_WM_DESTROY() ON_BN_CLICKED(IDC_SENDEN,OnSenden) END_MESSAGE_MAP() int CX::OnCreate(LPCREATESTRUCT lpCS)
void CX::OnDestroy()
void CX::OnSenden()
|
Diese Funktion im Header vereinbaren:
| public:
CString m_Text; public: virtual void DoDataExchange(CDataExchange* pDX); Dann in der CPP-Datei:
|
| UpdateData(true);
m_Text="Hejo"; UpdateData(false); |
| public:
CEdit m_ControlText; |
| void CX::DoDataExchange(CDataExchange*
pDX)
{ CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EINGABE, m_ControlText); } |
| m_ControlText.SetFocus(); |
In der MainHeader einfügen:
...
#include "resource.h"
...
class CMain : public
CFrameWnd
{
...
protected:
afx_msg void OnRButtonDown(UINT
nFlags,CPoint point);
...
DECLARE_MESSAGE_MAP()
};
Nun in der Main.cpp einfügen:
BEGIN_MESSAGE_MAP(CDumo,CFrameWnd)
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
...
void CMain::OnRButtonDown(UINT nFlags,CPoint
point)
{
CPoint m_Point = point;
ClientToScreen(&m_Point);
CMenu *m_Kontextmenu = new
CMenu();
m_Kontextmenu->LoadMenu(IDR_KONTEXTMENU);
CMenu *pMenu = m_Kontextmenu->GetSubMenu(0);
UINT nID=(UINT)pMenu->TrackPopupMenu (TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RETURNCMD|
TPM_LEFTBUTTON,m_Point.x,m_Point.y,this,NULL);
delete m_Kontextmenu;
if (nID!=0)
PostMessage(WM_COMMAND,nID,0);
}
| BOOL CTableClass::OnInitDialog()
{ ... CRect rect; m_ctrlTable.GetClientRect(&rect); int nColInterval = rect.Width()/4; m_ctrlTable.InsertColumn(0,_T("Spalte 1"),LVCFMT_LEFT,nColInterval*2);
m_ctrlTable.SetSelectionMark(1); m_ctrlTable.DeleteAllItems();
int CTableClass::InsertItem(CString
indexername, int &zeile)
//* jetzt die Subitems
int CTableClass::InsertColumn(int
zeile, int spalte, CString txt)
int CTableClass::GetCellValue(int
zeile, int spalte, CString &wert)
|
| class COutputTableDlg : public
CDialog
{ ... protected: // Generierte Nachrichtenzuordnungsfunktionen //{{AFX_MSG(COutputTableDlg) virtual BOOL OnInitDialog(); //}}AFX_MSG afx_msg void OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult ); DECLARE_MESSAGE_MAP() }; |
| BEGIN_MESSAGE_MAP(COutputTableDlg, CDialog)
//{{AFX_MSG_MAP(COutputTableDlg) //}}AFX_MSG_MAP ON_NOTIFY ( NM_CUSTOMDRAW, IDC__TABELLE, OnCustomdrawMyList ) END_MESSAGE_MAP() ... void COutputTableDlg::OnCustomdrawMyList
( NMHDR* pNMHDR, LRESULT* pResult )
*pResult = CDRF_DODEFAULT; if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage
)
// if(strTemp == "Hallo")
pLVCD->clrText = clrNewTextColor;
*pResult = CDRF_DODEFAULT;
|
| void CCloseClass::OnClose()
{ if (m_bClose==false) { MessageBox("Das Programm wird nicht beendet!!","Nicht schließen", MB_OK|MB_ICONWARNING); } else CDialog::OnClose(); } |
| //* Application Klasse *****************************************
int CSimulatorApp::SetClose(bool wert) { m_bClose = wert; return 0; } bool CSimulatorApp::GetClose()
//* View Klasse **********************************************
//* Main Klasse **********************************************
|
| Formate | Besitzt Schaltflächen | ja |
| Besitzt Linien | ja | |
| Linien am Stamm | ja | |
| Drag and Drop deaktivieren | ja | |
| Weitere Formate | Bildlauf | ja |
| Quickinfo | ja | |
| Track select | ja |
| BOOL CStrukturTestDlg::OnInitDialog()
{ ... TV_INSERTSTRUCT tvinsert; tvinsert.hParent = NULL; tvinsert.hInsertAfter = TVI_LAST; tvinsert.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT; tvinsert.item.hItem = NULL; tvinsert.item.state = 0; tvinsert.item.cchTextMax = 6; tvinsert.item.cChildren = 0; tvinsert.item.lParam = 0; //* oberste Ebene
//* zweite Ebene
//* dritte Ebene
|
Um dynamisch per rechten Mausklick mit Kontextmenü etwas hinzufügen
zu können, muß eine eigene Kindklasse von TreeCtrl geschaffen
werden.
Das Wichtigste ganz kurz:
| class CBaum: public CTreeCtrl
{ public: HTREEITEM m_hSelected; ... protected: CMyDlg *m_pParent; ... } |
| int CBaum::SetParent(CMyDlg *parent)
{ m_pParent = parent; return 0; } void CBaum::OnRButtonDown(UINT nFlags,
CPoint point)
if (m_hSelected!=NULL)
CTreeCtrl::OnRButtonDown(nFlags, point);
|
| a) | MDI-Projekt erstellen |
| b) | Downloaded Klassen ins Projekt mit einfügen |
| c) | Neue Klasse (CMyBar) erstellen, die von SizingControlBar public erbt. |
| d) | In Klasse CMainFrame eine protected Variable von CMyBar (m_wndMyBar) erstellen |
| e) | Nun in Create() von CMainFrame eintragen:
int CMainFrame::OnCreate(LPCREATESTRUCT
lpCreateStruct)
//* Eigene Toolbar erstellen
//* Docking erlauben!
//* Alten Status aus der Registry laden
(z. B. Wo Leiste das letzte Mal hingeschoben wurde)
|
| f) | Virtuelle Funktion OnDestroy() hinzufügen:
BOOL CMainFrame::DestroyWindow()
return CFrameWnd::DestroyWindow();
|
| g) | Jetzt vielleicht noch im Menü einen Punkt angeben, ob dieses Fenster
angezeigt werden soll oder nicht:
void CMainFrame::OnAnsichtMybar()
|
| h) | Dann noch den Menüpunkt, je nach Zustand checken:
void CMainFrame::OnUpdateAnsichtMybar(CCmdUI*
pCmdUI)
|
| BOOL CTestApp::InitInstance()
{ ... // Befehlszeile parsen, um zu prüfen auf Standard-Umgebungsbefehle DDE, Datei offen CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); if (cmdInfo.m_nShellCommand==CCommandLineInfo::FileNew) cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing; ... } |
| HANDLE hFile;
hFile = CreateFile("datei.txt", GENERIC_WRITE,0,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { TRACE("Datei konnte nicht geöffnet werden"); } char buff[225] = "Hey du Scherznase!"; DWORD bytes; WriteFile(hFile, buff, sizeof(buff), &bytes, NULL); CloseHandle(hFile); // DeleteFile("datei.txt"); |
| HANDLE hFile;
hFile = CreateFile("datei.txt", GENERIC_READ, 0,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) { TRACE("Datei konnte nicht geöffnet werden"); } char buff[225] = ""; DWORD bytes; ReadFile(hFile, buff, 700, &bytes, NULL); CloseHandle(hFile); // DeleteFile("datei.txt"); |
| DCB dcb;
HANDLE hCom2; BOOL fSuccess; hCom2 = CreateFile("COM2", GENERIC_WRITE,0, NULL, OPEN_EXISTING, 0,NULL); if (hCom2 == INVALID_HANDLE_VALUE) { TRACE("Fehler!"); } fSuccess = GetCommState(hCom2, &dcb); if (!fSuccess) { TRACE("GetCommState macht Ärger"); } dcb.BaudRate = CBR_2400; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; fSuccess = SetCommState(hCom2, &dcb); if (!fSuccess) { TRACE("SetCommState macht Ärger"); } char buff[225] = "Hey Pappnase!\n"; DWORD bytes; WriteFile(hCom2, buff, sizeof(buff), &bytes, NULL); CloseHandle(hCom2); |
Readfunktion. Das Handle auf den Port wurde bereits in der Open-Funktion
initialisiert.
| //* Die gelesenen Daten werden in txt pr Referenz
übergeben
//* return 0 - etwas gelesen, 1 - nix gelesen int CComPort::ReadPort(CString &txt) { DWORD bytes; ReadFile(m_hPort,txt.GetBufferSetLength(256),256,&bytes,NULL); txt.ReleaseBuffer(bytes); if (bytes==0) return 1; int pos=txt.Find((TCHAR)-52); if (pos>=0) txt=txt.Left(pos); return 0; } |
Writefunktion. Das Handle auf den Port wurde bereits in der Open-Funktion
initialisiert.
| //* Der zu schreibende Text wird in txt übergeben
int CComPort::WritePort(CString txt) { DWORD bytes; WriteFile(m_hPort,txt,txt.GetLength(),&bytes,NULL); return 0; } |
| class CSteuerung
{ public: void Threadfunktion(); static UINT Threadfunktion(LPVOID pParam); ... CWinThread* m_pThread; enum { NOTHING, START, STOP, EXIT_THREAD } m_iThreadCommand; enum { EXITED, RUNNING, IDLE } m_iThreadStatus; }; |
in der C++ Datei:
| CSteuerung::CSteuerung()
{ m_iThreadCommand = NOTHING; m_iThreadStatus = EXITED; m_pThread=AfxBeginThread(Threadfunktion,this,0,0,CREATE_SUSPENDED,0); m_pThread->m_bAutoDelete=FALSE; m_pThread->ResumeThread(); } CSteuerung::~CSteuerung()
int CSteuerung::StartMove()
UINT CSteuerung::Threadfunktion(LPVOID pParam)
void CSteuerung::Threadfunktion()
|
| CString help;
CString txt = "Hallo du da"; for (int i=0; i<txt.GetLength(); i++) { CString bu; bu.Format("%i ",txt[i]); help = help + bu; } OutputDebugString(help); |
| 1. | Möglichkeit: TRACE("bla "+szOutput+" hier\n");
Das klappt aber leider nur im Debug-Modus |
| 2. | Möglichkeit: OutputDebugString("hier"); bzw. OutputDebugString(szOutput);
Das klappt immer. Bei der Release-Version werden die Ausgaben auf den offenen Debugger ausgegeben, z.B. das Visual Studio. Ist keiner vorhanden, geht die Ausgabe ins Nirwana. |
| TCHAR path[_MAX_PATH];
::GetModuleFileName(NULL, path, _MAX_PATH); LPTSTR pszFileName = _tcsrchr(path, '\\') + 1; *pszFileName = '\0'; CString m_szProgramPath = path; |
| SYSTEMTIME realtime;
if (m_bLocalTime) GetLocalTime(&realtime); else GetSystemTime(&realtime); CString time;
|
| CString csText;
LPTSTR szText= csText.GetBuffer (100); _tcscpy (szText, _T("Hallo Welt!")); csText.ReleaseBuffer(); |
| WIN32_FIND_DATA w32fd;
HANDLE hFind= FindFirstFile("C:\\*.*", &w32fd); if (hFind == INVALID_HANDLE_VALUE) return; do { cout << w32fd.cFileName << endl; } while (FindNextFile(hFind, &w32fd)); FindClose(hFind); |
2. Möglichkeit (MFC)
| CFileFind finder;
BOOL bWorking = finder.FindFile("*.*"); while (bWorking) { bWorking = finder.FindNextFile(); if (!finder.IsDirectory()) cout << (LPCTSTR) finder.GetFileName() << endl; } |
| BOOL CProgrammApp::InitInstance()
{ ... HANDLE hMutex = CreateMutex (NULL, TRUE, "unverwechselbarer String"); bool found=false; if (GetLastError() == ERROR_ALREADY_EXISTS) found = true; if (hMutex) ReleaseMutex (hMutex); if (found) { AfxMessageBox("Das Programm wurde bereits gestartet"); return FALSE; } CProgrammDlg dlg;
|
| //* Funktion, um ein Datum und einen Wert
in die Registry einzutragen
bool CTest::WriteRegistry(LPCTSTR szName, DWORD dwValue) { HKEY hKey=NULL; //* Handle für eine Registrierung if (RegCreateKeyEx(HKEY_CURRENT_USER, //* Verzeichnis in der Registry _T("Software\\Katrin\\Test"), //* Schlüsseldatei 0, //* muß immer 0 sein! _T(""), //* String der die Klasse klassifieziert?! REG_OPTION_NON_VOLATILE, //* Die Angaben sollen nicht flüchtig KEY_WRITE, //* Zugriff NULL, //* Handle kann nicht vererbt werden &hKey, //* Handle, in das geschrieben werden soll, 0 //* Keine Disposition zurück )==ERROR_SUCCESS) { //* Jetzt einen Datum und ein spezifizierter Wert schreiben //* Wenn schreiben erfolgreich, dann wird ERROR_SUCCESS zurückgeliefert bool bResult=(RegSetValueEx(hKey, //* Handle auf die Registry szName, //* Übergebenes Datum 0, //* Muß 0 sein! REG_DWORD_LITTLE_ENDIAN, //* Typ (LPBYTE)&dwValue, //* Übergebener Wert sizeof(dwValue) //* Größe des Wertes )==ERROR_SUCCESS); RegCloseKey(hKey); //* Handle wieder schließen return bResult; } return false; } //* Funktion, um ein Datum und einen Wert aus
der Registry auszulesen
|