C语言实现SDK窗口编程
//--------------------------------------------------------------------------- #include//=========================================================================== long __stdcall WndProc(HWND hwnd,UINT msg,WPARAM WParam,LPARAM LParam) { switch(msg) { case WM_CLOSE: PostQuitMessage(0); break; case WM_LBUTTONDOWN: MessageBoxA(0,"ok","提示",0); PostQuitMessage(0); break; case WM_PAINT: { HDC hdc; hdc = GetDC(hwnd); RECT rect; GetClientRect(hwnd,&rect); rect.left = rect.right/2; rect.top = rect.bottom/2; rect.right = rect.left+50; rect.bottom = rect.top+30; DrawText(hdc,"hello",5,&rect,0); ReleaseDC(NULL,hdc); } default: return DefWindowProc(hwnd,msg,WParam,LParam); } return 0; } WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreHinstance, LPSTR, int) { g_hIns = hInstance; WNDCLASS theClass ={0}; char * lpszClassName = "myClass"; char * lpszTitle = "测试窗口"; HWND hwnd; MSG msg; theClass.style = 0;//缺省窗口风格 theClass.lpfnWndProc = WndProc; theClass.cbClsExtra = 0;//窗口类无扩展 theClass.cbWndExtra = 0;//窗口实例无扩展 theClass.hInstance = hInstance; //窗口的最小化图标为缺省图标,即窗口左上角图标 //VC中资源ID为IDI_APPLICATION //在BCB中资源名为MAINICON theClass.hIcon=LoadIcon(hInstance,"MAINICON"); theClass.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标 theClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH)); //窗口背景为白色 theClass.lpszMenuName=NULL; //窗口无菜单 theClass.lpszClassName= lpszClassName; //窗口类名为"窗口" if(!RegisterClass(&theClass)) //如果注册失败 发出警告 { MessageBeep(0); return FALSE; } hwnd=CreateWindow( lpszClassName, //窗口类名 lpszTitle, //窗口标题名 WS_OVERLAPPEDWINDOW, //窗口的风格 CW_USEDEFAULT, //窗口左上角坐标值为缺省值 CW_USEDEFAULT, 500, 300, //窗口的宽和高 NULL, //此窗口无父窗口 NULL, //此窗口无子菜单 hInstance, //创建此窗口的应用程序的当前句柄 NULL //不使用该值 ); ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } // if (GetMessage (&msg, NULL, 0, 0)) // { // TranslateMessage (&msg) ; // DispatchMessage (&msg) ; // } // else // break; } return msg.wParam; //消息循环结束 即程序结束时 将信息返回系统 }
PeekMessage与GetMessage函数处理不相同,两种方式都可以.PeekMessage是不阻塞的,GetMessage是线程阻塞的,内部处理退出消息,所以不用判断退出消息,关闭时,就跳到break结束循环。要在WM_PAINT消息中绘图,可用如下代码:
成都创新互联是一家专注于成都网站建设、成都做网站与策划设计,上高网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:上高等地区。上高做网站价格咨询:028-86922220
PAINTSTRUCT ps ={0}; HDC hdc = BeginPaint(hwnd,&ps); //HPEN hPen = GetStockObject(WHITE_PEN); HPEN hPen = CreatePen(PS_DASHDOTDOT,1,RGB(255 , 0 , 0 )); HGDIOBJ oldPen = (HGDIOBJ)SelectObject(hdc,hPen); if(!oldPen) { int err = GetLastError(); char buff[20]={0}; itoa(err,buff,10); OutputDebugStringA(buff); } //画矩形 Rectangle(hdc,20,30,80,120); //画线 MoveToEx ( hdc , 20 , 10 , NULL ); LineTo( hdc , 200 ,100); SelectObject(hdc, oldPen); DeleteObject(hPen); EndPaint(hwnd,&ps);
本文名称:C语言实现SDK窗口编程
文章起源:http://hbruida.cn/article/ijsdsh.html