文档库 最新最全的文档下载
当前位置:文档库 › mfc窗口实例详解

mfc窗口实例详解

#include"windows.h"
#include"iostream.h" ////我这用的是c++
////窗口原型函数(声明 可直接编写) 在编写部分详解////////////////////////
LRESULT CALLBACK WNDFUN ////要按需要更改“WNFUN”
(
HWND hWnd, // handle to window
UINT msg, // message identifier
WPARAM wparam, // first message parameter
LPARAM lparam // second message parameter
);

///程序的进入点///////////////////////////////////////////////////////////
int WINAPI WinMain //可以在MSDN查找关键字复制
(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
//窗口设计类
WNDCLASS wndcls;
wndcls.style =CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;//窗口类风格 见附录
wndcls.lpszClassName ="CustomWnd"; ////////////类名
wndcls.lpszMenuName =NULL; ////////////菜单
wndcls.lpfnWndProc =WNDFUN; ///////////窗口过程函数
wndcls.hInstance =GetModuleHandle(NULL); ////////////应用程序实例号
wndcls.hIcon =NULL; //标准 为null ////////////图标///////////可查MSDN赋值如=LoadIcon(NULL,IDI_ERROR)
wndcls.hCursor =NULL; //标准 为null ////////////光标 ///////////可赋值如=LoadCousor(NULL,IDC_CLOSS)
wndcls.hbrBackground =(HBRUSH)GetStockObject(GRAY_BRUSH);//背景 hurush强制转换
wndcls.cbClsExtra =0; ///////////额外附加字节数
wndcls.cbWndExtra =0; /////////////窗口额外附加字节数
/////////注册窗口类
RegisterClass(&wndcls);
////////创建窗口
HWND hwnd;
hwnd=CreateWindow /////定义句柄
("CustomWnd", //类名
"简单的Win32App", //窗口名
WS_CAPTION|WS_OVERLAPPEDWINDOW|WS_SYSMENU,////窗口类型 见附录
50,50,500,500, //坐标和大小
NULL, //副窗口
NULL, //菜单
wndcls.hInstance, /////
NULL); ////窗口创建的句柄

///////显示窗口
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd); ///更新窗口

////////进入消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg); //转换
DispatchMessage(&msg); //
}
return msg.wParam;
}////////////////////////////////////////////至此winmain写完

/////编写窗口函数
LRESULT CALLBACK WNDFUN //////可以复制声明部分贴上去
(
HWND hWnd, // handle to window
UINT msg, // message identifier
WPARAM wparam, // first message parameter
LPARAM lparam // second message parameter
)
{
switch(msg) ////写下要处理的消息种类
{
case WM_PAINT:

{
HDC hdc=GetDC(hWnd);
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,20,20,"简单的WinMain函数窗口制作",16);
DeleteDC(hdc);
break;
}
case WM_CLOSE:
{
DestroyWindow(hWnd);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hWnd,msg,wparam,lparam);
}
return 0;
}



/////////////////附录////////////////////////////////////////////////////
//// 窗口类风格////////////
////风 格 描 述
////CS_BYTEALIGNCLIENT 窗口的客户区域以"字符边界"对齐,当系统调整窗口的水平位置时,客户区域的左边坐标是8的整数倍
////CS_BYTEALIGNWINDOW 窗口以"字符边界"对齐,当系统调整窗口的水平位置时,客户区域的左边坐标是8的整数倍
////CS_CLASSDC 分配一个设备环境并被类中的所有窗体共享。它是可以适用于一个应用程序的若干线程创建的一个相同类的窗体。当多个线程试图同时使用相同的设备环境时,系统只允许一个线程成功地进行绘图操作
////CS_DBLCLKS 当用户双击窗口时,将向窗口函数发送鼠标双击消息
////CS_GLOBALCLASS 指定此窗体类是一个应用程序全局类。应用程序全局类是由一个在进程中对所有模块有效的exe或dll注册的窗体类
////CS_HREDRAW 如果窗口的位置或宽度发生改变,将重绘窗口
////CS_NOCLOSE 窗口中的"关闭"按钮不可见
////CS_OWNDC 为同一个窗口类中的每个窗口创建一个唯一的设备上下文
////CS_PARENTDC 设置子窗口中剪下的矩形区域到父窗口中,以使子窗口可以在父窗口上绘图。指定该风格可以提高应用程序的性能
////CS_SAVEBITS 把被窗口遮掩的屏幕图像作为位图保存起来。当该窗口被移动时,Windows操作系统使用被保存的位图来重建屏幕图像
////CS_VREDRAW 如果窗口的位置或高度改变,将重绘窗口

////////////Window Style窗口类型风格////
////////Style Meaning
//////WS_CHILD Specifies a child window. This should not be changed after the window is created.
//////WS_POPUP Specifies a pop-up window. This style should not be changed after the window is created.
//////WS_VISIBLE Specifies a window that is initially visible. This style can be turned on and off to change window visibility.
//////WS_DISABLED Specifies a window that is initially disabled. A disabled window cannot receive input from the user
//////WS_CLIPCHILDREN Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used on the parent window. Windows CE windows always have the WS_CLIPCHILDREN style.
//////WS_CLIPSIBLINGS Excludes the area occupied by sibling windows that are above a window.
//////WS_GROUP Specifies the first control of a group of controls. This style is used primarily when creating dialog boxes. The group consists of this first control and all controls de

fined after it, up to the next control for which the WS_GROUP style is specified. Because the first control in each group often has the WS_TABSTOP style, a user can move from group to group.
//////WS_TABSTOP Specifies a control that can receive the keyboard focus when the user presses the TAB key. This style is used primarily when creating controls in a dialog box. Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
//////WS_EX_NOACTIVATE Specifies that a window cannot be activated. If a child window has this style, tapping it does not cause its top-level parent to activate. Although a window that has this style will still receive stylus events, neither it nor its child windows can get the focus.
//////WS_EX_NODRAG Specifies a stationary window that cannot be dragged by its title bar.
//////WS_EX_NOANIMATION Prevents a window from showing animated exploding and imploding rectangles and from having a button on the taskbar.
//////WS_EX_TOPMOST



/////////////////我也是初学者 写好不容易啊 希望大家多多支持 共同提高mfc水平///联系方式 QQ:981921334注明:共同学习///////////

相关文档