2014-03-02
外面啊!
示例:
#include <windows.h>
#include "resource.h"
#include <stdio.h>
int x,y;
LRESULT CALLBACK myproc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CLOSE:
if(IDOK==MessageBox(hwnd,"你是真的要退出吗?","系统提示",MB_OKCANCEL|MB_ICONINFORMATION))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
case WM_CHAR:
{
char buf[100];
sprintf(buf,"%d",wParam);
MessageBox(hwnd,buf,"show",MB_OK);
break;
}
case WM_MOUSEMOVE:
{//这里和上面的情况用大括号一是为了防止buf的重复定义,二是为解决类似int x=LOWORD(lParam);的定义编译器糊涂报错。
HDC hdc=GetDC(hwnd);
char buf[100];
sprintf(buf,"x=%d,y=%d",x,y);
SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,0,0,buf,strlen(buf));
//InvalidateRect(hwnd,NULL,true);
//PostMessage(hwnd,WM_PAINT,0,0);
//SendMessage(hwnd,WM_PAINT,0,0);
x=LOWORD(lParam);
y=HIWORD(lParam);
memset(buf,0,100);
sprintf(buf,"x=%d,y=%d",x,y);
SetTextColor(hdc,RGB(0,0,0));
TextOut(hdc,0,0,buf,strlen(buf));
ReleaseDC(hwnd,hdc);
break;
}
}
return 1;
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=NULL;
wndclass.cbWndExtra=NULL;wndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
//wndclass.hCursor=LoadCursor(NULL,IDC_HAND);
wndclass.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_POINTER));//wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=myproc;
wndclass.lpszClassName="mywnd";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW;
RegisterClass(&wndclass);
HWND hwnd=CreateWindow("mywnd","window test",WS_OVERLAPPEDWINDOW,100,100,400,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
MSG msg;
while(GetMessage(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}