博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
8Windows_paint2_bimap 位图
阅读量:6402 次
发布时间:2019-06-23

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

 
  1. #include <windows.h>
  2. #include <iostream>
  3. #include "resource1.h"
  4. //宏定义输出函数
  5. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL);
  6. HINSTANCE g_hInst;
  7. HANDLE g_hStdout = NULL;
  8. CHAR szText[256] = {
    0 };
  9. //DrawBmp
  10. void DrawBmp(HDC hDC)
  11. {
  12. //加载位图
  13. HBITMAP hBMap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BALL01));
  14. //获取位图相关信息。。。新增加,对应笔记里的第8条
  15. BITMAP bmpInfo = {
    0 };
  16. GetObject(hBMap, sizeof(bmpInfo), &bmpInfo); //得到一个BITMAP结构体的数据,就可以直接使用得到bmpInfo.bmWidth
  17. //创建放置位图的DC
  18. HDC hBmpDC = CreateCompatibleDC(hDC);
  19. //将位图放入创建的hBmpDC中
  20. HBITMAP hOldBmp = (HBITMAP)SelectObject(hBmpDC, hBMap);
  21. //绘制位图 BitBlt
  22. BitBlt(hDC, 100, 100, 48, 48, hBmpDC, 0, 0, SRCCOPY);
  23. //拉伸绘制位图 StretchBlt
  24. StretchBlt(hDC, 200, 100, 200, 200, hBmpDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY); //拉伸方式绘制
  25. //取出位图
  26. SelectObject(hBmpDC, hOldBmp);
  27. //删除DC
  28. DeleteDC(hBmpDC);
  29. //删除位图
  30. DeleteObject(hBMap);
  31. }
  32. //OnPaint
  33. void OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  34. {
  35. PAINTSTRUCT ps = {
    0 };
  36. HDC hDC = BeginPaint(hWnd, &ps);
  37. DrawBmp(hDC);
  38. EndPaint(hWnd, &ps);
  39. }
  40. //OnCommand
  41. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. int hMenuID = LOWORD(wParam);
  44. switch (hMenuID)
  45. {
  46. case ID_EXIT:
  47. PostQuitMessage(0);
  48. break;
  49. }
  50. }
  51. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  52. {
  53. switch (nMsg)
  54. {
  55. case WM_PAINT:
  56. OnPaint(hWnd, nMsg, wParam, lParam);
  57. break;
  58. case WM_COMMAND:
  59. OnCommand(hWnd, nMsg, wParam, lParam);
  60. break;
  61. case WM_DESTROY:
  62. PostQuitMessage(0);
  63. break;
  64. }
  65. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  66. }
  67. BOOL RegisterWnd(LPSTR pszClassName)
  68. {
  69. WNDCLASSEX wce = {
    0 };
  70. wce.cbSize = sizeof(wce);
  71. wce.cbClsExtra = 0;
  72. wce.cbWndExtra = 0;
  73. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  74. wce.hCursor = NULL;
  75. wce.hIcon = NULL;
  76. wce.hIconSm = NULL;
  77. wce.hInstance = g_hInst;
  78. wce.lpfnWndProc = WndProc;
  79. wce.lpszClassName = pszClassName;
  80. wce.lpszMenuName = NULL;
  81. wce.style = CS_VREDRAW | CS_HREDRAW;
  82. ATOM aTom = RegisterClassEx(&wce);
  83. if (aTom == 0)
  84. {
  85. return FALSE;
  86. }
  87. else
  88. {
  89. return TRUE;
  90. }
  91. }
  92. HWND CreateWnd(LPSTR pszClassName)
  93. {
  94. //加入菜单
  95. HMENU hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MAIN));
  96. HWND hWnd = CreateWindowEx(0, pszClassName, "风火中原", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  97. NULL, hMainMenu, g_hInst, NULL);
  98. return hWnd;
  99. }
  100. void showWnd(HWND hWnd)
  101. {
  102. ShowWindow(hWnd, SW_SHOW);
  103. UpdateWindow(hWnd);
  104. }
  105. void msg()
  106. {
  107. MSG msg = {
    0 };
  108. while (GetMessage(&msg, NULL, 0, 0))
  109. {
  110. TranslateMessage(&msg);
  111. DispatchMessage(&msg);
  112. }
  113. }
  114. //控制台
  115. void ConsoleWnd()
  116. {
  117. AllocConsole();
  118. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  119. CHAR szText[] = "Debug start:\n";
  120. PrintLog(szText);
  121. }
  122. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  123. {
  124. g_hInst = hInstance;
  125. ConsoleWnd();
  126. RegisterWnd("OOOO");
  127. HWND hWnd = CreateWnd("OOOO");
  128. showWnd(hWnd);
  129. msg();
  130. return 0;
  131. }

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

你可能感兴趣的文章
Centos5上安装JRE和LUMAQQ
查看>>
Ping不通不代表主机不在线!
查看>>
SCCM2007安装部署指南
查看>>
关于监控工具的主动发起性能测试
查看>>
AngularJs $resource 高大上的数据交互
查看>>
Cisco基础实验回顾4--IP classless
查看>>
上接扩展GridView控件(6) - 响应行的单击事件和双击事件
查看>>
负载均衡实施 应该因地制宜
查看>>
非常好的BASH脚本编写教程
查看>>
MFC类库之CArray作为函数参数和返回值
查看>>
VMware vSphere 5.1 群集深入解析(十八)-DPM推荐向导&汇总
查看>>
plesk panel 虚拟主机管理平台 0day
查看>>
Java正则表达式进阶(一):写出常用的正则模式
查看>>
Android:Typeface、fonts、字体
查看>>
PgSQL · 源码分析 · AutoVacuum机制之autovacuum launcher
查看>>
MySQL初步使用
查看>>
【计算机网络】 DNS学习笔记 (>﹏<)
查看>>
ORA-01111: name for data file 119 is unknown - rename to correct file
查看>>
源代码构建Apache反向代理(包括SSL配置)
查看>>
找出apache日志中访问量最大的IP
查看>>