|
Captain's
VC++ LOG #2:
This is not a tutorial. But wait! This is my log, started when I was seriously
beginning to do VC++. You might find the one or another hint here - some
topics are hard to find on the net.
Writing integers (int) and double values into a file
Reading integers (int) and double values in native format
from a file
Both cases with CFile
Load and draw a bitmap
Capture window messages and clipboard copy & paste
Remove frame window title bar text (Unbenannt)
Command line file names with white spaces
Accessing
(reading) variables from CWinApp in CView, CDoc etc...
###################################################################################
Writing integers (int) and double values into a file
#include "fstream.h"
double pi = 3.14159;
int ix = 123;
ofstream fo("file.dat");
fo.setmode(filebuf::binary);
fo.write((char *)&pi,
sizeof(double));
fo.write((char *)&ix, sizeof(int));
fo.close();
top
###################################################################################
Reading integers (int) and double values in native format
from a file
#include "fstream.h"
double pi;
int ix;
ifstream fin("file.dat");
fin.setmode(filebuf::binary);
fin.read((char*)&pi, sizeof(double));
fin.read((char*)&ix, sizeof(int));
fin.close();
top
###################################################################################
Both
cases with CFile:
CFile cf; CFileException ex;
if (cf.Open("data.dat", CFile::modeWrite, &ex)) { double pi = 3.14159; int ix = 123; cf.Write(&pi, sizeof(double)); cf.Write(&ix, sizeof(int)); cf.Close(); }
if (cf.Open("data.dat", CFile::modeRead, &ex)) { double pi; int ix; cf.Read(&pi, sizeof(double)); cf.Read(&pi, sizeof(double)); cf.Close(); }
top
###################################################################################
Load
a bitmap and draw it into the window:
//Load the bitmap
HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(), "field.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
//Attach the HBITMAP
to a CBitmap
CBitmap m_bmpBitmap;
m_bmpBitmap.Attach(hBitmap);
//Get the loaded bitmap
BITMAP bm;
m_bmpBitmap.GetBitmap(&bm);
//prepare drawing
CDC dcMem;
CDC* pdc = GetDC();
dcMem.CreateCompatibleDC(pdc);
CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(m_bmpBitmap);
CRect lRect;
GetClientRect(lRect);
lRect.NormalizeRect();
//draw the bitmap
pdc->BitBlt (10, 10, bm.bmWidth, bm.bmHeight, &dcMem, 0, 0, SRCCOPY)
;
top
###################################################################################
Capture
window messages and clipboard copy & paste
m_ceditctrl must be
a Edit Control and not a simple CString attached to the Edit Box
Setting text to the CEditCtrl with m_ceditctrl.SetWindowText(resu); instead
of m_cedit = [CString]
BOOL CRomeView::PreTranslateMessage(MSG* pMsg)
{
/* if (pMsg->message==WM_CHAR) { // convert all chars to upper case
if (islower(pMsg->wParam)) {
pMsg->wParam=_toupper(pMsg->wParam);
}
}
*/
if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN) {
//do something if enter (return) is pressed
}
if(pMsg->message ==WM_KEYDOWN) {
if (pMsg->wParam == 0x11) { //CTRL PRESSED
m_CtrlButtonDown = true;
} else if (pMsg->wParam == 0x43 ) { //COPY
if( m_CtrlButtonDown == true) {
m_ceditctrl.Copy();
}
} else if (pMsg->wParam == 0x56 ) { //PASTE
if( m_CtrlButtonDown == true) {
m_ceditctrl.Paste();
}
}
}
if(pMsg->message ==WM_KEYUP) {
m_CtrlButtonDown = false;
}
return CFormView::PreTranslateMessage(pMsg);
}
http://www.codeproject.com/editctrl/editctrltutorial.asp
Another way to copy text to the clipboard:
CString source; //string to copy to clipboard if(OpenClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
An how to get a string
from the clipboard:
CString result; // string from clipboard
char * buffer = NULL;
//open the clipboard
if ( OpenClipboard() ) {
HANDLE hData = GetClipboardData( CF_TEXT );
char * buffer = (char*)GlobalLock( hData );
result = buffer; // clipboard to_result
GlobalUnlock( hData );
CloseClipboard();
}
top
###################################################################################
Remove
frame window title bar text (Unbenannt)
Put cs.style
&= ~FWS_ADDTOTITLE;
into BOOL CMainFrame::PreCreateWindow(CREATESTRUCT&
cs)
top
###################################################################################
Command
line file names with white spaces
Remove
ParseCommandLine(cmdInfo);
from your CWinApp.
Then you've got the whole command-line-string in m_lpCmdLine
top
###################################################################################
Accessing
(reading) variables from CWinApp in CView, CDoc etc...
//
get a pointer to the CWinApp object
CCapeditApp* pApp = (CCapeditApp*)AfxGetApp();
// access any variable or function
filename = pApp->filename;
|