|
InvalidateRect(hWnd,NULL,TRUE);
The third parameter tells the system whether to erase the background on the next call to BeginPaint() ; change it to FALSE to prevent the erase. You can also trap the WM_ERASEBKGND message and provide your own code to redraw the background however you like.
The best things in life are not things.
|
|
|
|
|
thanks works, but why when i resize or maximize the window it doesn't work very well?
|
|
|
|
|
Member 2965471 wrote: ...it doesn't work very well?
Is that supposed to be helpful?
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
sorry...well when i increase the size of the window, the two lines partially disappear under the bitmap, while when i maximize the window the two lines do not appear and even the background image is distorted, may you help me please?
|
|
|
|
|
I think you should be drawing the bitmap on the temporary DC, then drawing the lines on top of that, and then BitBlt'ing the temporary DC onto the screen DC.
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
i've tried, when i maximize the window bitmap disappear and when i restoring the window the bitmap reappears, it seems that the coordinates are different when the window is maximize, it may be true?
|
|
|
|
|
Member 2965471 wrote: it seems that the coordinates are different when the window is maximize, it may be true?
That depends on if you are using screen or client coordinates. With the former, the coordinates will always change. With the latter, top and left will always be 0.
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
Where are you calling InvalidateRect from? Don't do it in your WM_PAINT handler!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
no no of course, i call it from WM_MOUSEMOVE.
|
|
|
|
|
You're joking, right?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i don't understand, why? i post my code in the first msg...
|
|
|
|
|
Well it's not as bad as the recursion you'd get from calling it in response to WM_PAINT, but it certainly doesn't need to be called on every WM_MOUSEMOVE message.
Maybe call it from WM_SIZE handling code instead...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I need to call it on mouse move because i need to draw two lines converging at the mouse cursor position.
|
|
|
|
|
Ok then I would double-buffer as mentioned by DavidCrow and use Invalidate();UpdateWindow() combination.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
 Since this is fun and I haven't touched C++ in years, here's a double-buffered version...
