📄 hyperlinkdemo.cpp
字号:
// HyperlinkDemo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Hyperlink.h"
#include "resource.h"
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
void CreateHyperlinkControls( HWND hWnd );
void OnHyperlink1( void* pVoid );
void OnHyperlink2( void* pVoid );
void OnHyperlink3( void* pVoid );
void OnPaint( HWND hWnd );
CHyperlink m_Hyperlink1; // Declaration of CHyperlink objects
CHyperlink m_Hyperlink2;
CHyperlink m_Hyperlink3;
HBITMAP hBitmapBackground;
HDC hMemoryDC;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HBRUSH hBrushBknd; // Handle to brush for background of window
char szClassName[] = "HyperlinkDemo";
HWND hWnd;
WNDCLASSEX wndClass = {0};
MSG Msg;
HDC winDC;
hBrushBknd = (HBRUSH)GetStockObject( NULL_BRUSH ); // Create background brush
// Fill out window structure
wndClass.cbSize = sizeof( WNDCLASSEX );
wndClass.hbrBackground = hBrushBknd;
wndClass.hIcon = LoadIcon( NULL, IDI_WINLOGO );
wndClass.hIconSm = LoadIcon( NULL, IDI_WINLOGO );
wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WndProc;
wndClass.lpszClassName = szClassName;
wndClass.cbClsExtra = sizeof( long );
wndClass.cbWndExtra = NULL;
wndClass.lpszMenuName = NULL;
wndClass.style = NULL;
if( !RegisterClassEx( &wndClass ) ) // Register the window class
{
MessageBox( 0, "Failed to register class!", "Fatal error", MB_OK | MB_ICONSTOP );
return 0;
}
hWnd = CreateWindow( szClassName, " CHyperlink Demo App",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 282, 420, NULL, NULL, GetModuleHandle( NULL ), NULL );
if( hWnd == NULL )
{
MessageBox( 0, "Failed to create window!", "Fatal error", MB_OK | MB_ICONSTOP );
return 0;
}
SetWindowLong( hWnd, GWL_STYLE, GetWindowLong( hWnd, GWL_STYLE ) & ~WS_MAXIMIZEBOX );
ShowWindow( hWnd, SW_SHOW );
hBitmapBackground = (HBITMAP)LoadBitmap( hInstance, (LPCSTR)IDB_PARENTBG );
if( hBitmapBackground == NULL )
{
MessageBox( 0, "Failed to load bitmap file.", "Fatal error", MB_OK | MB_ICONSTOP );
return 0;
}
winDC = GetDC( hWnd );
hMemoryDC = CreateCompatibleDC( winDC );
ReleaseDC( hWnd, winDC );
if( hMemoryDC == NULL )
{
MessageBox( 0, "Failed to create memory device context.", "Fatal error", MB_OK | MB_ICONSTOP );
return 0;
}
SelectObject( hMemoryDC, hBitmapBackground );
// Enter message loop
while( GetMessage( &Msg, NULL, 0, 0 ) )
{
TranslateMessage( &Msg );
DispatchMessage ( &Msg );
}
DeleteObject( hBrushBknd );
DeleteObject( hBitmapBackground );
DeleteDC( hMemoryDC );
return 0;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CREATE:
CreateHyperlinkControls( hWnd );
return true;
case WM_CLOSE:
m_Hyperlink1.Destroy();
m_Hyperlink2.Destroy();
m_Hyperlink3.Destroy();
DestroyWindow( hWnd );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_PAINT:
OnPaint( hWnd );
return true;
case WM_GETMINMAXINFO:
PMINMAXINFO mmi = (PMINMAXINFO)lParam;
POINT pt;
pt.x = 282;
pt.y = 420;
mmi->ptMinTrackSize = pt;
mmi->ptMaxTrackSize = pt;
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam);
}
void CreateHyperlinkControls( HWND hWnd )
{
HYPERLINKSTRUCT hls = {0};
hls.bBold = false; // Bold font?
hls.bUnderline = true; // Underlined font?
hls.bUseBg = false; // Use opaque background?
hls.bUseCursor = true; // Use custom cursor?
hls.hCursor = LoadCursor( GetModuleHandle( NULL ), (LPCSTR)IDC_HAND );
hls.clrBack = RGB(0,0,0); // Fill this out if bUseBg is true
hls.clrHilite = RGB( 255, 0, 0 ); // Color of mouseover font
hls.clrText = RGB( 0, 0, 255 ); // Color of regular font
hls.coordX = 10; // X-axis coordinate of upper left corner
hls.coordY = 10; // Y-axis coordinate of upper left corner
hls.ctlID = 0; // Set this to use GetDlgItem on this control
hls.fontsize = 12; // Size of font used
hls.height = 20; // Height of control
hls.hWndParent = hWnd; // Handle to parent window
hls.pFn = OnHyperlink1; // Function pointer to function that is called when link is clicked
hls.szCaption = "Hyperlink #1"; // Caption
hls.szFontName = "Arial"; // Font face name
hls.width = 100; // Width of control
if( !m_Hyperlink1.Create( &hls ) )
MessageBox( 0, "Failed to create hyperlink 1.", "Fatal error!", MB_OK | MB_ICONSTOP );
hls.bBold = 1;
hls.coordX = 10;
hls.coordY = 35;
hls.pFn = OnHyperlink2;
hls.szFontName = "Lucida Console";
hls.szCaption = "Hyperlink #2";
hls.clrHilite = RGB(255,255,255);
if( !m_Hyperlink2.Create( &hls ) )
MessageBox( 0, "Failed to create hyperlink 2.", "Fatal error!", MB_OK | MB_ICONSTOP );
hls.bUseBg = true;
hls.bUnderline = false;
hls.clrBack = GetSysColor( COLOR_3DFACE );
hls.pFn = OnHyperlink3;
hls.szCaption = "Hyperlink #3";
hls.coordY = 60;
hls.clrHilite = RGB(255,0,0);
hls.height = 15;
hls.szFontName = "MS Sans Serif";
if( !m_Hyperlink3.Create( &hls ) )
MessageBox( 0, "Failed to create hyperlink 3.", "Fatal error!", MB_OK | MB_ICONSTOP );
}
void OnPaint( HWND hWnd )
{
PAINTSTRUCT ps = {0};
HDC dc;
RECT myRect = {0};
dc = BeginPaint( hWnd, &ps );
GetClientRect( hWnd, &myRect );
BitBlt( dc, 0, 0, (myRect.right - myRect.left), (myRect.bottom - myRect.top), hMemoryDC, 0, 0, SRCCOPY );
EndPaint( hWnd, &ps );
}
void OnHyperlink1( void* pVoid )
{
MessageBox( 0, "Click event from hyperlink #1.", "Click", MB_OK );
}
void OnHyperlink2( void* pVoid )
{
MessageBox( 0, "Click event from hyperlink #2.", "Click", MB_OK );
}
void OnHyperlink3( void* pVoid )
{
MessageBox( 0, "Click event from hyperlink #3.", "Click", MB_OK );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -