Chp1之使用图标资源

第一章小例子中使用的图标资源

一般格式:

方法一:使用字符串名

icon_name ICON FILENAME.ico

例:

windowicon  ICON star.ico

MyCoolIcon ICON cool.ico

 

方法二:使用整型ID

icon_id ICON FILENAME.ico

例:

windowicon ICON star.ico

124 ICON ship.ico

假定你想在.RC文件RESOURCES.RC中定义三个符号图标,同时也需要一个.h文件RESOURCES.h

RESOURCES.h的内容:

#define ID_ICON1 100

#define ID_ICON2 101

#defie ID_ICON3 103

RESOURCES.RC中内容:

#include“rsources.h”

ID_ICON1 ICON star.ico

ID_ICON2 ICON  ball.ico

ID_ICON3 ICON  cross.ico

代码:

Skeleton.cpp
//-----------------------------------------------------------------
// Skeleton Application
// C++ Source - Skeleton.cpp
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Skeleton.h"

//-----------------------------------------------------------------
// Global Function Declarations
//-----------------------------------------------------------------
LRESULT CALLBACK  WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

//-----------------------------------------------------------------
// Global Functions
//-----------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  PSTR szCmdLine, int iCmdShow)
{
  static TCHAR  szAppName[] = TEXT("Skeleton");
  WNDCLASSEX    wndclass;
  HWND          hWindow;
  MSG           msg;

  // Create the window class for the main window
  wndclass.cbSize         = sizeof(wndclass);
  wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc    = WndProc;
  wndclass.cbClsExtra     = 0;
  wndclass.cbWndExtra     = 0;
  wndclass.hInstance      = hInstance;
  wndclass.hIcon          = LoadIcon(hInstance,
    MAKEINTRESOURCE(IDI_SKELETON));
  wndclass.hIconSm        = LoadIcon(hInstance,
    MAKEINTRESOURCE(IDI_SKELETON_SM));
  wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  wndclass.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  wndclass.lpszMenuName   = NULL;
  wndclass.lpszClassName  = szAppName;

  // Register the window class
  if (!RegisterClassEx(&wndclass))
    return 0;

  // Create the window
  hWindow = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

  // Show and update the window
  ShowWindow(hWindow, iCmdShow);
  UpdateWindow(hWindow);

  // Enter the main message loop
  while (GetMessage(&msg, NULL, 0, 0))
  {
    // Process the message
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
  HDC         hDC;
  PAINTSTRUCT ps;
  RECT        rect;

  switch (msg) 
  {
    case WM_PAINT:
      // Draw some text centered in the client area of the main window
      hDC = BeginPaint(hWindow, &ps);
      GetClientRect(hWindow, &rect);
      DrawText(hDC, TEXT("This is a skeleton application!"), -1, &rect,
        DT_SINGLELINE | DT_CENTER | DT_VCENTER);
      EndPaint(hWindow, &ps);
      return 0;

    case WM_DESTROY:
      // Exit the application
      PostQuitMessage(0);
      return 0;
  }
  return DefWindowProc(hWindow, msg, wParam, lParam);
}

Resource.h

//-----------------------------------------------------------------
// Skeleton Resource Identifiers
// C++ Header - Resource.h
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Icons                    Range : 1000 - 1999
//-----------------------------------------------------------------
#define IDI_SKELETON        1000
#define IDI_SKELETON_SM     1001



Skeleton.h
//-----------------------------------------------------------------
// Skeleton Application
// C++ Header - Skeleton.h
//-----------------------------------------------------------------

#pragma once

//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include <windows.h>
#include "Resource.h"


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值