Win32-點陣圖-座標系-對映-文字-字型-有模式對話方塊
阿新 • • 發佈:2021-12-02
相關程式碼
// 建立點陣圖 // WinBmp.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" HINSTANCE g_hInst = NULL; void DrawBmp( HDC hDC ) { //載入點陣圖資源 HBITMAP hBmp = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_BALL01) ); //建立放置點陣圖的DC HDC hBmpDC = CreateCompatibleDC( hDC ); //將點陣圖放入hBmpDC HBITMAP hOldBmp = (HBITMAP) SelectObject( hBmpDC, hBmp ); //繪製點陣圖 BitBlt( hDC, 100, 100, 48, 48, hBmpDC, 0, 0, SRCCOPY ); //取出點陣圖 SelectObject( hBmpDC, hOldBmp ); //刪除DC DeleteDC( hBmpDC ); //刪除點陣圖 DeleteObject( hBmp ); } void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); DrawBmp( hDC ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HMENU hMenu = LoadMenu( g_hInst, MAKEINTRESOURCE(IDR_MAIN) ); HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 拉伸繪製點陣圖 // WinBmp.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" HINSTANCE g_hInst = NULL; void DrawBmp( HDC hDC ) { //載入點陣圖資源 HBITMAP hBmp = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_BALL01) ); //獲取點陣圖的相關資訊 BITMAP bmpInfo = { 0 }; GetObject( hBmp, sizeof(bmpInfo), &bmpInfo ); //建立放置點陣圖的DC HDC hBmpDC = CreateCompatibleDC( hDC ); //將點陣圖放入hBmpDC HBITMAP hOldBmp = (HBITMAP) SelectObject( hBmpDC, hBmp ); //繪製點陣圖 BitBlt( hDC, 100, 100, bmpInfo.bmWidth, bmpInfo.bmHeight, hBmpDC, 0, 0, SRCCOPY ); //拉伸繪製 StretchBlt( hDC, 200, 100, 300, 300, hBmpDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY ); //取出點陣圖 SelectObject( hBmpDC, hOldBmp ); //刪除DC DeleteDC( hBmpDC ); //刪除點陣圖 DeleteObject( hBmp ); } void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); DrawBmp( hDC ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HMENU hMenu = LoadMenu( g_hInst, MAKEINTRESOURCE(IDR_MAIN) ); HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 對映 // WinMap.cpp : Defines the entry point for the application. // #include "stdafx.h" HINSTANCE g_hInst = NULL; void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); Rectangle( hDC, 100, 100, 200, 200 ); //設定新對映模式 int nOldMap = SetMapMode( hDC, MM_LOMETRIC ); //繪圖 Rectangle( hDC, 100, -100, 200, -200 ); SetMapMode( hDC, MM_HIMETRIC ); Rectangle( hDC, 100, -100, 200, -200 ); //SetMapMode( hDC, MM_ISOTROPIC ); //Rectangle( hDC, 100, 100, 200, 200 ); //恢復舊對映模式 SetMapMode( hDC, nOldMap ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 對映設定座標 // WinMap.cpp : Defines the entry point for the application. // #include "stdafx.h" HINSTANCE g_hInst = NULL; void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); Rectangle( hDC, 100, 100, 200, 200 ); //設定新對映模式 //int nOldMap = // SetMapMode( hDC, MM_LOMETRIC ); //繪圖 //Rectangle( hDC, 100, -100, 200, -200 ); //SetMapMode( hDC, MM_HIMETRIC ); //Rectangle( hDC, 100, -100, 200, -200 ); int nOldMap = SetMapMode( hDC, MM_ANISOTROPIC ); SetViewportExtEx( hDC, 4, 4, NULL ); SetWindowExtEx( hDC, 2, 2, NULL ); SetViewportOrgEx( hDC, 100, 100, NULL ); Rectangle( hDC, 100, 100, 200, 200 ); //恢復舊對映模式 SetMapMode( hDC, nOldMap ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 文字 // WinText.cpp : Defines the entry point for the application. // #include "stdafx.h" HINSTANCE g_hInst = NULL; void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); CHAR szText[260] = "Hello Text, long long long long"; TextOut( hDC, 100, 100, szText, strlen(szText) ); RECT rcText = { 0 }; rcText.left = 100; rcText.top = 120; rcText.right = 200; rcText.bottom = 200; Rectangle( hDC, rcText.left, rcText.top, rcText.right, rcText.bottom ); DrawText( hDC, szText, strlen(szText), &rcText, DT_CENTER|DT_WORDBREAK); //DT_VCENTER|DT_SINGLELINE|DT_NOCLIP ); CHAR szText2[12] = "E你xtTex好"; INT nText[10] = { 30, 0, 30, 30, 30, 30, 30, 30, 0, 10 }; ExtTextOut( hDC, 100, 300, ETO_OPAQUE, NULL, szText2, strlen(szText2), nText ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 設定文字和字型顏色及背景 // WinText.cpp : Defines the entry point for the application. // #include "stdafx.h" HINSTANCE g_hInst = NULL; void OnPaint( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps = { 0 }; HDC hDC = BeginPaint( hWnd, &ps ); //設定文字顏色 COLORREF nOldTextColor = SetTextColor( hDC, RGB(255, 0, 0 ) ); //設定文字的背景顏色 COLORREF nOldBKColor = SetBkColor( hDC, RGB( 0, 255, 0 )); //設定字型 HFONT hFont = CreateFont( 300, 30, 100, 0, FW_BLACK, TRUE, TRUE, TRUE, DEFAULT_CHARSET, 0, 0, 0, 0, "宋體" ); //選擇字型到DC HFONT hOldFont = ( HFONT ) SelectObject( hDC, hFont ); //繪製文字 CHAR szText[260] = "Hello Text, long long long long"; TextOut( hDC, 100, 100, szText, strlen(szText) ); //設定文字背景繪製模式 SetBkMode( hDC, TRANSPARENT ); RECT rcText = { 0 }; rcText.left = 100; rcText.top = 120; rcText.right = 200; rcText.bottom = 200; Rectangle( hDC, rcText.left, rcText.top, rcText.right, rcText.bottom ); DrawText( hDC, szText, strlen(szText), &rcText, DT_CENTER|DT_WORDBREAK); //DT_VCENTER|DT_SINGLELINE|DT_NOCLIP ); CHAR szText2[12] = "E你xtTex好"; INT nText[10] = { 30, 0, 30, 30, 30, 30, 30, 30, 0, 10 }; ExtTextOut( hDC, 100, 300, ETO_OPAQUE, NULL, szText2, strlen(szText2), nText ); SetTextColor( hDC, nOldTextColor ); //取出字型 SelectObject( hDC, hOldFont ); //刪除字型 DeleteObject( hFont ); EndPaint( hWnd, &ps ); } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_PAINT: OnPaint( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_BTNFACE+1); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; } -------------------------------------------------------------------------------- // 有模式對話方塊 // WinDlg.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" HINSTANCE g_hInst = NULL; //對話方塊的視窗處理函式 BOOL CALLBACK SettingDlgProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM ) { switch( nMsg ) { case WM_SYSCOMMAND: { switch( wParam ) { case SC_CLOSE: //關閉對話方塊並返回1 EndDialog( hWnd, 1 ); break; } } break; } //return DefDlgProc( hWnd, nMsg, // wParam, lParam ); return 0; } void Setting( HWND hWnd ) { //建立對話方塊並等候返回 int nRet = DialogBox( g_hInst, MAKEINTRESOURCE(IDD_SETTING), hWnd, SettingDlgProc ); //根據返回值做不同處理 if( 1 == nRet ) { MessageBox( NULL, "取消配置", "WinDlg", MB_OK ); } else { MessageBox( NULL, "應用配置", "WinDlg", MB_OK ); } } void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nCmdID = LOWORD( wParam ); switch( nCmdID ) { case ID_SETTING: Setting( hWnd ); break; case ID_EXIT: PostQuitMessage( 0 ); break; } } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HMENU hMenu = LoadMenu( g_hInst, MAKEINTRESOURCE(IDR_MAIN) ); HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; }