|
Yes. Not a CString or std::string though. LPTSTR are fine as long as you guarantee that it remains allocated until the message has been processed.
|
|
|
|
|
Niklas Lindquist wrote:
Yes. Not a CString or std::string though. LPTSTR are fine as long as you guarantee that it remains allocated until the message has been processed.
One of many ideas is to use a ptr on a static buffer - or using PostMessage() (so the data can get evaluated before returning and releasing a dynamic memory).
|
|
|
|
|
Yes, I do this in a lot of my applications. Especially in multi-threaded apps where you should only post messages between threads. Here is an example posting a CString.
Note you should allocate whatever you wish to post. It can be any kind of object.
...
CString *pStr = new CString();
*pStr = "Set String Here!";
pMyView->PostMessage(WM_MY_MSG, 0, (LPARAM)pStr);
... Note that the receiver of this message MUST free the allocated memory.
LRESULT CMyView::OnHandleMyMsg(WPARAM wParam, LPARAM lParam)
{
CString *pStr = (CString *)lParam;
if(pStr)
{
delete pStr;
}
return 0;
} This last step is important. Before the window is destroyed, check for any unhandled messages and delete their memory.
void CMyView::OnDestroy()
{
MSG msg;
while(::PeekMessage(&msg, (HWND)NULL, WM_MY_MSG, WM_MY_MSG, PM_REMOVE))
{
CString *pStr = (CString *)msg.lParam;
if(pStr)
delete pStr;
}
CView::OnDestroy();
} Hope this helps.
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
Hi,
what is a good way to combine a listbox with an edit control?
I run a small chat application (heavy alpha yet). I have the problem that the interface is not very user friendly yet. For example, when setting focus on listbox the keyboard input is not shown in edit control - or - when being in edit control the mouse wheel is not routed to the listbox. Both controls should be merged more together.
Thx for some design guide lines or pointing me into the right direction!
Thx, Moak
|
|
|
|
|
ComboBox?
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
If a man is standing in the middle of the forest speaking and there is no woman around to hear him...is he still wrong?
|
|
|
|
|
Redirect keystrokes from your list to your dialog. Mouswheel events from your edit to your dialog. Keystrokes from your dialog to your edit. Mousewheel events from your dialog to your list.
|
|
|
|
|
Niklas Lindquist wrote:
Redirect keystrokes from your list to your dialog. Mouswheel events from your edit to your dialog. Keystrokes from your dialog to your edit. Mousewheel events from your dialog to your list.
thx, will try that!
I know the window handle of each other. Which is the MFC place to scan the correspoding messages, is it PreTranslateMessage() handler from CListBox/CEdit?
|
|
|
|
|
That is probably the easiest place.
|
|
|
|
|
okie... will hook me up there.
If there are any alternatives, let me know.
But I wont add a unique handler for every message type, hmm maybe a combined handler for 'relaying'.... hmmm.
|
|
|
|
|
Does map reserve memory space like vector does? To check
the cost of having a map inside my class, I make a test
class like this:
struct object {
map<int, int=""> data;
};
the class size itself is 16 byte, obviously. But when I
make instances of that class, such as
int main() {
for(int i=0;i<10000;++i) object* ob = new object();
}
from the task manager I see that my process takes up about
1 MB for each additional 10000 objects, which means one
object cost about 100 bytes. Where did the extra 100-16 =
84 bytes come from? I haven't even added any entry to the maps. What can I do to reduce memory usage per object?
Thanks in advance,
Danny
.
|
|
|
|
|
The map manages its memory through the allocator that is specified for the map. So it is up to the allocator that is chosen to decide how memory is allocated. I suspect that the default allocator does cache a few map entry nodes to speed up the use of the map.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Probably in release mode the memory used is much less (give it a try). Also, my STL block allocator could help gain a little space (though it was not designed for that purpose.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I did use the release version. I even run the release exe directly from windows instead of the debugger (there was a slight difference). I'll experiment with your block allocator, thank you.
Danny
|
|
|
|
|
Is there any function to add time delay to a process;( like delay() function in c,c++)
thanking u
|
|
|
|
|
Maybe you are looking for _sleep or Sleep .
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Thanx
for reply ; can u please tell the syntax of sleep .
|
|
|
|
|
VOID Sleep(
DWORD dwMilliseconds
);
Parameters
dwMilliseconds
[in] Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.
Return Values
This function does not return a value.
|
|
|
|
|
I've written a video capture application which also captures Frames. I use SampleGrabber filter and CaptureBuilder.
when i connect filters using CaptureBuilder, SampleGrabber fails.
alternatively, when i connect without using CaptureBuilder(using
graph->connect (pins)), SampleGrabber works but there's time-delay in stream rendering.
If any one have worked on this already, pls help me.
Thanx in advance for ur guidence
Ramesh.D
|
|
|
|
|
Hi,
I was working on this and i had a problem too with
ISampleGrabber, Actually my function GetMediaType
was returning an error.
Can you explain me how can i perform the following..
1. Open the Camera and just take a shot from it
2. close the device..
Sameer
|
|
|
|
|
I too face the same problem when i use the GraphBuilder.
Instead I instanciated the filters, added to the Graph, and the connected their pins(pGraph->Connect(inPin, outPin)
now Sample grabber works fine.
But there's time delay rendering the video input to the screen
Ramesh.D
|
|
|
|
|
Hello
I'm trying to print a bitmap from the resources. The bitmap has more than 256 colours. The idea is that i transfered my program from winnt 4.0 to windows xp. On the windows nt machine the program works. On the windows xp machine the app works only for the local printer; when it comes for the network printers i get an empty page. Why is that ? Somebody pls help me ... The prog is compiled with visual c++ 6.0. Here is the code in the OnDraw function - the only code i added :
int x=0, y=0;
CRect rcClient; GetClientRect(&rcClient);
if(pDC->IsPrinting()){
x = pDC->GetDeviceCaps(HORZRES);
y = pDC->GetDeviceCaps(VERTRES);
}
else {
x=rcClient.Width();
y=rcClient.Height();
}
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP8);
CDC memDC;
memDC.CreateCompatibleDC(NULL);
BITMAP bm;
bmp.GetObject( sizeof(BITMAP), &bm );
CBitmap* pOldBitmap = memDC.SelectObject(&bmp);
pDC->StretchBlt(0,0,x,y, &memDC, 0,0, bm.bmWidth,bm.bmHeight, SRCCOPY);
P.S. I would appreciate almost any good advice. This printing thing is something i wasn't able to find in any book (i mean printing a bitmap). This is sort of my only hope.
|
|
|
|
|
I have had the same problem trying to print CBitmap objects. DIBs work better for printing. Here is a code sniplet that I used to print a logo in the upper right corner of a printer page. It loads the resource into a CBitmapToDIB object. You will have to modify this for you application.
...
CBitmapToDIB dib;
if(dib.Create(IDB_LOGO1))
{
LPBITMAPINFOHEADER lpHdr;
CSize sizeDisplay;
CSize sizeLogo;
lpHdr = dib.GetBitmapHeader();
sizeLogo.cx = lpHdr->biWidth;
sizeLogo.cy = lpHdr->biHeight;
sizeDisplay.cx = sizeLogo.cx * 2;
sizeDisplay.cy = sizeLogo.cy * 2;
StretchDIBits(pDC->m_hDC, rcHeader.right - sizeDisplay.cx, rcHeader.top,
sizeDisplay.cx, sizeDisplay.cy,
0, 0, sizeLogo.cx, sizeLogo.cy,
dib.GetBitmapBits(), dib.GetBitmapData(),
DIB_RGB_COLORS, SRCCOPY);
}
... I have added the code for CBitmapToDIB below. I can't take credit for this code, I think I got it out of the MSDN years ago. There may be some newer or better code in the current MSDN or on Code Project.
But the code below works...
#if !defined(_BITMAPTODIB_H__INCLUDED_)
#define _BITMAPTODIB_H__INCLUDED_
class CBitmapToDIB
{
public:
CBitmapToDIB();
~CBitmapToDIB();
BOOL Create(CBitmap *hBitmap, CDC *pDC, CPalette *pPal = NULL, int nBitsPerPixel = -1);
BOOL Create(LPCSTR lpResourceString);
BOOL Create(WORD nResourceID);
void Destroy();
const LPVOID GetBitmapBits();
const LPBITMAPINFO GetBitmapData();
const LPBITMAPINFOHEADER GetBitmapHeader();
BOOL WriteDIBToFile(LPCTSTR lpszFileName, CProgressCtrl *pProgressCntrl = NULL);
BOOL WriteDIBToFile(CFile *pFile, CProgressCtrl *pProgressCntrl = NULL);
private:
void InitBitmapInfoHeader(LPBITMAPINFOHEADER pHdr, int nBitsPerPixel);
static WORD PaletteSize(LPBITMAPINFOHEADER pHdr);
static WORD DibNumColors(LPBITMAPINFOHEADER pHdr);
private:
HANDLE m_hDIB;
BITMAP m_Bitmap;
LPBITMAPINFO m_lpbmInfo;
LPBITMAPINFOHEADER m_lpbmInfoHdr;
LPBYTE m_lpBits;
DWORD m_dwDIBSize;
};
#endif //_BITMAPTODIB_H__INCLUDED_
#include "stdafx.h"
#include "BitmapToDIB.h"
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
CBitmapToDIB::CBitmapToDIB()
{
m_hDIB = NULL;
Destroy();
}
CBitmapToDIB::~CBitmapToDIB()
{
Destroy();
}
void CBitmapToDIB::Destroy()
{
if(m_hDIB)
delete [] m_hDIB;
m_hDIB = NULL;
m_lpBits = NULL;
m_lpbmInfo = NULL;
m_lpbmInfoHdr = NULL;
m_dwDIBSize = 0;
}
void CBitmapToDIB::InitBitmapInfoHeader(LPBITMAPINFOHEADER pHdr, int nBitsPerPixel)
{
int nBPP;
memset(pHdr, 0, sizeof(BITMAPINFOHEADER));
pHdr->biSize = sizeof(BITMAPINFOHEADER);
pHdr->biWidth = m_Bitmap.bmWidth;
pHdr->biHeight = m_Bitmap.bmHeight;
pHdr->biPlanes = 1;
if ((nBitsPerPixel == 1) || (nBitsPerPixel == 4) ||
(nBitsPerPixel == 8) || (nBitsPerPixel == 24))
{
nBPP = nBitsPerPixel;
}
else
{
nBPP = m_Bitmap.bmPlanes * m_Bitmap.bmBitsPixel;
}
if(nBPP <= 1)
nBPP = 1;
else
if(nBPP <= 4)
nBPP = 4;
else
if(nBPP <= 8)
nBPP = 8;
else
nBPP = 24;
pHdr->biBitCount = (unsigned short)nBPP;
pHdr->biSizeImage = WIDTHBYTES(m_Bitmap.bmWidth * nBPP) *
m_Bitmap.bmHeight;
}
const LPVOID CBitmapToDIB::GetBitmapBits()
{
return LPVOID(m_lpBits);
}
const LPBITMAPINFO CBitmapToDIB::GetBitmapData()
{
return m_lpbmInfo;
}
const LPBITMAPINFOHEADER CBitmapToDIB::GetBitmapHeader()
{
return m_lpbmInfoHdr;
}
BOOL CBitmapToDIB::Create(CBitmap *hBitmap, CDC *pDC, CPalette *pPal, int nBitsPerPixel)
{
CDC dc;
BITMAPINFOHEADER tempHdr;
ASSERT(hBitmap);
if(!hBitmap)
return FALSE;
if(!hBitmap->GetBitmap(&m_Bitmap))
return FALSE;
Destroy();
InitBitmapInfoHeader(&tempHdr, nBitsPerPixel);
m_dwDIBSize = sizeof(BITMAPINFOHEADER) + PaletteSize(&tempHdr) + tempHdr.biSizeImage;
m_hDIB = (HANDLE)new char[m_dwDIBSize];
if(!m_hDIB)
return FALSE;
m_lpbmInfo = (LPBITMAPINFO)m_hDIB;
m_lpbmInfoHdr = &m_lpbmInfo->bmiHeader;
*m_lpbmInfoHdr = tempHdr;
m_lpBits = (LPBYTE)m_hDIB + (WORD)m_lpbmInfoHdr->biSize + PaletteSize(m_lpbmInfoHdr);
if(!dc.CreateCompatibleDC(pDC))
return FALSE;
if(pPal)
{
dc.SelectPalette(pPal, FALSE);
dc.RealizePalette();
}
if(!::GetDIBits(dc.m_hDC, HBITMAP(*hBitmap), 0, m_Bitmap.bmHeight, m_lpBits,
m_lpbmInfo, DIB_RGB_COLORS))
{
return FALSE;
}
return TRUE;
}
BOOL CBitmapToDIB::Create(WORD nResourceID)
{
return Create(MAKEINTRESOURCE(nResourceID));
}
BOOL CBitmapToDIB::Create(LPCSTR lpResourceString)
{
HRSRC hRsrc;
HGLOBAL hTemp;
LPSTR lpRes;
HINSTANCE hInstance = AfxGetInstanceHandle();
ASSERT(hInstance && lpResourceString);
if(!hInstance || !lpResourceString)
return FALSE;
hRsrc = FindResource(hInstance, lpResourceString, RT_BITMAP);
if(hRsrc)
{
Destroy();
hTemp = LoadResource(hInstance, hRsrc);
m_dwDIBSize = SizeofResource(hInstance, hRsrc);
lpRes = (LPSTR)LockResource(hTemp);
m_hDIB = (HANDLE)new char[m_dwDIBSize];
if(!m_hDIB)
return FALSE;
memcpy(m_hDIB, lpRes, m_dwDIBSize);
UnlockResource(hTemp);
FreeResource(hTemp);
m_lpbmInfo = (LPBITMAPINFO)m_hDIB;
m_lpbmInfoHdr = &m_lpbmInfo->bmiHeader;
m_lpBits = (LPBYTE)m_hDIB + (WORD)m_lpbmInfoHdr->biSize + PaletteSize(m_lpbmInfoHdr);
return TRUE;
}
return FALSE;
}
WORD CBitmapToDIB::PaletteSize(LPBITMAPINFOHEADER pHdr)
{
WORD NumColors;
NumColors = DibNumColors(pHdr);
if (pHdr->biSize == sizeof(BITMAPCOREHEADER))
return (WORD)(NumColors * sizeof(RGBTRIPLE));
else
return (WORD)(NumColors * sizeof(RGBQUAD));
}
WORD CBitmapToDIB::DibNumColors(LPBITMAPINFOHEADER pHdr)
{
int bits;
LPBITMAPINFOHEADER lpbi;
LPBITMAPCOREHEADER lpbc;
lpbi = (LPBITMAPINFOHEADER)pHdr;
lpbc = (LPBITMAPCOREHEADER)pHdr;
if(lpbi->biSize != sizeof(BITMAPCOREHEADER))
{
if(lpbi->biClrUsed != 0)
return (WORD)lpbi->biClrUsed;
bits = lpbi->biBitCount;
}
else
bits = lpbc->bcBitCount;
switch(bits)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
BOOL CBitmapToDIB::WriteDIBToFile(LPCTSTR lpszFileName, CProgressCtrl *pProgressCntrl)
{
CString s;
if(!m_hDIB)
return FALSE;
try
{
CFileStatus rStatus;
if(CFile::GetStatus(lpszFileName, rStatus))
{
s.Format("File \"%s\" already exits. Overwrite?", lpszFileName);
if(AfxMessageBox(s, MB_OKCANCEL) == IDCANCEL)
return FALSE;
}
CFile file(lpszFileName, CFile::modeCreate | CFile::modeWrite);
return WriteDIBToFile(&file, pProgressCntrl);
}
catch(...)
{
s.Format("ERROR: Could not create BMP file \"%s\".", lpszFileName);
AfxMessageBox(s);
}
return FALSE;
}
BOOL CBitmapToDIB::WriteDIBToFile(CFile *pFile, CProgressCtrl *pProgressCntrl)
{
BITMAPFILEHEADER bfh;
DWORD dwFileSize;
ASSERT(m_hDIB && pFile);
if(!m_hDIB || !pFile)
return FALSE;
dwFileSize = sizeof(BITMAPFILEHEADER) + m_dwDIBSize;
bfh.bfType = 0x4d42;
bfh.bfSize = dwFileSize;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = (m_lpBits - (LPBYTE)m_hDIB) + sizeof(BITMAPFILEHEADER);
try
{
if(pProgressCntrl)
pProgressCntrl->SetRange(0, 100);
pFile->Write(&bfh, sizeof(BITMAPFILEHEADER));
if(pProgressCntrl)
pProgressCntrl->SetPos(50);
pFile->Write(m_hDIB, m_dwDIBSize);
if(pProgressCntrl)
pProgressCntrl->SetPos(100);
return TRUE;
}
catch(...)
{
CString s;
s.Format("ERROR: Problem writing to BMP file \"%s\".", pFile->GetFilePath);
AfxMessageBox(s);
}
return FALSE;
} I hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
Hi,
I have an IP address in a CString (actually a char[])
how can i check whether the syntax is correct
Ne snipplets???
Thanx
Sameer
|
|
|
|
|
Sample function for verifing that the address has format like:
XXX.XXX.XXX.XXX
where XXX is a number between 0 and 255
bool CDummyDlg::checkIP(char *ip)
{char* tmpIP = ip;
char* tmpNR = new char[4];
bool OK = true;
memset(tmpNR,0,4);
int len = 0, groups = 0;
do
{if (*tmpIP >= '0' && *tmpIP <= '9')
if(len < 4)
tmpNR[len++] = *tmpIP;
else
OK = false;
else if('.' == *tmpIP || 0 == *tmpIP)
{if (atoi(tmpNR) > 255)
OK=false;
memset(tmpNR,0,4);
len = 0;
groups++;
}
else
OK = false;
}
while(*tmpIP++ && OK);
delete tmpNR;
return OK && (4 == groups);
}
I inserted a lot of comments, hoping it would be easier to read.
/moliate
Two o'clock and walking through familiar London - Or what was familiar London before the cursor deleted certain certainties -
I watch a suit and tie man giving suck to the Psion Organizer lodged in his breast pocket
its serial interface like a cool mouth hunting his chest for sustenance, familiar feeling, and I'm watching my breath steam in the air.
Neil Gaiman - Cold Colours
|
|
|
|
|
Hi,
I have an object of my class CSomeClass and this
object is global.
Now i have multiple threads running in my
application and they access this global object.
How do i make it thread safe.. like does it require
some critical section code or mutex etc..
Thanx in advance
Sameer
|
|
|
|
|