|
If you use the CStringT class you can do it via the SpanIncluding()[^] method.
It's time for a new signature.
|
|
|
|
|
It's not so true because, as the MSDN documentation says, SpanIncluding returns an empty substring if the first character in the string is not in the specified set.
|
|
|
|
|
Oops!
It's time for a new signature.
|
|
|
|
|
std::count_if is another method.
Something like
bool is_comma(char c)
{
return c == ',';
}
std::string s = "1,2,3,4";
int count = std::count_if(s.begin(), s.end(), is_comma);
Edit for clarification: This algorithm can of course be applied to a CString as well using the GetBuffer() member.
home
modified on Thursday, August 26, 2010 5:31 AM
|
|
|
|
|
Of course you don't need GetBuffer :
#include <algorithm>
using namespace std;
bool is_comma(TCHAR c)
{
return c==_T(',');
}
LPCTSTR beg = (LPCTSTR) str;
LPCTSTR end = beg + str.GetLength();
int result = count_if(beg, end, is_comma);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
Assuming your string is
CString str = _T("Sample(10,20,30,40,50)");
Then
int start = -1;
int count = 0;
while ( (start = str.Find(_T(','), start + 1)) != -1)
count++;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
raju_shiva wrote: i.e For ex: CString str = Sample("10,20,30,40,50");
I want to get the "," character count i.e 5
Shouldn't that be 4?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
You can use the CString::Tokenize[^] method by specifying "," as the token delimiter.
You can get the count by incrementing a variable in the while loop in the example in the above link.
|
|
|
|
|
Hi all,
dialog box in my application having so many controls.i want to print it.
i want to print dialog box screen.
please tell me how can i do this.
thank in advance.
|
|
|
|
|
I've answered a similar question in the QA section some time ago.
Check out my answer[^] which contains a working example.
|
|
|
|
|
its working fine thanks.
but dialog size display smaller than its real size.
|
|
|
|
|
Set your printer resolution same as your screen resolution and the problem should gone.
Another approach is to create the bitmap of the dialog based on the printer resolution, but in some cases
(on high resolution printers) this can generate huge bitmaps, so setting the printer resolution is easier and safer.
|
|
|
|
|
Nice piece of code and certainly worth a 5.
It's time for a new signature.
|
|
|
|
|
Thank you very much Richard!
|
|
|
|
|
its not display the OnPaint graphics in image.
|
|
|
|
|
Le@rner wrote: its not display the OnPaint graphics in image.
I'm not sure what you mean.
There is not OnPaint method in my code.
There is only OnPrint and it is working as expected, prints the dialog.
|
|
|
|
|
i draw a header on dialog box in OnPaint function.
|
|
|
|
|
In that case you should draw it again in on the bitmap of the dialog.
This can be done in OnPrint method.
Here is a snippet from the method with comments to show you where you can draw:
void CDlgTestDlg::OnPrint(CDC *pDC, CPrintInfo* pInfo)
{
HBITMAP hWndBmp = GetWndBmp(GetSafeHwnd());
RECT rect;
GetWindowRect(&rect);
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top;
CDC dcSrc;
dcSrc.CreateCompatibleDC(NULL);
const int nRestorePoint = dcSrc.SaveDC();
dcSrc.SelectObject(hWndBmp);
pDC->BitBlt(0, 0, nWidth, nHeight, &dcSrc, 0, 0, SRCCOPY);
dcSrc.RestoreDC(nRestorePoint);
dcSrc.DeleteDC();
DeleteObject(hWndBmp);
}
|
|
|
|
|
|
You are welcome!
|
|
|
|
|
One more query,
OnPaint graphics overlap with image and not display exactly same as dialog display,
if i draw graphics on OnPrint again.
there is any other option to print dialog with Paint graphics.
thanks.
|
|
|
|
|
This happens because the viewport of the bitmap dc is not synchronized with the client rect of the dialog.
Try something like that:
void CDlgTestDlg::OnPrint(CDC *pDC, CPrintInfo* pInfo)
{
HBITMAP hWndBmp = GetWndBmp(GetSafeHwnd());
RECT rect;
GetWindowRect(&rect);
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top;
CDC dcSrc;
dcSrc.CreateCompatibleDC(NULL);
const int nRestorePoint = dcSrc.SaveDC();
dcSrc.SelectObject(hWndBmp);
CRect rcClient, rcWindow;
GetWindowRect(rcWindow);
GetClientRect(rcClient);
CRect rcClientMoved(rcClient);
rcClientMoved.MoveToXY((rcWindow.Width() - rcClient.Width()) / 2,
(rcWindow.Height() - rcClient.Height() - GetSystemMetrics(SM_CYBORDER)));
CPoint ptViewPortOrg = dcSrc.GetViewportOrg();
dcSrc.SetViewportOrg(rcClientMoved.TopLeft());
dcSrc.SetViewportOrg(ptViewPortOrg);
pDC->BitBlt(0, 0, nWidth, nHeight, &dcSrc, 0, 0, SRCCOPY);
dcSrc.RestoreDC(nRestorePoint);
dcSrc.DeleteDC();
DeleteObject(hWndBmp);
}
This should do the job!
|
|
|
|
|
Hi, how to use iterators in file stream can any one explain me with sample code, that read and write files using iterator
|
|
|
|
|
Please explain what you are trying to achieve. Files and iterators are two completely different concepts.
|
|
|
|