Windows编程的基础问题
程序清单:
#include <windows.h>
#include <math.h>
POINT P[5];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
double theta;
bool p;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SquareDemo") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
theta = 0;
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 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("SquareDemo"), // 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 (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
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 rect ;
switch (message)
{
case WM_PAINT:
//hdc = GetDC(hwnd);
hdc = BeginPaint(hwnd,&ps);
P[0].x = P[4].x = 100 * cos(theta) - 100 * sin(theta) + 300;
P[0].y = P[4].y = 100 * cos(theta) + 100 * sin(theta) + 300;
P[1].x = (-100) * cos(theta) - 100 * sin(theta) + 300;
P[1].y = (-100) * sin(theta) + 100 * cos(theta) + 300;
P[3].x = 100 * cos(theta) + 100 * sin(theta) + 300;
P[3].y = 100 * sin(theta) - 100 * cos(theta) + 300;
P[2].x = (-100) * cos(theta) + 100 * sin(theta) + 300;
P[2].y = (-100) * sin(theta) - 100 * cos(theta) + 300;
Polygon(hdc,P,5);
EndPaint(hwnd,&ps);
//ReleaseDC(hwnd,hdc);
return 0 ;
case WM_LBUTTONUP:
theta += .1;
UpdateWindow(hwnd);//***********
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
这段程序在一个窗口上画一个正方形,然后当鼠标左键点击时,旋转一个角度。
问题是:
为什么打星的一行UpdateWindow没有用。在鼠标点击后还是没有重画窗口?