錯誤的程式段:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbrush; ///////////////////////
HPEN hpen;
static int dispmode = -1;
LPCTSTR str;
switch (message)
{.....}
hbrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
hpen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hdc, hbrush); ///////////////error:使用了可能未初始化的本地指標變數"hdc"?
SelectObject(hdc, hpen);
RoundRect(hdc, 50, 120, 100, 200, 15, 15);
EndPaint(hWnd, &ps);
return 0;
}
完整代碼:
// Win32Project1.cpp : 定義應用程式的入口點。
//
include "stdafx.h"
include "Win32Project1.h"
define MAX_LOADSTRING 100
// 全域變數:
HINSTANCE hInst; // 當前實體
WCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類名
// 此代碼模塊中包含的函式的前向宣告:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代碼。
// 初始化全域字串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 執行應用程式初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
MSG msg;
// 主訊息回圈:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函式: MyRegisterClass()
//
// 目的: 注冊視窗類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函式: InitInstance(HINSTANCE, int)
//
// 目的: 保存實體句柄并創建主視窗
//
// 注釋:
//
// 在此函式中,我們在全域變數中保存實體句柄并
// 創建和顯示主程式視窗。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 將實體句柄存盤在全域變數中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd) {
return FALSE;
}
ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
return TRUE;}
//
// 函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 處理主視窗的訊息。
//
// WM_COMMAND - 處理應用程式選單
// WM_PAINT - 繪制主視窗
// WM_DESTROY - 發送退出訊息并回傳
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbrush; ///////////////////////
HPEN hpen;
static int dispmode = -1;
LPCTSTR str;
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析選單選擇:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_LBUTTONDOWN:
InvalidateRect(hWnd, nullptr, TRUE);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
dispmode = 0;
switch (dispmode)
{
case 0:str = _T(" hi");
SetMapMode(hdc, MM_TEXT);
TextOut(hdc, 0, 0, str, _tcsclen(str));break;
case 1:str = _T("hi 2");
SetMapMode(hdc, MM_ISOTROPIC);
SetWindowExtEx(hdc, 20, 20, nullptr);
TextOut(hdc, 0, 0, str, _tcsclen(str));break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
hbrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
hpen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hdc, hbrush); ///////////////error:使用了可能未初始化的本地指標變數"hdc"?
SelectObject(hdc, hpen);
RoundRect(hdc, 50, 120, 100, 200, 15, 15);
EndPaint(hWnd, &ps);
return 0;
}
// “關于”框的訊息處理程式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
uj5u.com熱心網友回復:
HBRUSH hbrush=0;uj5u.com熱心網友回復:
hdc = BeginPaint(hWnd, &ps);在 paint 外 要 使用
hdc=GetDC(hWnd);
注意 這是2個不同的DC!
uj5u.com熱心網友回復:
GetDCThe GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context.
The GetDCEx function is an extension to GetDC, which gives an application more control over how and whether clipping occurs in the client area.
HDC GetDC(
HWND hWnd // handle to a window
);
Parameters
hWnd
Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen.
Windows 98, Windows NT 5.0 and later: If this parameter is NULL, GetDC retrieves the device context for the primary display monitor. To get the device context for other display monitors, use the EnumDisplayMonitors and CreateDC functions.
Return Values
If the function succeeds, the return value identifies the device context for the specified window's client area.
If the function fails, the return value is NULL.
Windows NT: To get extended error information, callGetLastError.
Remarks
The GetDC function retrieves a common, class, or private device context depending on the class style specified for the specified window. For common device contexts, GetDC assigns default attributes to the device context each time it is retrieved. For class and private device contexts, GetDC leaves the previously assigned attributes unchanged.
After painting with a common device context, the ReleaseDC function must be called to release the device context. Class and private device contexts do not have to be released. The number of device contexts is limited only by available memory.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Device Contexts Overview, Device Context Functions, GetDCEx, ReleaseDC, GetWindowDC
uj5u.com熱心網友回復:
BeginPaintThe BeginPaint function prepares the specified window for painting and fills a PAINTSTRUCT structure with information about the painting.
HDC BeginPaint(
HWND hwnd, // handle to window
LPPAINTSTRUCT lpPaint
// pointer to structure for paint information
);
Parameters
hwnd
Handle to the window to be repainted.
lpPaint
Pointer to the PAINTSTRUCT structure that will receive painting information.
Return Values
If the function succeeds, the return value is the handle to a display device context for the specified window.
If the function fails, the return value is NULL, indicating that no display device context is available.
Windows NT: To get extended error information, callGetLastError.
Remarks
The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area. If the update region is marked for erasing, BeginPaint sends a WM_ERASEBKGND message to the window.
An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function.
If the caret is in the area to be painted, BeginPaint automatically hides the caret to prevent it from being erased.
If the window's class has a background brush, BeginPaint uses that brush to erase the background of the update region before returning.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Painting and Drawing Overview, Painting and Drawing Functions, EndPaint, InvalidateRect, InvalidateRgn, PAINTSTRUCT, ValidateRect, ValidateRgn
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/106585.html
標籤:基礎類
