Win32 SDK 学习笔记5

来源:互联网 发布:windows处于通知状态 编辑:程序博客网 时间:2024/04/29 08:04

要在指定位置显示文字,用的更多是TextOut()函数。它与DrawText()函数特点不同,作用也不同。

一、TextOut()函数的基本用法

TextOut()函数既不处理回车符、TAB键等,也不自动回行,以指定的位置作基准点,在其附近显示,默认时基准点是这一行字的左上角。要知道当前文字的对齐方式,可以用GetTextAlign()函数,本节先学习与之相对的SetTextAlign()函数。TextOut(hdc, 30, 20, str1, (int)strlen(str1) );

二、文字的对齐方式

SetTextAlign()函数用来设置文字的对齐方式。下面我们以窗口正中间作为基准点来了解这个函数基本内容。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {    HDC hdc;    PAINTSTRUCT ps;    TCHAR str1[] = "My first GDI is so easy and cool!";    RECT rc;//    int x;//    switch(message) {        case WM_PAINT:            hdc = BeginPaint(hWnd, &ps);            GetClientRect(hWnd, &rc);//            x = rc.right / 2;//            SetTextAlign(hdc, TA_LEFT);//左对齐            TextOut(hdc, x, 20, str1, (int)strlen(str1) );//            EndPaint(hWnd, &ps);            break;    //其中"//"表示左对齐新加的代码      

三、设置文字的字体、大小、方向

#include <windows.h>TCHAR szWindowClass[] = "My first window's name";TCHAR szTitle[] = "My first window";ATOM MyRegisterClass(HINSTANCE);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);HFONT SetMyFont(HDC, LPCTSTR, int, int, int);int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) {    MSG msg;    MyRegisterClass(hInstance);    if ( !InitInstance(hInstance, nShowCmd) ) {        return FALSE;    }    while ( GetMessage(&msg, NULL, 0, 0) ) {        TranslateMessage(&msg);        DispatchMessage(&msg);    }    return (int)msg.wParam;}ATOM MyRegisterClass(HINSTANCE hInstance) {    WNDCLASSEX wc;    wc.cbSize = sizeof(WNDCLASSEX);    wc.style = CS_VREDRAW | CS_HREDRAW;    wc.lpfnWndProc = (WNDPROC)WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);    wc.hCursor = LoadCursor(hInstance, IDC_SIZEALL);    wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = szWindowClass;    wc.hIconSm = LoadIcon(wc.hInstance, IDI_APPLICATION);    return RegisterClassEx(&wc);}BOOL InitInstance(HINSTANCE hInstance, int ncmdshow) {    HWND hWnd;    hWnd = CreateWindow(szWindowClass,        szTitle,        WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT,        CW_USEDEFAULT,        200,        200,        NULL,        NULL,        hInstance,        NULL);    if (!hWnd) {        return FALSE;    }    ShowWindow(hWnd, ncmdshow);    UpdateWindow(hWnd);    return TRUE;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {    HDC hdc;    PAINTSTRUCT ps;    HFONT hFont, hFontOld;    TCHAR str1[] = "My first GDI";    TCHAR str2[] = "星期三";    TCHAR str3[] = "Configuration";    int width, height;    switch(message) {        case WM_PAINT:            RECT rc;            hdc = BeginPaint(hWnd, &ps);            GetClientRect(hWnd, &rc);            SetBkMode(hdc, TRANSPARENT);            width = (rc.right - 20) / (int)strlen(str1);            height = rc.bottom - 20;            hFont = SetMyFont(hdc, (LPCTSTR)"黑体", width, height, 90);            hFontOld = (HFONT)SelectObject(hdc, hFont);            SetTextColor(hdc, 0xC0C0C0);            TextOut(hdc, 10, 10, str1, (int)strlen(str1));            DeleteObject(hFont);            hFont = SetMyFont(hdc, (LPCTSTR)"楷体", width, height, 450);            hFontOld = (HFONT)SelectObject(hdc, hFont);            SetTextColor(hdc, 0xF00000);            TextOut(hdc, 16, 0, str2, (int)strlen(str2));            DeleteObject(hFont);            hFont = SetMyFont(hdc, (LPCTSTR)"宋体", width, height, -90);            hFontOld = (HFONT)SelectObject(hdc, hFont);            SetTextColor(hdc, 0x0F0000);            TextOut(hdc, 0, 10, str3, (int)strlen(str3));            DeleteObject(hFont);            break;        case WM_DESTROY:            PostQuitMessage(0);            break;        default:            return DefWindowProc(hWnd, message, wParam, lParam);    }    return 0;}HFONT SetMyFont(HDC hdc, LPCTSTR face, int width, int height, int angle){    HFONT hFont;    hFont = CreateFont(        height,      //字体的逻辑高度        width,       //逻辑平均字符宽度        angle,       //与水平线的角度        0,           //基线方位角度        FW_REGULAR,  //字形:常规        FALSE,       //字形:斜体        FALSE,       //字形:下划线        FALSE,       //字形:粗体        GB2312_CHARSET,          //字符集        OUT_DEFAULT_PRECIS,      //输出精度        CLIP_DEFAULT_PRECIS,     //剪截精度        PROOF_QUALITY,           //输出品质        FIXED_PITCH | FF_MODERN, //倾斜度        face                     //字体        );     return hFont;}
0 0