//------------------------------------------------------------------------------
// Copyright (c) 2009 eryar All rights reserved.
//
// File : Main.cpp
// Author : eryar@163.com
// Date : 2009-9-18 23:04
// Version : 1.0v
//
// Description :
//
//==============================================================================
#include <windows.h>
#define IDM_FILE_OPEN 10000
#define IDM_FILE_EXIT 10001
CHAR* g_szAppName = "Read Bitmap";
CHAR* g_szClassName = "CReadBMP";
HINSTANCE g_hInst;
void TrackMenu(HWND hWnd, POINT point);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
MSG Msg;
HWND hWnd;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = NULL; //(HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = g_szClassName;
if (!RegisterClassEx(&wndclass)) {
MessageBox(NULL, "register class failed! ", g_szAppName, MB_OK|MB_ICONERROR);
return false;
}
hWnd = CreateWindowEx(
NULL,
g_szClassName,
g_szAppName,
WS_OVERLAPPEDWINDOW,
0,0,
300, 300,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd) {
MessageBox(NULL, " create window failed! ", g_szAppName, MB_OK|MB_ICONERROR);
return false;
}
g_hInst = hInstance;
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
while(GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
static RECT rect;
static HDC hDC;
static HDC hDCBackBuffer;
static HBITMAP hBitmap;
static HBITMAP hOldBitmap;
switch(Msg) {
case WM_CREATE: {
GetClientRect(hWnd, &rect);
}
return 0;
case WM_RBUTTONUP: {
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
ClientToScreen(hWnd, &point);
TrackMenu(hWnd, point);
}
return 0;
case WM_PAINT:
GetClientRect(hWnd, &rect);
hDC = BeginPaint(hWnd, &ps);
hBitmap = (HBITMAP)LoadImage(g_hInst, "Demo.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
hDCBackBuffer = CreateCompatibleDC(NULL);
hOldBitmap = (HBITMAP)SelectObject(hDCBackBuffer, hBitmap);
BitBlt(hDC, 0, 0, rect.right, rect.bottom, hDCBackBuffer, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
// keep it clear
SelectObject(hDCBackBuffer, hOldBitmap);
DeleteDC(hDCBackBuffer);
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
}
void TrackMenu(HWND hWnd, POINT point)
{
HMENU hMenu;
HMENU hMenuPopup;
hMenu = CreatePopupMenu();
hMenuPopup = CreatePopupMenu();
AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_OPEN, "&Open");
AppendMenu(hMenu, MF_POPUP,(UINT)hMenuPopup, "&File");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, IDM_FILE_EXIT, "&Exit");
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL);
}