64,688
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
//函数声明
BOOL InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
//变量说明
HWND hWnd; //窗口句柄
//************************************************************
//函数:WinMain( )
//功能:Windows程序入口函数。创建主窗口,处理消息循环
//************************************************************
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if ( !InitWindow( hInstance, nCmdShow ) ) return FALSE; //创建主窗口
//如果创建不成功则返回FALSE并同时退出程序
MSG msg;
//进入消息循环:
for(;;)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if ( msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//************************************************************
//函数:InitWindow( )
//功能:创建窗口
//************************************************************
static BOOL InitWindow( HINSTANCE hInstance, int nCmdShow )
{
//定义窗口风格:
WNDCLASS wc;
wc.style = NULL;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = CreateSolidBrush(0x0); //暗红色的背景
wc.lpszMenuName = NULL;
wc.lpszClassName = "My_Test";
RegisterClass(&wc);//注册窗口
//按所给参数创造窗口
hWnd = CreateWindow("My_Test",
"I'll be the one!",
WS_OVERLAPPEDWINDOW,//WS_POPUP|WS_MAXIMIZE,
CW_USEDEFAULT,//0,
CW_USEDEFAULT,//0,
CW_USEDEFAULT,//GetSystemMetrics( SM_CXSCREEN ), //此函数返回屏幕宽度
CW_USEDEFAULT,//GetSystemMetrics( SM_CYSCREEN ), //此函数返回屏幕高度
NULL,NULL,hInstance,NULL);
if( !hWnd ) return FALSE;
ShowWindow(hWnd,nCmdShow);//显示窗口
UpdateWindow(hWnd);//刷新窗口
MoveWindow(hWnd,150,100,400,500,true);
RECT rect;
GetClientRect(hWnd,&rect); //....................
//GetWindowRect(hWnd,&rect);
ShowCursor(FALSE); //....................
return TRUE;
}
//************************************************************
//函数:WinProc( )
//功能:处理窗口消息
//************************************************************
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}
case WM_MOUSEMOVE:
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor
if(yPos >= (rect.top) && yPos <= rect.bottom)
ShowCursor(FALSE);
else
ShowCursor(TRUE);
break;
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
int xPos;
int yPos;
switch( message )
{
case WM_QUIT:
ExitProcess(0);
break;
case WM_CLOSE:
SendMessage(hWnd, WM_QUIT, NULL, NULL);
break;
case WM_MOUSEMOVE:
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor
if(yPos >= (rect.top) && yPos <= rect.bottom)
ShowCursor(FALSE);
break;
default:
break;
}
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}
ShowCursor
The ShowCursor function displays or hides the cursor.
int ShowCursor(
BOOL bShow // cursor visibility
);
Parameters
bShow
[in] Specifies whether the internal display counter is to be incremented or decremented. If bShow is TRUE, the display count is incremented by one. If bShow is FALSE, the display count is decremented by one.
Return Values
The return value specifies the new display counter.
Remarks
This function sets an internal display counter that determines whether the cursor should be displayed. The cursor is displayed only if the display count is greater than or equal to 0. If a mouse is installed, the initial display count is 0. If no mouse is installed, the display count is –1.
Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
See Also
Cursors Overview, Cursor Functions, ClipCursor, GetCursorPos, SetCursor, SetCursorPos
Built on Thursday, May 11, 2000