Hwnd、Handle、HINSTANCE区别

本文介绍了Windows编程中常用的两种资源句柄:hwnd(窗口句柄)和HINSTANCE(应用程序实例句柄)。这两种句柄用于标识特定的系统资源,并在Windows API调用中广泛使用。

Handle  是用来标记windows资源

Hwnd  Handle的一个子类 就好比char是int的一个子类一样。这个是窗口句柄

HINSTANCE Handle的一个子类,用来标记可执行程序的实例App实例。

 

使用hwnd 和 HINSTANCE是方便用户阅读维护代码。

转载于:https://www.cnblogs.com/wolfrickwang/p/3238760.html

#include <windows.h> //窗口处理函数WndProc()的声明 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //主函数,程序的入口,相当于之前的main()函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szAppName[] = "HelloWin"; HWND hwnd; MSG msg; WNDCLASS wndclass;//WNDCLASSEX也行,会多几个分量,配合RegisterClassEX使用 //设置WNDCLASS结构体,为下一步调用RegisterClass()函数做准备 wndclass.style = CS_HREDRAW | CS_VREDRAW;//支持重绘,不能为0 wndclass.lpfnWndProc = (WNDPROC)WndProc;//主函数与窗口处理函数绑定 wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; //注册 if (!RegisterClass(&wndclass)) { MessageBox(NULL, "This program requires Windows NT!", szAppName, MB_ICONERROR); return 0; } //调用CreateWindow()函数创建窗体 hwnd = CreateWindow (szAppName, // window class name "The Windows Program", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters //显示和更新窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); //消息循环,不停接收消息直至收到WM_QUIT消息后,跳出循环,程序结束 while(GetMessage(&msg, NULL, 0, 0))//如果函数取得WM_QUIT之外的其他消息,返回非零值。如果函数取得WM_QUIT消息,返回值是零 { TranslateMessage(&msg);//翻译消息,将按键消息转换为字符消息 DispatchMessage(&msg);//以此消息为参数转发给相应的窗口消息处理程序 } return msg.wParam; } //窗口处理函数,函数格式固定,函数名无所谓 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //Code 使用以上的框架代码,编写程序,要求不断变换窗口屏幕中的颜色,以红橙黄绿青蓝紫的顺序依次变换,间隔0.5秒,不断循环
03-12
#include <windows.h> class CWnd { public: BOOL CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam); // window-creation data BOOL ShowWindow(int nCmdShow); BOOL UpdateWindow(); public: HWND m_hWnd; }; BOOL CWnd::CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam) // window-creation data { m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle, x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam); if(m_hWnd!=NULL) return TRUE; else return FALSE; } BOOL CWnd::ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd,nCmdShow); } BOOL CWnd::UpdateWindow() { return ::UpdateWindow(m_hWnd); } LRESULT CALLBACK MyWndProc( 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 MyWnd; MyWnd.cbClsExtra = NULL; MyWnd.cbWndExtra = NULL; MyWnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); MyWnd.hCursor = LoadCursor(NULL, IDC_ARROW); MyWnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); MyWnd.hInstance = hInstance; MyWnd.lpfnWndProc = MyWndProc; MyWnd.lpszClassName = "Hello"; MyWnd.lpszMenuName = NULL; MyWnd.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&MyWnd); CWnd wnd; wnd.CreateEx(0,"Hello","CWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); wnd.ShowWindow(SW_SHOWNORMAL); wnd.UpdateWindow(); MSG msg; while (GetMessage(&msg,NULL,0,0)) //?????????? { TranslateMessage(&msg); //???? //4.???? DispatchMessage(&msg); //??????????? } return 0; } LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 在窗口中心绘制"Hello, World!" RECT rect; GetClientRect(hwnd, &rect); const char* text = "Hello, World!"; DrawText( hdc,text, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); EndPaint(hwnd, &ps); break; } case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) // 左键拖拽时绘制 { // 记录轨迹点... InvalidateRect(hwnd, NULL, FALSE); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } 拓展各种功能,尽可能多
04-01
#include <windows.h> //窗口处理函数WndProc()的声明 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //主函数,程序的入口,相当于之前的main()函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szAppName[] = “HelloWin”; HWND hwnd; MSG msg; WNDCLASS wndclass;//WNDCLASSEX也行,会多几个分量,配合RegisterClassEX使用 //设置WNDCLASS结构体,为下一步调用RegisterClass()函数做准备 wndclass.style = CS_HREDRAW | CS_VREDRAW;//支持重绘,不能为0 wndclass.lpfnWndProc = (WNDPROC)WndProc;//主函数与窗口处理函数绑定 wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; //注册 if (!RegisterClass(&wndclass)) { MessageBox(NULL, "This program requires Windows NT!", szAppName, MB_ICONERROR); return 0; } //调用CreateWindow()函数创建窗体 hwnd = CreateWindow (szAppName, // window class name "The Windows Program", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters //显示和更新窗口 ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); //消息循环,不停接收消息直至收到WM_QUIT消息后,跳出循环,程序结束 while(GetMessage(&msg, NULL, 0, 0))//如果函数取得WM_QUIT之外的其他消息,返回非零值。如果函数取得WM_QUIT消息,返回值是零 { TranslateMessage(&msg);//翻译消息,将按键消息转换为字符消息 DispatchMessage(&msg);//以此消息为参数转发给相应的窗口消息处理程序 } return msg.wParam; } //窗口处理函数,函数格式固定,函数名无所谓 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //Code 使用以上的框架代码,编写程序,要求不断变换窗口屏幕中的颜色,以红橙黄绿青蓝紫的顺序依次变换,间隔0.5秒,不断循环
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值