#include "StdAfx.h"
//#include <windows.h>
#include <stdio.h>
//先定义回调函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
//窗口结构
WNDCLASS wndcls;
wndcls.style=CS_HREDRAW | CS_VREDRAW; //窗口类型
wndcls.lpfnWndProc=WinSunProc; //回调函数,接受窗口消息并处理
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hInstance=hInstance; //程序运行时传进来的Imagebase
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); //左上角图标
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS); //鼠标类型
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); //背景色
wndcls.lpszMenuName=NULL; //菜单名
wndcls.lpszClassName="ganxiang2020"; //窗口名字
//注册窗口
int ret = RegisterClass(&wndcls);
//创建窗口
HWND hwnd;
hwnd=CreateWindow("ganxiang2020","我的第一个窗口",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);
//显示窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
//更新窗口
UpdateWindow(hwnd);
//消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//回调函数实现
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
//接受键盘按键时
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
//鼠标左键
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"鼠标左键点击了!",strlen("鼠标左键点击了!"));
//ReleaseDC(hwnd,hdc);
break;
//窗口最大化最小化还原传递消息
case WM_SIZE:
char s[50];
sprintf(s,"%d -- %d\n",wParam,lParam);
OutputDebugString(s);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
//其他情况交给系统默认处理
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}