#include "stdafx.h" HINSTANCE g_hInst = NULL; void onPaint(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){ PAINTSTRUCT ps={0}; HDC hDC=BeginPaint(hWnd,&ps); //设置文字颜色 COLORREF nOldTextColor=SetTextColor(hDC,RGB(255,0,0)); //设置文字背景颜色 系统默认的是白色背景色 COLORREF nOldBGColor=SetBkColor(hDC,RGB(87,138,87)); //设置字体 旋转字体都可以设置 字体加粗 下划线 HFONT hFont=CreateFont(30,30,50,100,0,false,true,false,DEFAULT_CHARSET,0,0,0,0,"黑体"); HFONT hOldFont=(HFONT)SelectObject(hDC,hFont); char szText[260]="hello,Text"; TextOut(hDC,100,100,szText,strlen(szText)); RECT rcText={0}; rcText.left =100; rcText.top=120; rcText.right=200; rcText.bottom=200; //设置一个矩形 Rectangle(hDC,rcText.left,rcText.top,rcText.right,rcText.bottom); //把文字装载到矩形里面 后面DT_CENTER的参数和TABLE对齐一样 DrawText(hDC,szText,strlen(szText),&rcText,DT_CENTER|DT_VCENTER); //调整字符的间距,如果是中文字符的话 前间距为0 //设置文字的背景颜色为透明 SetBkMode(hDC,TRANSPARENT); char szText2[15]="luoqiang罗强"; INT nText[10] = {100,100,100,100,100,100,100,100,100,100}; ExtTextOut(hDC,300,300,ETO_OPAQUE,NULL,szText2,strlen(szText2),nText); SelectObject(hDC,hOldFont); DeleteObject(hFont); EndPaint(hWnd,&ps); } //主窗口的窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: onPaint(hWnd, nMsg, wParam, lParam); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } //窗口注册函数 BOOL RegisterWnd( LPSTR pszClassName,int nBrush ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.style = CS_HREDRAW|CS_VREDRAW; wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.lpfnWndProc= WndProc; wce.hInstance = g_hInst; wce.hCursor = NULL; wce.hIcon = NULL; wce.hbrBackground = HBRUSH(nBrush); wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.hIconSm = NULL; ATOM nAtom = RegisterClassEx( &wce ); if( nAtom == 0 ) { return FALSE; } return TRUE; } //创建主窗口 HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MainWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } //消息循环 void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } //显示窗口 void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. g_hInst = hInstance; RegisterWnd("luoqiang",COLOR_BACKGROUND); DisplayWnd(CreateWnd("luoqiang")); Message(); return 0; }