|
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.
|
|
|
|
|
The error does not occur in my system. It occurs at a target machine. So I can't trace the value by debugging.
modified on Thursday, May 26, 2011 9:28 AM
|
|
|
|
|
You really need to go away and think about the details of this problem and how you can provide a clear explanation of :
- what you are trying to achieve
- what your code is doing
- what are the actual values of any variables that the code is using
- what results you are seeing
These vague one line statements are not a lot of help, I'm afraid.
The best things in life are not things.
|
|
|
|
|
I would go for ShellExecute()[^]. It will open a document, in this case your URL, in the default application, in this case the default web browser.
|
|
|
|
|
It shows the following error:
APPLICATION ERROR:
This error has occurred for one of the following reasons:
(i) You have kept the browser window idle for a long time. Your session has expired.
(ii) You have logged in from another browser window.
(iii) You are accessing the application URL from a saved or static page.
|
|
|
|
|
Was the URL retrieved during a authenticated session? Then you're in trouble.
|
|
|
|
|
Yes. But I've to open this URL with the default browser in a System. How to do this?
|
|
|
|
|
Is it an option to open the website in your default browser and log in to the site manually, before trying to open the link? That could work.
|
|
|
|
|
Hi all,
How can i get the handle of the the window(i.e other dialog),if it doesnt have Windows caption.
I am trying with this code,
HWND hWnd1;
hWnd1=::FindWindow(NULL,"Test");
if(hWnd1!=NULL)
{
::SetDlgItemText(hWnd1,IDOK,"sadsd");
}
If i dont have caption means,how can i change the text.If any one has any idea,please let me know
Thanks
Manju
|
|
|
|
|
Hello,
in place of NULL you can try using class name (of dialog).
otherwise, you can use EnumChildWindows(...) and GetWindowText(..) to find the appropriate window and get hwnd.
Regards,
A. Gopinath.
|
|
|
|
|
Is it custom window application, where you want to change the caption of the window or it would going to be generic one. if it custom window: you can use findwindow and findwindowex to search the window application using windowclass, otherwise follow the method told in earlier post
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I am using firefox and dreamweaver for my webprojects.
when firefox or dreamweaver is closed they take a long time clearing all the virtual memory and things like that. so mostly i invoke taskmanager and end task firefox and dreamweaver. I dont have valuable information stored neither in ff nor in dreamweaver so abnormal termination is not a problem i think.
I wanted to write a win32 api program which when executed should find firefox or dreamweaver and should terminate them abruptly like what we do as endtask using taskmanager.
So i want to know where do i have to start in the documentation of windows programing to do some thing like this.
References, suggestions, answers and external links could be useful.
Thank You.
|
|
|
|
|