您的位置:首页 > 其它

MFC学习之窗口基础

2014-08-04 13:48 330 查看
WinMain函数

1、句柄(HANDLE):{

1. 定义:资源的标识

2. 句柄的作用: 操作系统通过句柄来找到对应的资源,从而对这些资源进行管理和操作。

3句柄的分类:(按资源){

1.图标句柄(HICON) ,

2.光标句柄(HCURSOR) ,

3. 窗口句柄(HWND) ,

4.应用程序实列句柄(HINSTANCE).





2、Windows应用程序,操作系统,计算机硬件之间的相互关系

Windows程序的入口函数:



窗口应用程序入口:

Int WINAPI WinMain(

HINSTANCE hinstance ; // 应用程序实列句柄

HINSTANCE hPrevInstance ; // 基本都设置为0

LPSTR ipCmdLine ; /commandLLine LPSTRLP(long point 长指针)

int nCmdSbow ; //显示状态

)

3、 窗口的创建:

大致来说,如果要创建一个完整的窗口需要经过下面四个操作步骤:{

(1)、设计一个窗口类;

(2)、 注册窗口类 ;

(3)、创建窗口;

(4)、显示及更新窗口。

}

#include<stdio.h>
#include<string.h>
#include<windows.h>

/*声明winSunProc*/
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 wndclass;
/*声明定义什么的*/
wndclass.cbClsExtra = NULL;
wndclass.cbWndExtra = NULL;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WinSunProc ;
wndclass.lpszClassName = "Gxjun";
wndclass.lpszMenuName =NULL;
wndclass.style= CS_HREDRAW|CS_VREDRAW ;
RegisterClass(&wndclass);    //注册窗口函数
HWND hwnd;
hwnd =  CreateWindow("Gxjun","龚细军的第一个窗口程序",WS_OVERLAPPEDWINDOW,0,0,400,600,NULL,NULL,hInstance,NULL);  /*创建窗口*/
ShowWindow(hwnd,SW_SHOWNORMAL);    /*显示窗口*/
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);       /*该函数将虚拟键消息转换为字符消息*/

DispatchMessage(&msg);        /*该函数分发一个消息给窗口程序。通常消息从GetMessage函数获得。
消息被分发到回调函数(过程函数),作用是消息传递给操作系统,
然后操作系统去调用我们的回调函数,也就是说我们在窗体的过程函
数中处理消息*/
}

return 0;
}
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 str[26];
sprintf(str,"char is %d",wParam);
MessageBox(hwnd,str,"Gxjun",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","Gxjun",0);
HDC hdc;
/*PAINTSTRUCT  ps;*/
hdc=GetDC(hwnd);
TextOut(hdc,200,400,"我是胡萝卜头,呼叫北极站",strlen("我是胡萝卜头,呼叫北极站"));
ReleaseDC(hwnd,hdc);    /*函数释放设备上下文环境(DC)供其他应用程序使用。*/
break;
case WM_PAINT:
HDC hdc_1;
PAINTSTRUCT paints;
hdc_1 = BeginPaint(hwnd,&paints);
TextOut(hdc_1,0,0,"我是长城好哇",strlen("我是长城好哇"));
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"哇哈哈,你丫的真的打算关掉吗?","卖萌之家",MB_YESNO))  {
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam, lParam);
}
return 0;
}


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: