Original Post
The windows are child windows that have their procs modified so that you can move them around, by selecting the top part of the window (top 20 pixels). But when you do, i get horrible flickering, i guess due to me calling InvalidateRect whenever the window is moved. I need to called invalidate, to clear the background of previous line drawing. I have pasted the whole app below, which is just a simplified version of my current app, to help make the problem clearer. could someone give me some ideas on how to reduce the flickering? and on a side not, when the user moves the window quickly, the cursor becomes in the incorrect place for detecting whether or not the user wants to move the window, ie not in that 20 pixel marking area, any ideas on this issue too?
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK EventProc (HWND, UINT, WPARAM, LPARAM) ;
HWND CreateEventWindow();
HINSTANCE hInstance;
HWND hApp ;
HWND SelWin;
int WINAPI WinMain (HINSTANCE _hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
MSG msg ;
WNDCLASS wndclass ;
hInstance = _hInstance;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hApp = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hApp, iCmdShow) ;
UpdateWindow (hApp) ;
HWND Event1 = CreateEventWindow();
CreateWindowEx(WS_EX_WINDOWEDGE , L"static", L"Node1", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 0, 198, 20, Event1, (HMENU) 0, hInstance, NULL);
CreateWindowEx(WS_EX_CLIENTEDGE , L"static", L"Node2", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 20, 198, 20, Event1, (HMENU) 0, hInstance, NULL);
CreateWindowEx(WS_EX_CLIENTEDGE , L"static", L"Node3", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 40, 198, 20, Event1, (HMENU) 0, hInstance, NULL);
CreateWindowEx(WS_EX_CLIENTEDGE , L"static", L"Node4", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 60, 198, 20, Event1, (HMENU) 0, hInstance, NULL);
SetWindowPos(Event1, 0, 500, 500, 200, 80, 0 );
//this code just enbles the first inital line
SelWin = HWND(1);
SendMessage(Event1, WM_MOUSEMOVE, 0, 0);
SelWin = 0;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT r ;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK EventProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int MX, MY;
POINT p;
RECT r;
HDC hdc ;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_DESTROY:
return 0 ;
case WM_LBUTTONDOWN:
if(HIWORD(lParam) <= 20 )
SelWin = hwnd;
return 0;
case WM_MOUSEMOVE:
if(SelWin != 0)
{
hdc = GetDC (hApp) ;
InvalidateRect(hApp, NULL, 0); //clear background of previous lines
p.x = LOWORD(lParam); p.y = HIWORD(lParam); //p is in Event client coordiantes
ClientToScreen(hwnd, &p); //p is now in screen
ScreenToClient(hApp, &p); //p is now in hApps client
SetWindowPos(SelWin, 0, //set the centre of the window at the mouse pos
p.x - 100,
p.y - 10, 0, 0, SWP_NOSIZE);
GetWindowRect( hwnd, &r); //draw a line from 0,0 to window corner
p.x = r.left; p.y = r.top;
ScreenToClient(hApp, &p);
MoveToEx (hdc, 0, 0, NULL) ;
LineTo (hdc, p.x, p.y) ;
ReleaseDC (hApp, hdc) ;
}
return 0;
case WM_LBUTTONUP:
SelWin = 0;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
HWND CreateEventWindow()
{
WNDCLASS wndclass ;
HWND Handle;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = EventProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = L"Event" ;
if (!RegisterClass (&wndclass))
{
//class already registered // ** need to fix this, no check for register failure **
}
Handle = CreateWindow (L"Event",
L"",
WS_CHILD | WS_BORDER,
300,
300,
200,
20,
hApp,
0,
hInstance,
0) ;
if(!Handle)
MessageBox(NULL, L"Window creation failure", L"win", NULL);
ShowWindow (Handle, SW_SHOWNORMAL) ;
UpdateWindow (Handle) ;
return Handle;
}