孙鑫 Lesson1 window
2016-07-29 11:45
267 查看
#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//设计窗口
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Test2016";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); //注册窗口
//创建窗口、显示窗口、更新窗口
HWND hwnd;
hwnd=CreateWindow("Test2016","Test C++'s window",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 0;
}
//窗口回调函数
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"Test",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","Test",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"计算机编程语言C++",strlen("计算机编程语言C++"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,0,0,"testA",strlen("testA"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","Test",0))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//设计窗口
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Test2016";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); //注册窗口
//创建窗口、显示窗口、更新窗口
HWND hwnd;
hwnd=CreateWindow("Test2016","Test C++'s window",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 0;
}
//窗口回调函数
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"Test",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","Test",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"计算机编程语言C++",strlen("计算机编程语言C++"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,0,0,"testA",strlen("testA"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","Test",0))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
LPCTSTR,其中T为宏 宏定义,用_T可以保证从ascii编码类型转换到unicode编码类型的时候,程序不需要修改, 用_T来保证兼容性 Fn=function、L=long、 W=WORD、 H=handle、 U=unsign C=const ptr=pointer P=pointer cb=count of bytes Sz=string terminate by 0、 n=number Abbreviation LONG_PTR 表示32位与64环境下都兼容,32位下4个字节,64位环境下8个字节
相关文章推荐
- 过滤淘宝网弹出窗口
- 三个不同文件停顿二秒弹出不同窗口的批处理
- C#中父窗口和子窗口之间控件互操作实例
- Android开发系列三之窗口的常用事件
- jQuery窗口、文档、网页各种高度的精确理解
- C#实现简单的Login窗口实例
- C# Winform窗口之间传值的多种方法浅析
- C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法
- WPF弹出自定义窗口的方法
- WPF实现渐变淡入淡出的登陆窗口效果
- JavaScript实现模仿桌面窗口的方法
- 详解C#切换窗口
- js弹出窗口返回值的简单实例
- javascript实现浏览器窗口传递参数的方法
- JavaScript简单实现弹出拖拽窗口(二)
- Jquery仿IGoogle实现可拖动窗口示例代码
- jQuery实现鼠标悬停显示提示信息窗口的方法
- JavaScript实现上下浮动的窗口效果代码
- jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
- Boostrap模态窗口的学习小结