博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10windows_font_text
阅读量:6176 次
发布时间:2019-06-21

本文共 2984 字,大约阅读时间需要 9 分钟。

 
  1. #include <windows.h>
  2. #include <iostream>
  3. CHAR szText[256] = {
    0 };
  4. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  5. HINSTANCE g_hInst = NULL; //窗口句柄
  6. HANDLE g_hStdout = NULL; //控制台句柄
  7. //OnPaint
  8. void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10. PAINTSTRUCT ps = {
    0 };
  11. HDC hDC = BeginPaint(hWnd, &ps);
  12. //TDD...
  13. //设置字体颜色
  14. COLORREF nOldTextColor = SetTextColor(hDC, RGB(255, 0, 0));
  15. //设置文字背景颜色
  16. COLORREF nOldBkColor = SetBkColor(hDC, RGB(0, 0, 255));
  17. //设置字体
  18. HFONT hFont = CreateFont(20, 20, 0/*倾斜度*/, 0, 0, FALSE, FALSE, DEFAULT_CHARSET,
  19. 0, 0, 0, 0, 0, "宋体");
  20. //选择字体到DC中
  21. HFONT hOldFon = (HFONT)SelectObject(hDC, hFont);
  22. sprintf_s(szText, 256, "Hello Text");
  23. //TextOut
  24. TextOut(hDC, 100, 100, szText, strlen(szText));
  25. //设置文字背景绘制模式,为透明模式
  26. SetBkMode(hDC, TRANSPARENT);
  27. //DrawText
  28. RECT rcText = {
    0 };
  29. INT nText[10] = {
    30, 30, 30, 10, 30, 10, 30, 10,20, 2}; //间距,汉字的间距为0
  30. rcText.left = 100;
  31. rcText.top = 120;
  32. rcText.right = 200;
  33. rcText.bottom = 200;
  34. Rectangle(hDC, rcText.left, rcText.top, rcText.right, rcText.bottom);
  35. DrawText(hDC, szText, strlen(szText), &rcText, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  36. ExtTextOut(hDC, 100, 300, ETO_OPAQUE, NULL, szText, strlen(szText), nText);
  37. SetTextColor(hDC, nOldTextColor); //恢复字体颜色
  38. SetBkColor(hDC, nOldBkColor); //恢复背景颜色
  39. //恢复字体
  40. SelectObject(hDC, hOldFon); //取出字体
  41. DeleteObject(hFont); //删除字体
  42. EndPaint(hWnd, &ps);
  43. }
  44. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  45. {
  46. switch (nMsg)
  47. {
  48. case WM_PAINT:
  49. OnPaint(hWnd, nMsg, wParam, lParam);
  50. break;
  51. case WM_DESTROY:
  52. PostQuitMessage(0);
  53. break;
  54. }
  55. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  56. }
  57. BOOL RegisterWnd(LPSTR pszClassName)
  58. {
  59. WNDCLASSEX wce = {
    0 };
  60. wce.cbSize = sizeof(wce);
  61. wce.cbClsExtra = 0;
  62. wce.cbWndExtra = 0;
  63. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  64. wce.hCursor = NULL;
  65. wce.hIcon = NULL;
  66. wce.hIconSm = NULL;
  67. wce.hInstance = g_hInst;
  68. wce.lpfnWndProc = WndProc;
  69. wce.lpszClassName = pszClassName;
  70. wce.lpszMenuName = NULL;
  71. wce.style = CS_HREDRAW | CS_VREDRAW;
  72. ATOM atom = RegisterClassEx(&wce);
  73. if (atom == NULL)
  74. {
  75. return FALSE;
  76. }
  77. else
  78. {
  79. return TRUE;
  80. }
  81. }
  82. HWND CreateWnd(LPSTR pszClassName)
  83. {
  84. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  85. CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
  86. return hWnd;
  87. }
  88. void ShowWnd(HWND hWnd)
  89. {
  90. ShowWindow(hWnd, SW_SHOW);
  91. UpdateWindow(hWnd);
  92. }
  93. void Msg()
  94. {
  95. MSG msg = {
    0 };
  96. while (GetMessage(&msg, NULL, 0, 0))
  97. {
  98. TranslateMessage(&msg);
  99. DispatchMessage(&msg);
  100. }
  101. }
  102. void ConsoleWnd()
  103. {
  104. AllocConsole();
  105. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  106. CHAR szText[] = "Debug start:\n";
  107. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  108. }
  109. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  110. {
  111. g_hInst = hInstance;
  112. //ConsoleWnd();
  113. RegisterWnd("oooo");
  114. HWND hWnd = CreateWnd("oooo");
  115. ShowWnd(hWnd);
  116. Msg();
  117. return 0;
  118. }

转载于:https://www.cnblogs.com/nfking/p/5573168.html

你可能感兴趣的文章
docker安装gitlab只需要3分钟
查看>>
Android菜鸟学习js笔记 一
查看>>
Java基础之SPI机制
查看>>
使用js控制滚动条的位置
查看>>
【Tornado源码阅读笔记】tornado.web.Application
查看>>
lsyncd搭建测试
查看>>
移动web开发之像素和DPR
查看>>
nginx+tomcat+redis实现session共享
查看>>
UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)
查看>>
rsync 介绍
查看>>
做一个合格的Team Leader -- 基本概念
查看>>
leetcode 190 Reverse Bits
查看>>
阿里巴巴发布AliOS品牌 重投汽车及IoT领域
查看>>
OPENCV图像处理(二):模糊
查看>>
glassfish4系统启动脚本
查看>>
VMware 虚拟化编程(13) — VMware 虚拟机的备份方案设计
查看>>
独家 | 一文读懂推荐系统知识体系-下(评估、实战、学习资料)
查看>>
UIEvent&amp;nbsp;UIResponder&amp;nbsp;UI_04
查看>>
从非GP到GP
查看>>
云计算助力CDN加速
查看>>