Note I moved stuff out of the WM_PAINT handler that didn't need to be done every paint. For example, the offscreen buffer is only (re)created when the window size changes, the client rect is only updated when the window size changes, and the bitmap and its associated memoryDC are only loaded/created once (in WM_CREATE handler in the sample code).
Flicker free!
POINT pt;
BITMAP Bitmap;
HANDLE hBitmap = 0;
HDC hBitmapMemoryDC = 0;
RECT ClientRect;
HBITMAP hOffScreenBitmap = 0;
HDC hOffScreenMemoryDC = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
hBitmap = ::LoadImage(hInst, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
::GetObject(hBitmap, sizeof(Bitmap), &Bitmap);
hBitmapMemoryDC = ::CreateCompatibleDC(0);
::SelectObject(hBitmapMemoryDC, hBitmap);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
::StretchBlt(hOffScreenMemoryDC, 0, 0, ClientRect.right, ClientRect.bottom, hBitmapMemoryDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, SRCCOPY);
::MoveToEx(hOffScreenMemoryDC, pt.x , 0 , NULL);
::LineTo(hOffScreenMemoryDC, pt.x, ClientRect.bottom);
::MoveToEx(hOffScreenMemoryDC, 0, pt.y, NULL);
::LineTo(hOffScreenMemoryDC, ClientRect.right, pt.y);
::BitBlt(hdc, 0, 0, ClientRect.right, ClientRect.bottom, hOffScreenMemoryDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_MOUSEMOVE:
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
::InvalidateRect(hWnd,NULL,FALSE); ::UpdateWindow(hWnd);
break;
case WM_ERASEBKGND:
return 1;
case WM_SIZE:
ClientRect.left = 0;
ClientRect.top = 0;
ClientRect.right = LOWORD(lParam);
ClientRect.bottom = HIWORD(lParam);
if (hOffScreenBitmap != 0)
::DeleteObject(hOffScreenBitmap);
if (hOffScreenMemoryDC != 0)
::DeleteDC(hOffScreenMemoryDC);
hdc = ::GetDC(hWnd);
hOffScreenMemoryDC = ::CreateCompatibleDC(hdc);
hOffScreenBitmap = ::CreateCompatibleBitmap(hdc, ClientRect.right, ClientRect.bottom);
::SelectObject(hOffScreenMemoryDC, hOffScreenBitmap);
::ReleaseDC(hWnd, hdc);
break;
case WM_DESTROY:
if (hBitmap != 0)
::DeleteObject(hBitmap);
if (hBitmapMemoryDC != 0)
::DeleteDC(hBitmapMemoryDC);
if (hOffScreenBitmap != 0)
::DeleteObject(hOffScreenBitmap);
if (hOffScreenMemoryDC != 0)
::DeleteDC(hOffScreenMemoryDC);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Mark Salsbery
Microsoft MVP - Visual C++
modified on Thursday, May 26, 2011 8:30 PM
|
|
|
|
|
It almost sounds like your asking how to rubberband a line. If not, ignore my comments but if you are looking to have the bitmap drawn in your WM_PAINT handler and you want to stretch a line from some starting point to where the mouse cursor is, this is normally referred to as "rubberbanding".
Here's an old post of mine describing how to rubberband a line (btw, the idea is basically the same for a rectangle). Rubberbanding Lines[^]
NOTE: One you commit to the line being at some location permanently, you would need to keep track of that object and also draw it in your WM_PAINT handler but only after you no longer want to rubberband it. I reference the book by Ivor Horton which is truly a must have if your going to pursue a drawing project as he basically builds one in the second half of the book.
Also, this is all using MFC but the concepts can be easily adjusted to WIN32.
|
|
|
|
|
Hello,
I need help in converting these two function into one function (since basicaly it is the same code).
Does anoyone have an idea ?
struct File_Writer
{
File_Writer(ofstream& output) : m_output(output) { ; }
void operator(MyStruct& ms)
{
output << ms.a << endl;
output << ms.b << endl;
}
ofstream& m_output; };
struct Display_Writer
{
void operator(MyStruct& ms)
{
cout << " a: " << ms.a << endl;
cout << " b: " << ms.b << endl;
}
};
Thanks, Berlus
|
|
|
|
|
Berlus wrote: void operator(MyStruct& ms)
You first need to show code that actually compiles.
Berlus wrote: I need help in converting these two function into one function (since basicaly it is the same code).
Since they are outputting to two unrelated streams, what exactly is it you think can be merged?
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
Since all output stream classes inherit from ostream, you can do this:
using std;
struct MyStruct {
int a;
double b;
};
ostream& operator<<(ostream& output, MyStruct& ms) {
output << " a: " << ms.a << endl;
output << " b: " << ms.b << endl;
return output;
}
int main () {
MyStruct ms;
ms.a = 3;
ms.b = 3.1415926;
ofstream myfile("test.txt");
myfile << ms;
cout << ms;
}
[edit]fixed implementation[/edit]
modified on Thursday, May 26, 2011 9:31 AM
|
|
|
|
|
Thanks for the quick response.
Is there a way to insert the "serialization" code into the struct itself ? something like:
#include <iostream>
#include <fstream>
using namespace std;
struct MyStruct
{
ostream& operator<<(ostream& output) {
output << " a: " << a << endl;
output << " b: " << b << endl;
return output;
}
int a;
double b;
};
int main(void)
{
MyStruct ms;
ms.a = 5;
ms.b = 4.2;
cout << ms;
ofstream my_file("me.txt");
my_file << ms;
return 0;
}
I tried it, and got compilation errors.
IntelliSense: no operator "<<" matches these operands
I am trying to create some sort of a generic ToString.
Thanks,
Berlus
|
|
|
|
|
Hehe, that's what I first did, and hence my edit.
Unfortunately the streaming operators expect the type to be streamed as second parameter. If it were the first parameter, then your code would work, as in class methods the instance of the class itself is always passed as a hidden parameter.
So, what you can do, is overriding the subtraction operator like in either of these ways:
struct MyStruct {
int a;
MyStruct(int value) : a(value) {}
MyStruct operator-(const MyStruct& op2) {
return (a-op2.a);
}
};
or
struct MyStruct {
int a;
MyStruct(int value) : a(vlaue) {}
};
MyStruct operator-(const MyStruct& op1, const MyStruct& op2) {
return op1.a - op2.a;
}
Both implementations are equivalent. The only difference is that the first variant skips the first parameter of the second variant, because the instance will be used in its stead.
|
|
|
|
|
Hi!
I'm trying to open a URL from a Dialog in MFC with the following code.
CWebBrowser2 m_WebBrowserCtrl.Navigate(URL.c_str(), NULL, NULL, NULL, NULL);
But it navigates to the URL at the computers where IE is installed. Otherwise it's not working. I've to read the default browser from Regsitry and open the URL with that browser. How to do this in MFC?
|
|
|
|
|
Show the contents of your URL so people can see if it is valid.
The best things in life are not things.
|
|
|
|
|
Richard MacCutchan wrote: Show the contents of your URL so people can see if it is valid.
It's here:
http://[^]
|
|
|
|
|
No I did not mean post a link to the site, I mean show the exact value that is in the variable at the time you get the error.
The best things in life are not things.
|
|
|
|
|