window move and call to sleep

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I wrote a little clock widget using win32 api in visual c++ 2008 express. I am quite satifyed with it since the default windows manager behavior is ok apart from when I tries to move the window. Then, if I keep the mouse left button pressed, the application
completly stalls, waiting for a mouse move and a mouse button release. I would like to keep the call to the Sleep function (with a finite delay) which I find meaningful without compromising the default window behavior. Could you help me?
Thank you in advance
Here is my code.

<div style="color:black; background-color:white
<pre>#include <span style="color:#a31515 "math.h"
#include <span style="color:#a31515 "stdio.h"
#include <span style="color:#a31515 "windows.h"

#define PI 3.1415926

#define SLEEP

<span style="color:blue int index;
HBRUSH brush;
HPEN pen;

LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

<span style="color:blue double radius = 100.0;
<span style="color:blue double center_x = 110.0;
<span style="color:blue double center_y = 110.0;

PAINTSTRUCT ps;
HDC hdc;
RECT r;

<span style="color:blue switch (message)
{

#ifndef SLEEP

<span style="color:blue case WM_TIMER:

<span style="color:blue if (wParam == 0x100) {
InvalidateRect(hwnd, NULL, <span style="color:blue true);
SendMessage(hwnd, WM_PAINT, NULL, NULL);
index ++;
} <span style="color:blue else {
<span style="color:blue break;
}
<span style="color:blue return 0;

#endif

<span style="color:blue case WM_PAINT:

hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, pen);
<span style="color:blue for(<span style="color:blue int i=0; i<60; i++) {
r.left = center_x+radius*cos(2*PI*i/60)-2;
r.top = center_y+radius*sin(2*PI*i/60)-2;
r.right = r.left+2;
r.bottom = r.top+2;
FillRect(hdc, &r, brush);
}
MoveToEx(hdc, center_x, center_y, NULL);
LineTo(hdc, center_x+(radius-10)*cos(2*PI*index/60), center_y+(radius-10)*sin(2*PI*index/60));
EndPaint(hwnd, &ps);
<span style="color:blue return 0;

<span style="color:blue case WM_DESTROY:

PostQuitMessage(0);
printf(<span style="color:#a31515 "Goodbye ;-)n");
exit(0);
<span style="color:blue return 0;
}

<span style="color:green // Process other messages.
<span style="color:blue return DefWindowProc(hwnd, message, wParam, lParam);
}

<span style="color:blue int main(<span style="color:blue int argc, <span style="color:blue char ** argv) {


HWND hwndMain;
index=0;
brush = CreateSolidBrush(RGB(0,0,0xFF));
pen = CreatePen(PS_SOLID, 1, RGB(0,0,0xFF));

HINSTANCE hInstance = GetModuleHandle(NULL);
HINSTANCE hPrevInstance = NULL;
LPSTR lpszCmdLine = NULL;
<span style="color:blue int nCmdShow=SW_SHOW;

HINSTANCE hinst;
WNDCLASS wc;
UNREFERENCED_PARAMETER(lpszCmdLine);

<span style="color:green // Register the window class for the main window.

<span style="color:blue if (!hPrevInstance)
{
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE) NULL,
IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL,
IDC_ARROW);
wc.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wc.lpszMenuName = <span style="color:#a31515 "MainMenu";
wc.lpszClassName = <span style="color:#a31515 "MainWndClass";

<span style="color:blue if (!RegisterClass(&wc))
printf(<span style="color:#a31515 "cannot register classn");
}

hinst = hInstance; <span style="color:green // save instance handle

<span style="color:green // Create the main window.

hwndMain = CreateWindow(<span style="color:#a31515 "MainWndClass", <span style="color:#a31515 "Clock",
WS_OVERLAPPEDWINDOW, 0, 0,
230, 250, (HWND) NULL,
(HMENU) NULL, hinst, (LPVOID) NULL);

<span style="color:green // If the main window cannot be created, terminate
<span style="color:green // the application.

<span style="color:blue if (!hwndMain)
printf(<span style="color:#a31515 "cannot create windown");

<span style="color:green // Show the window and paint its contents.
ShowWindow(hwndMain, nCmdShow);

#ifndef SLEEP

SetTimer(hwndMain, 0x100, 1000, NULL);

#endif

<span style="color:blue while(<span style="color:blue true) {

MSG msg;
<span style="color:blue while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
DispatchMessage(&msg);
}

#ifdef SLEEP

InvalidateRect(hwndMain, NULL, <span style="color:blue true);
SendMessage(hwndMain, WM_PAINT, NULL, NULL);

Sleep(1000);

index++;

#endif

}

<span style="color:blue return 0;
}
[/code]


<br/>

View the full article
 
Back
Top