Sup guys,
This time I would like to know how to work with SetCapture and ReleaseCapture and would like a complete example.
Thanks.
Printable View
Sup guys,
This time I would like to know how to work with SetCapture and ReleaseCapture and would like a complete example.
Thanks.
Quote:
Originally Posted by .pcbrainbuster
Code:SetCapture(yourHwnd);
// do your stuff
ReleaseCapture();
Thanks for your post, but it seems that I wasn't clear enough. What I wanted was HOW to work with it.
What do you mean "How to work with it"?
Just call SetCapture at the moment you want to start the capture and call ReleaseCapture to release it.
It took me long but know I know how to use it and it seems that this is the wrong method I'm using -
This code has so many problems and it is all because of the method used to capture the mouse, you even have to click in the program to start the capture and then go outside for it to still be drawing. One problem is that when the desktop as the mouse in it's power my program hangs. What I want this TEST program to do is to be able to start drawing even when the mouse is held down while its outside my window. How do you do this?Code:#include <windows>
const char *ClsName = "BasicApp";
const char *WndName = "A Simple Window";
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
HINSTANCE hInstance;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;
// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the application
RegisterClassEx(&WndClsEx);
// Create the window object
hWnd = CreateWindowEx(NULL,
ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// Find out if the window was created
if( !hWnd ) // If the window was not created,
return 0; // stop the application
// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
SetCapture(hWnd);
// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
static POINT Area;
static int Draw = 0;
static RECT Point;
switch (Msg) {
case WM_DESTROY :
PostQuitMessage(WM_QUIT);
ReleaseCapture();
break;
case WM_LBUTTONUP :
Draw = 0;
break;
case WM_LBUTTONDOWN :
Draw = 1;
break;
case WM_CAPTURECHANGED :
SetCapture(hWnd);
break;
case WM_MOUSEMOVE :
HDC DC = GetDC(hWnd);
if (Draw == 1) {
GetCursorPos(&Area);
ScreenToClient(hWnd, &Area);
LineTo(DC, Area.x+1, Area.y+1);
GetClientRect(hWnd, &Point);
MoveToEx(DC, Point.right, Point.top, NULL);
LineTo(DC, Area.x+1, Area.y+1);
MoveToEx(DC, Point.left, Point.bottom, NULL);
LineTo(DC, Area.x+1, Area.y+1);
MoveToEx(DC, Point.right, Point.bottom, NULL);
LineTo(DC, Area.x+1, Area.y+1);
}
ReleaseDC(hWnd, DC);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
Thanks.
According to MSDN:
So what you want is not possible with SetCapture.Quote:
Originally Posted by MSDN
But if you implement what you want, meaning start drawing even when clicking outside your window, how can the user switch to a different application? You should not do this.
This is a test you know, meaning that I simply when to test the mouse capture and that's simply it... When I know how to do it I will be changing the code to also draw on the screen and not just the application, but that will also be a test the main goal in the future would be to use it in combination with WindowFromPoint and many others.
question -
1) In my country/area when you sign-up for a new school they ask whether many things including what you would/could do for the school in return, is the ability to program good or not? I mean FOR the school.
Thanks.
If you want to capture mouse events outside of your window, you need to use windows hook. Try to browse the articles at www.codeguru.com. There are some explaining mouse hooks.
Aww, but I do not know how to work with DLLs. Can you please direct me to a tutorial?
Thanks.
Please :( Look at the "cute"(ekhh) face(:)).
Have you tried googling 'dll tutorial'?
Besides google, did you search the articles at codeguru.com? There is probably example code which you can learn from.
Hey. I always though the way to 'draw' over the desktop was to create a transparent window to cover the desktop. The only problem is that the user will not be able to interact with anything under your transparent window anymore. But I guess that is your intention, since you only want to draw stuff into your DC, and not do much else with your app. except highlight things? Is this a CBT application?
try : Browse this ancient thread http://www.codeguru.com/forum/showthread.php?t=433664