|
No worries and since you came back can I offer you a piece of code that can very quickly reject all the trivial shape areas that do not require drawing when moving in your current code.
You need to set the "dragging" flag to TRUE when you are moving a shape and set it back to FALSE when done dragging. I don't have the structures so you need to feed in x1,y1,x2,y2 into the two /* xxxxx */. So for each Area x1,y1 needs to be top left corner, x2,y2 lower right corner of the on screen minimum box around the shape. It puts an extra very fast test on redrawing a shape if the flag is set and I will leave you to work out what it does but it is commented
BOOL CanAreasOverlap (int Area1_x1, int Area1_y1, int Area1_x2, int Area1_y2, int Area2_x1, int Area2_y1, int Area2_x2, int Area2_y2){
if (Area1_x1 > Area2_x2) return (FALSE);
if (Area1_x2 < Area2_x1) return (FALSE);
if (Area1_y1 > Area2_y2) return (FALSE);
if (Area1_y2 < Area2_y1) return (FALSE);
return (TRUE);
}
BOOL dragging = FALSE;
OnDraw(CDC* pDC){
for (int i = 0; i < numberObjects; i++){
if ((!dragging) || CanAreasOverlap(, )) {
objects[i]->DrawShape(pDC);
}
}
}
When you drag a shape and go to redraw
In vino veritas
|
|
|
|
|
If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.
|
|
|
|
|
Ideally you avoid any activity that would throw an exception in the constructor. Instead of using new on the constructor. Mark the pointers as nullptr and then have a separate init function.
But you might try
new (std::nothrow)
which will eat the exception and instead return nullptr.
or you can do something like.
try {
ptr = new char[100];
} catch(std::bad_alloc& e) {
}
But overall, is a bad idea to throw on a constructor.
|
|
|
|
|
Move the memory allocation to a class of its own so that the new happens in its constructor and delete happens in its destructor. That should be the only responsibility of the allocating class.
In the class whos constructor throws, create an object of the allocating class on the stack.
Since destructors are only called for objects that are fully constructed, the allocating class destructor is always guaranteed to be called.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
|
I need to send a string or char to a "POS Receipt Printer" via USB.
My problem is how to establish a communication with the printer by USB port in C++ (I'm using MFC)
I tried this:
HANDLE dev = CreateFile(
"\\\\.\\USB001",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(dev!=INVALID_HANDLE_VALUE)
{
DWORD dwWritten = 0;
LPCSTR lpszTest = "Hello world!";
::WriteFile(dev, lpszTest, strlen(lpszTest), &dwWritten, NULL);
::CloseHandle(dev);
}
but, it returns by GetLastError() a value = 2 (Can not find the specified file)
Also, I tried this:
HANDLE dev;
CString s("\\\\.\\USB001");
LPWSTR p=(LPWSTR)(LPCTSTR)s;
if(OpenPort(p,(PHANDLE)&dev))
{
CString str = "Hello world!";
LPBYTE pByte = new BYTE[str.GetLength()+1];
memcpy(pByte,(VOID*)LPCTSTR(str),str.GetLength());
LPDWORD r;
r=0;
WritePort(dev,pByte,6,r);
delete [] pByte;
}
But I got frustrated,
due that OpenPort and WritePort belongs to #include <winsplp.h> and <winspool.h> , so it generates a lot of dependencies (libs,dlls) that escapes my knowledge.
If anyone can help me, I will be really grateful.
Yany
|
|
|
|
|
To open a handle to a USB device, you have to use the SetupDixxx APIs to enumerate and find the device.
Here is an example for enumerating USB devices - http://www.velleman.eu/images/tmp/usbfind.c[^]
Once the correct VID/PID pair is found, you can use the DevIntfDetailData->DevicePath in a call to CreateFile .
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Just a guess, but have you tried something like:
CreateFile("\\\\.\\USB#VID_0781&PID_5530#077443117B924E40#{a5dcbf10-6530-11d2-901f-00c04fb951ed}", ...); or if your USB drive has a drive letter mapped to it:
CreateFile("\\\\.\\D:", ...);
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi
Our project uses a stl map to save meta info. The map is the entrance of all operations, include read / write / update, etc. It must be very efficient.
Every item of the map is about 0.1kB. Now the length is 1M at most. So, the memory used is not over 100M. It is OK by now.
However, in the further, the length might be 1G or more. We have to consider a distrusted solution with high performance…
We tested memcached / Redis. The performance is not so good (no more than 5k query per seocns, need time to optimise.) What’s more, both the solution (memcached / redis) bring many issues for product deployment.
Is there a light weight solution for distrusted map in memory? For example, hold 3 maps in 3 servers and a meta in primary. In detail,
1. All query / update / insert / delete operation is finished in primary
2. If the info is in primary, do it locally, otherwise, send request (query / insert / update / delete) to peer.
3. Three parts of information can be balanced dynamically.
Thanks
Jack
|
|
|
|
|
Hi all,
I need some help from you: I try to render an SVG image on the screen (with alpha channel). The problem is, when I try to draw the image on the screen, I see absolutely nothing.
First of all, my SVG image is loaded correctly, and the ATL CImage from the below code, contains a correct image. The problem start when I try to use Gdiplus + alpha channel.
For instance, if I use PixelFormat24bppRGB instead of PixelFormat32bppARGB, the image is drawn correctly, but alpha channel is not preserved - a black halo is shown around my image.
Do you see any problem in my code?
PS: I use Win7, 32 bit + Visual Studio 2013 Ultimate.
Many thanks!
void CAppView::OnDraw(CDC* pDC)
{
int width = 600, height = 600;
GError* pError = NULL;
rsvg_init();
g_my_svg = rsvg_handle_new_from_file("d:\\myImage.svg", &pError);
rsvg_handle_get_dimensions(g_my_svg, &g_dimension);
cairo_surface_t *surface = cairo_win32_surface_create_with_dib(CAIRO_FORMAT_ARGB32, width, height);
cairo_t* cr = cairo_create(surface);
rsvg_handle_render_cairo(g_my_svg, cr);
HDC srcHDC = cairo_win32_surface_get_dc(surface);
CImage image;
image.Create(width, height, 32);
HDC imageHDC = image.GetDC();
TransparentBlt(imageHDC, 0, 0, width, height, srcHDC, 0, 0, width, height, RGB(0, 0, 0));
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Gdiplus::Bitmap bmp(image.GetWidth(), image.GetHeight(), image.GetPitch(), PixelFormat32bppARGB, static_cast<BYTE*>(image.GetBits()));
Gdiplus::Graphics graphics(pDC->GetSafeHdc());
graphics.DrawImage(&bmp, Gdiplus::Rect(0, 0, width, height));
Gdiplus::GdiplusShutdown(gdiplusToken);
image.ReleaseDC();
image.Destroy();
cairo_surface_flush(surface);
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
|
|
|
|
|
Can I first say you have no error checking at all in that code ... really bad programming practice
cairo_win32_surface_get_dc can fail and return NULL ... read the manual.
Why might there not be a win32 surface, well because you have to set a #define CAIRO_HAS_WIN32_SURFACE 1 in the cairo library .... again in the manual.
You code is totally reliant on srcHDC being valid ... Yet inexplicably you don't check it.
Secondly you probably want an alpha channel on the CIMage .. the create should be CreateEx
CImage image;
image.CreateEx(width, height, 32, BI_RGB, 0, 0);
Thirdly read TransparentBlt detail and if we do the Alpha channel it will then copy the source alpha onto the destination alpha ... is that Ok? If not you have to use AlphaBlend function.
Fourth check GDIPlus I seem to remember it had issues with Alpha channels.
Final comment is you seem to be making life difficult for yourself using 3 different systems for reasons that aren't clear. You are moving bitmap data around between systems for no real reason what you want can be done in any of the 3 systems and even natively on the Win32 API. You are even initializing the GDI plus system with a draw process ... like seriously please don't do it are you really hell bent on making the draw as slow as possible. Start the GDIPlus with the window or program and close it when done but not in a draw process ever.
In vino veritas
|
|
|
|
|
Actually the problem was related to the way of copying from srcHDC to ImageHDC. I have done like this:
HDC svgDC = cairo_win32_surface_get_dc(surface);
image.Create(width, height, 32);
image.SetHasAlphaChannel(TRUE);
HDC imageHDC = image.GetDC();
BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(imageHDC, 0, 0, width, height, svgDC, 0, 0, width, height, blend);
image.ReleaseDC();
Best regards!
|
|
|
|
|
|
When? How?
Sidenote: Was this meant to be a question, rant or discussion topic?
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
|
|
He might need it, but I doubt that he can get any decent beer in Iran.
(See the guy's profile)
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
I think he has already had a few too many.
Speed of sound - 1100 ft/sec
Speed of light - 186,000 mi/sec
Speed of stupid - instantaneous.
|
|
|
|
|
Hi
I am new to VC++/MFC but have C++ background.
I am using addpage() of CPropertySheet class to add new tabs. My problem is that on click of a radiobutton control all tab pages should be disable except one.
How to solve it?
Vivek
|
|
|
|
|
This is how I've done it when the need arises.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Anybody can please provide the solution?
|
|
|
|
|
I sent you a link to a working solution. I've used it in several projects.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
VS2008 unicode mfc project. Heavy use of CString variables. Other than the HMI display of text to the user, all other text is english. So, I might have something like this:
CString stmp;
stmp.Format(_T("%s"), _T("192.168.0"));
displaying in the debugger: stmp.m_pszData ---- 192.168.0
other times, I'll get what I guess is the unicode version --- 㤱⸲㘱⸸⸰
Anyone seen this weird behavior? I can run the program multiple times, and it seems like the debugger's presentation can change. I've not found the pattern yet.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Have you tried %S instead?
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Darn, someone responded before I could delete my silly post. I'm converting strings incorrectly (most of the IP work on Windows uses char and now wchar_t. So, self inflicted injury.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|