码迷,mamicode.com
首页 > Windows程序 > 详细

10windows_font_text

时间:2016-06-10 08:32:57      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

  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. }





10windows_font_text

标签:

原文地址:http://www.cnblogs.com/nfking/p/5573168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!