windows32挺好玩的,买本windows32的书,慢慢啃,理解windows32到底是怎么工作的,尤其是消息循环这一块,其实windows32最重要的就是你写的proc函数,消息循环接收任何消息,都调用你写的windowproc函数,如果有人觉得windows32入门很痛苦,建议大家看看孙鑫的c++20讲之第一讲,保证有收获。
windows32不用重头到尾都自己敲代码,可以有技巧的使用MSDN,可以复制函数原型... 写了第一遍了,第二遍就可以拷贝第一遍写的,然后改就行了。
有两种方法可以获取HDC(设备环境句柄)
1成对使用BeginPaint(hwnd,&ps),EndPaint(hwnd,&ps);
2成对使用GetDC(hwnd),ReleaseDC(hwnd,hdc);
就在刚刚,我分别用了两种方法测试了下效果:
#include<windows.h>
LRESULT CALLBACK WindowProc(
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
)
{
static TCHAR szAppName[]=TEXT("AppName");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass)){
MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR);
}
hwnd=CreateWindow(
szAppName, // registered class name
TEXT("This is title"), // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
TCHAR zsBuffer[20];
switch(uMsg){
case WM_CREATE:
//hdc=GetDC(hwnd);
//TextOut(hdc,0,0,TEXT("hello,world"),strlen("hello,world"));
//ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,TEXT("WM_PAINT"),strlen("WM_PAINT"));
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
跟预想的效果一样。
但是:
#include<windows.h>
LRESULT CALLBACK WindowProc(
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
)
{
static TCHAR szAppName[]=TEXT("AppName");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass)){
MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR);
}
hwnd=CreateWindow(
szAppName, // registered class name
TEXT("This is title"), // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
TCHAR zsBuffer[20];
switch(uMsg){
case WM_CREATE:
//hdc=GetDC(hwnd);
//TextOut(hdc,0,0,TEXT("hello,world"),strlen("hello,world"));
//ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=GetDC(hwnd);
TextOut(hdc,0,0,TEXT("WM_PAINT"),strlen("WM_PAINT"));
ReleaseDC(hwnd,hdc);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
窗体中显示了WM_PAINT,但是明显的看到一直在闪屏。
TextOut是用来输出的:
看看它的定义:
TextOut和DrawText都是用来写的函数,这两者有什么区别呢?其实只要查查MSDN这两个函数原型,你就都清楚了,确定具体位置的用TextOut,确定区域的用DrawText
有了函数原型了,一切都迎刃而解了。
哪位仁兄帮下忙,说一下上面两种方法获取的HDC有什么不同,为什么会出现闪屏?
已经找到答案了:
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//TextOut(hdc,0,0,TEXT("WM_PAINT"),strlen("WM_PAINT"));
//TextOut(hdc,200,200,TEXT("WM_PAINT"),strlen("WM_PAINT"));
//SetTextAlign(hdc,TA_RIGHT);
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
TextOut(hdc,i*50,j*50,TEXT("HELLO"),strlen("HELLO"));
}
}
EndPaint(hwnd,&ps);
return 0;
也可是使用TextOut输出变量:
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//TextOut(hdc,0,0,TEXT("WM_PAINT"),strlen("WM_PAINT"));
//TextOut(hdc,200,200,TEXT("WM_PAINT"),strlen("WM_PAINT"));
SetTextAlign(hdc,TA_RIGHT);
for( i=0;i<10;++i){
for( j=0;j<10;++j){
TextOut(hdc,i*50,j*50,szBuffer,wsprintf(szBuffer,TEXT("%d"),i*j));
}
}
//TextOut(hdc,200,200,szBuffer,wsprintf(szBuffer,TEXT("%d"),i*j));
EndPaint(hwnd,&ps);
return 0;
TEXTMERIC相关操作:
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static int cxClient,cyClient;
HDC hdc;
PAINTSTRUCT ps;
int i=50,j=20;
TCHAR szBuffer[20];
TEXTMETRIC tm;
switch(uMsg){
case WM_CREATE:
//hdc=GetDC(hwnd);
//TextOut(hdc,0,0,TEXT("hello,world"),strlen("hello,world"));
//ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetTextMetrics(hdc,&tm);
TextOut(hdc,0,0,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmAveCharWidth));//字符宽度
TextOut(hdc,0,50,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmHeight+tm.tmExternalLeading));//字符高度
EndPaint(hwnd,&ps);
return 0;
case WM_SIZE:
hdc=BeginPaint(hwnd,&ps);
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
GetTextMetrics(hdc,&tm);
TextOut(hdc,0,100,szBuffer,wsprintf(szBuffer,TEXT("%d"),cxClient/tm.tmAveCharWidth));//一行最多显示多少个字符
TextOut(hdc,0,150,szBuffer,wsprintf(szBuffer,TEXT("%d"),cyClient/(tm.tmHeight+tm.tmExternalLeading)));//一列最多显示多少字符
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
分享到:
相关推荐
iOS上使用TextOut输出文字,文字沿着半圆Path绘制
在不同开发环境中TextOut方法的正确使用
1、学会使用VC6.0建立一个Win32 Console Application工程程序内容为屏幕上分成3行输出自己的班级、姓名、学号。 2、学会使用VC6.0建立一个Win32Application工程,把附录的程序调通,并能够运行。 3、在实验内容(2...
GetWindowText 可以帮你提取 windows 窗口中不能复制的文字. Windows Text Copyer 可以复制你无法复制到的文字,例如注册表状态栏上的详细注册表路径。另外当把Windows Text Copyer的抓取按钮放到任务管理器进程中时...
手动读取bmp并textout显示,问题在于图片大小必须是偶数否则显示错误.rar 读24位的rgb位图.并显示
VC++中简单的TextOut输出文本字符的使用实例,新手有兴趣的源码,实现过程:获取窗口设备上下文,然后在窗口客户区域指定位置输出文本。 运行环境:Windows/Visual C/C++
学习MFC,用到垂直显示内容,在网上查找了一些方法,做一下总结。 在View类的头文件中增加CString m_strLeft; 在View类中增加WM_PAINT消息的函数OnPaint(),设置输出字体,设置颜色 CPaintDC dc(this); // ...
CDC::TextOut输出的字体只有加粗和不加粗2种效果,这是微软系统默认的效果,现在项目的需要,设置画笔宽度(使字体变粗变细),输出字体轮廓路径,调用API:BeginPath--》TextOut--》EndPath--》StrokeAndFillPath....
VC++自动绘制表格和输出文字, //函数功能:给定指定原点输出文字 //参数start_x,start_y,text分别为原点坐标x,y和文本的内容 void CDrawDlg::MyDrawText(int start_x,int start_y,CString text,int txtStyle) { CDC...
Textout:通过面向可视化的学习来检测移动应用程序中的文本布局错误
首先将坐标映射方式改变为MM_ANISOTROPIC方式,即各向异性的意思,在这种坐标...通过函数得到显示器和打印机每逻辑英寸的像素数量,然后对视口大小进行相应的缩放,就可以便利屏幕上的显示和打印机的输出是一致的了。
visual c++各种windows钩子 Hook API的使用示例代码 包括: hook file hook game hook internet hook reg注册表 hook socket hook textout字典的屏幕取词功能 hook 加载dll事件 hook cmd命令行
控件提供textout函数输出文本内容,cellout函数输出一个单元格(与textout函数的区别是有边框),此两个函数都具有自动换页功能,即如果输出的文本大于1页,可以自动增加新页并打印到下一页。 控件还提供了输出...
USB摄像头数据采集,摄像头参数获取,摄像头图像畸变校正。VC+OPENCV实现。
delphi中实现字体长度,宽度的拉伸
另外控件提供textout函数输出文本内容,cellout函数输出一个单元格(与textout函数的区别是有边框),此两个函数都具有自动换页功能,即如果输出的文本大于1页,可以自动增加新页并打印到下一页。 控件还提供了输出...
MFC环境下文本输出,包括路径层,DrawText函数使用等
VS2019对话框输入文字
另外控件提供textout函数输出文本内容,cellout函数输出一个单元格(与textout函数的区别是有边框),此两个函数都具有自动换页功能,即如果输出的文本大于1页,可以自动增加新页并打印到下一页。 控件还提供了输出...
控件提供textout函数输出文本内容,cellout函数输出一个单元格(与textout函数的区别是有边框),此两个函数都具有自动换页功能,即如果输出的文本大于1页,可以自动增加新页并打印到下一页。 控件还提供了输出图片...